Reputation: 47
i am trying to run the following script i have an error with (Fmin) i think. i have the following error:
TypeError: 'numpy.float64' object is not callable
thank you very much , i tried alot to solve it but i was not able ...Thanks again
import numpy as n
from scipy import optimize
a=2
b=3
def f (ts):
c= ts
y= optimize.fmin(np.linalg.norm(a/c +b),x0=0.1)
return y
f2=np.vectorize(f)
ts=np.linspace(1,50)
print(f2(ts))
Upvotes: 0
Views: 754
Reputation: 23356
Search for other TypeError: ... is not callable
questions. This means you've made a programming error by passing a non-callable object somewhere that expects a callable object. In Python, "callable" generally means a function (or any object that accepts the obj()
syntax. The scipy.optimize
APIs typically take a function as their first argument, being the objective function you wish to optimize.
The expression np.linalg.norm(a/b + c)
is not a function. You are simply evaluating the function on one argument and returning the result. It's not clear from your code which variable(s) you want to optimize for. Let's say it's c
(with a
and b
fixed) (though you should really call it x
--by convention it's common to use high letters in the Latin alphabet like w
, x
, y
, z
as independent variables, and low letters like a
, b
, c
as constants--of course, this is highly context and domain specific). Then you might do
from np.linalg import norm
optimize.fmin(lambda x: norm(a/x + b), x0=0.01)
The lambda x:
operator in this defines an in-line one argument function that implements your objective function on a single variable. Of course, you can also optimize all of a
, b
, and c
together as a vector. But you haven't specified what problem you're trying to solve, so I'll leave that as an exercise.
I should add, using np.vectorize
the way you are is very slow. You can use fmin
on an array.
Upvotes: 1
Reputation: 175
optimize.fmin()
takes in the first argument as a callable function but it is provided with a float64 object i.e. the return value of np.linalg.norm(a/c +b)
Upvotes: 0