Sachin_ruk Sachin_ruk - 4 months ago 9
Python Question

list of functions with parameters

I need to obtain a list of functions, where my function is defined as follows:

import theano.tensor as tt

def tilted_loss(y,f,q):
e = (y-f)
return q*tt.sum(e)-tt.sum(e[e<0])


I attempted to do

qs = np.arange(0.05,1,0.05)
q_loss_f = [tilted_loss(q=q) for q in qs]


however, get the error
TypeError: tilted_loss() missing 2 required positional arguments: 'y' and 'f'
. I attempted the simpler
a = tilted_loss(q=0.05)
with the same result.

How do you go about creating this list of functions when parameters are required? Similar questions on SO consider the case where parameters are not involved.

Answer

You can use functools.partial:

q_loss_f = [functools.partial(tilted_loss, q=q) for q in qs]
Comments