我有一段代码可以运行回归,但是我不知道为什么会出现语法错误。下面的代码

时间:2020-02-13 02:01:53

标签: python

def computeCost(X,y,theta):  
    tobesummed = np.power(((X @ theta.T)-y),2) # he yells invalid syntax no idea why   
    return np.sum(tobesummed)/(2 * len(X))  

1 个答案:

答案 0 :(得分:0)

其中有一个@符号,Python会忽略它作为装饰器。您是说*还是X.dot(theta.T)

import numpy as np
def computeCost(X,y,theta):
    tobesummed = np.power(((X * theta.T)-y),2)
    return np.sum(tobesummed)/(2 * len(X))

X, y, theta = np.random.randn(1,10), np.random.randn(1,10), np.random.randn(1,10) 
print(computeCost(X, y, theta))

打印90.03281689585363