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:
- You import the function by name; why do you call it from the
so
namespace? - You have the same functions given for
f
andfprime
; it's usually incorrect unlessf = 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'