CFR
CFR

Reputation: 131

How to minimize one argument of a function using Scipy?

I have the following function

def fun(X, alpha, y):
    #some stuff
    return J, gradient

And I am trying to minimze alpha with this, but nothing happens.

optimized_alpha = sp.optimize.minimize(lambda t: fun(X, t, y), alpha, method="Newton-CG", jac=True)

Upvotes: 2

Views: 1104

Answers (1)

Saullo G. P. Castro
Saullo G. P. Castro

Reputation: 58985

You can use functools.partial to turn your function to partial function with only one argument. In order to make it work with scipy.optimize.minimize you will need to keep the variable argument at the last position:

def fun(X, y, alpha):
    #some stuff
    return J, gradient

then:

from functools import partial

optfunc = partial(func, X, y)
optimized_alpha = sp.optimize.minimize(optfunc, alpha, method="Newton-CG", jac=True)

Upvotes: 3

Related Questions