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

问题描述 投票:-1回答:1
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))  
python
1个回答
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

© www.soinside.com 2019 - 2024. All rights reserved.