Python &引用;类型错误:';numpy.ndarray和#x27;对象不可调用";-Numpy错误
这是我的密码Python &引用;类型错误:';numpy.ndarray和#x27;对象不可调用";-Numpy错误,python,python-3.x,numpy,matrix-multiplication,Python,Python 3.x,Numpy,Matrix Multiplication,这是我的密码 def h(x, theta): # this is probability/hypotheses return np.dot(x, theta) def cost(x, y, theta): # this is cost function m = x.shape[0] hypothesis = h(x, theta) error = hypothesis - y
def h(x, theta): # this is probability/hypotheses
return np.dot(x, theta)
def cost(x, y, theta): # this is cost function
m = x.shape[0]
hypothesis = h(x, theta)
error = hypothesis - y
return 1 / (2 * m) * (np.dot(error.T, error)) # (1/2m)*sum[(error)^2]
我有一个函数“h”,它计算两个矩阵的点积。它正在按预期工作。
我测试了它,这里是输出
print("x.shape = ", x.shape) # x.shape = (97, 2)
print("theta.shape =", theta.shape) # theta.shape = (2, 1)
print("my_hypothesis.shape =", my_hypothesis.shape) # my_hypothesis.shape = (97, 1)
但当我从“cost”函数中调用函数“h”时。
假设=h(x,θ)
我得到一个错误:
TypeError: 'numpy.ndarray' object is not callable
如果我用假设=np.dot(x,θ)替换直线假设=h(x,θ),那么它工作得很好
请告诉我我做错了什么?我已经解决了这个问题,我只是做了这个陈述
返回(alpha*(1/m)*(np.dot(error.T,x))).T
下面是工作代码的链接
您确定没有在代码中的某个地方重新定义
h
?对我来说,这看起来像是一个打字错误,涉及到你没有展示的代码。还有,为什么不干脆h=np.dot
?函数在Python中是一流的公民——您可以直接将它们分配给变量。此外,您可以说“我测试了它[指h
),这里是输出”,但接下来给出了三行,其中甚至没有提到h
。这三行如何构成对h
的测试?请提供一份报告。这是代码,这是我在代码中使用的数据,当我运行j_列表,θ列表,假设=批量梯度下降(x,y,θ,alpha)时,我再次遇到同样的错误