I am trying to use scipy.optimize spo, and keep on getting error " 'numpy.float64' object is not callable". Could anyone point me to where the error is coming from? TIA!
The first argument of scipy.optimize.minimize is a call able, and you seem to give it a function value instead. In the OP screenshot, what is -sharpe_ratio.
You might be looking for lambda x, prices_norm : -sharpe_ratio(x, prices_norm) or some such.
Related
I am implementing unscented kalman filter and getting this error "numpy.ndarray object is not callable" for the non-linear function 'g' in the prediction step.
enter image description here
I have also attached my code where I got this error. Any assistance would be highly appreciated. Thanks!
Just like the error message says, gx is a numpy array:
gx = np.array([g_E, g_R])
But you are trying to call it as if it were a function:
self.sigmas_x[:,i] = gx(self.sigmas[:,i],dt, u)
Hence the error.
I'm using Python to work with networkx and draw some graphs.
I ran into a problem raising:
TypeError: 'dict' object is not callable
on this line of code:
set_node_color(num, list(Graph.node()))
I searched to find that this error is raised when I'm using a variable name dict.
The problem is, I'm not using any variables with the name dict, nor am I using any dictionary types anywhere in the code.
In case it's necessary, printing the type of Graph gives <class 'networkx.classes.digraph.Digraph'>.
I also tried printing the type for Graph.node() only to receive the same error, telling me 'dict' object is not callable.
So I suspect Graph.node() to be a dict type variable, but using (Graph.node()).items() raises the same TypeError.
Any help or advices would be nice. Thanks.
Maybe Graph.node is a dict object, so Graph.node() is not callable.
So I have been writing a code to standardize the elements of a matrix and the function I used is as follows:
def preprocess(Data):
if stdn ==True:
st=np.empty((Data.shape[0],Data.shape[1]))
for i in xrange(0,Data.shape[0]):
st[i,0]=Data[i,0]
for i in xrange(1,Data.shape[1]):
st[:,i]=((Data[:,i]-np.min(Data[:,i]))/(np.ptp(Data[:,i])))
np.random.shuffle(st)
return st
else:
return Data
It works very well outside the class but when used inside of it it gives me this error:
AttributeError: 'tuple' object has no attribute 'shape'
Any idea on how I can fix it??
P.S. This is a KNN classification code
According to the error you posted, Data is of type tuple and there is no attribute shape defined for data. You could try casting Data when you call your preprocess function, e.g.:
preprocess(numpy.array(Data))
Running
np.log(math.factorial(21))
throws an AttributeError: log. Why is that? I could imagine a ValueError, or some sort of UseYourHighSchoolMathsError, but why the attribute error?
The result of math.factorial(21) is a Python long. numpy cannot convert it to one of its numeric types, so it leaves it as dtype=object. The way that unary ufuncs work for object arrays is that they simply try to call a method of the same name on the object. E.g.
np.log(np.array([x], dtype=object)) <-> np.array([x.log()], dtype=object)
Since there is no .log() method on a Python long, you get the AttributeError.
Prefer the math.log() function, that does the job even on long integers.
I got a problem that the np.load() doesn't work suddenly.
When I run a code like this:
import numpy as np
a=np.array([[1,2,3],[4,5,6],[7,8,9]])
print a
np.save('test',a)
p=np.load('test.npy')
There is a problem: TypeError: 'str' object is not callable
I used np.save() and np.load() many times before it worked well. I don't know why it suddenly become wrong. Please help me to see where is the mistake. Thanks!