Home > Software engineering >  Error while using of scipy.optimize.fmin_bfgs
Error while using of scipy.optimize.fmin_bfgs

Time:10-28

My code look like this:

print(X.shape)
print(Y.shape)
print(theta0.shape)

(100, 2)
(100,)
(3,)
from scipy.optimize import fmin_bfgs

model = logistic_func
theta_opt = so.fmin_bfgs(negative_log_likelihood_derivative(theta0, X, Y, model), theta0, 
                         fprime=negative_log_likelihood_derivative(theta0, X, Y, model), 
                         args=(X,Y), disp=True)

and I am getting an error:

'numpy.ndarray' object is not callable

but I am not using numpy.ndarray as a function here. Any advices?

I am not able to resolve the problem by myself.

CodePudding user response:

From the docs:

Parameters

f callable f(x,*args)
     Objective function to be minimized.

x0 ndarray
    Initial guess.

fprime callable f'(x,*args), optional
    Gradient of f.

Callable means that a function must be passed as a parameter. You, however, pass a return value of that function, negative_log_likelihood_derivative(theta0, X, Y, model) (note the round brackets that are used to call the function!). Instead you need to pass the function itself:

theta_opt = so.fmin_bfgs(negative_log_likelihood_derivative, theta0, 
                         fprime=negative_log_likelihood_derivative, 
                         args=(X,Y), disp=True)

Side notes:

  1. You import the function by name; why do you call it from the so namespace?
  2. You have the same functions given for f and fprime; it's usually incorrect unless f = np.exp(x)

CodePudding user response:

"You import the function by name; why do you call it from the so namespace?" - I do not know what you mean by "so namespace"

"You have the same functions given for f and fprime" - corrected

When I type:

theta_opt = so.fmin_bfgs(negative_log_likelihood_derivative, theta0, 
                         fprime=negative_log_likelihood_derivative, 
                         args=(X,Y), disp=True)

I am getting:

negative_log_likelihood_derivative() missing 1 required positional argument: 'model'

  • Related