简体   繁体   中英

"TypeError: 'numpy.ndarray' object is not callable " - Numpy Error

Here is my code.

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]

I have a function "h" which is calculating dot products of 2 matrices. and it is working as expected. I tested it and here is output

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)

But when i am calling function "h" from with-in "cost" function. hypothesis = h(x, theta) I am getting error:

TypeError: 'numpy.ndarray' object is not callable

If i replace line hypothesis = h(x, theta) with hypothesis = np.dot(x, theta) then it is working fine.

Please let me what wrong i am doing?

I have fixed the issue , i just this statement

return (alpha * (1 / m) * (np.dot(error.T, x))).T

Below is link of working code.

https://github.com/amitsuneja/MachineLeaning/blob/master/AndrewNG/Week2/Excercise01/Week2-LinearRegressionSingleVariable.py

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM