February 27th, 2013, 11:45 AM
How to Parly Initialize a Function
Hi All, I'm a first-time poster here, trying to teach myself Python by taking every possible excuse to use it. Normally I'm able to find answers to problems using the Python tutorial or a Google search, but this one has me stumped.
Here's the issue - I have a function that takes another function f(z) as an argument. The function I am trying to pass it takes three arguments - i.e. f(x,y,z). So here's what I'm trying to do:
def: MyMethod(func,z, otherstuff):
x,y = #some list comprehension
z = some other list
newf = f(x,y)
Obviously this doesn't really illustrate why I'm trying to do this, but take my word for it, if it can be done this is definitely the best way to implement my specific application. Anyway, I've tried several different techniques but so far had no luck. Can anyone help?
February 27th, 2013, 11:53 AM
February 27th, 2013, 03:49 PM
You would call the second function from the first function if I understand you correctly, as you don't explain what "otherstuff" is. Also, following the Python Style Guide, functions only contain lower case letters in this case, makes your code easier to read.
def my_method(z, otherstuff):
## assume otherstuff is a list containing x & y
return_val=f(otherstuff, otherstuff, z)
February 28th, 2013, 11:29 AM
Thanks! That's just what I was looking for.
February 28th, 2013, 02:44 PM
Thanks very much for the correction on style. That's a kind of bad habit that's easy to get into when teaching yourself.
Originally Posted by dwblas
Without going into too much detail, my_method is a boostrap function that generates a list of random samples based off of an initial list of values passed it it. It then calculates some statistic (also passed to it) for each new random sample. The currying problem involved a specific use of of bootstrap (resampling residuals from a linear regression, adding the new resids to predicted values from the original fit, then generating a new regression line based on each new sample of residuals). I didn't want to mess with the bootstrap function itself, because I mean it to be a general-purpose tool. But using the currying described above I was able to generate a stat function which did the trick. For fun I also did it using a function that defined another function (very similar to what you suggested). If you're interested, it looks like this: (any tips on style and readability are highly appreciated)
Takes a list of x and y values, performs linear regression on the values.
Generates a list of residuals using the fit and y values. Defines and
instantiates a function new_fit, which generates new y values based on the
yfit values and a new set of residuals, then returns the result of a linear
regression on the new yfit vs x values.
Bootstrap is called on the residuals, with the new_fit function as the stat.
Bootstrap then returns a sample of new fits based on the resamples of the
slope,intercept = np.polyfit(x,y,1)[:2]
fit = lambda x: slope*x + intercept
yfits = [fit(n) for n in x]
resids0 = residuals(fit,x,y)
newY = [a - n for a,n in zip(yfits,resids)]
f = new_fit
bsamples = bootstrap(resids0,f)
Slopes = ([n for n in bsamples])
Intercepts = ([n for n in bsamples])
meanSlope = sp.mean([n for n in bsamples])
meanInt = sp.mean([n for n in bsamples])
print "Mean Slope of sample fits: %f " % sp.mean(Slopes)
print "Mean Intercept of sample fits: %f " % sp.mean(Intercepts)
def bootstrap(vals,stat = sp.mean,its=500):
dist = listDist(vals)
sampleSize = len(vals)
print "Computing Bootstrap Samples..."
sampleStats = [stat(dist.rvs(size= sampleSize)) for a in range(its)]