【发布时间】:2021-04-13 22:08:09
【问题描述】:
我是 Python 的新手,我一直在坚持现在该做什么,因为我不断收到此错误。我正在尝试更改以下等式:
z = np.power(((float(X) * theta.T)-float(Y)), 2)
但我似乎无法让它工作。
我的代码:
# convert from data frames to numpy matrices
X = np.matrix(x.values)
Y = np.matrix(y.values)
theta = np.matrix(np.array([0,0]))
# cost functionstrong text
def computeCost(X, Y, theta):
z = np.power(((X * theta.T)-Y), 2)
return np.sum(z) / (2 * len(X))
print('computeCost(X, y, theta) = ' , computeCost(X, Y, theta))
矩阵充满了浮点数:
X = [[ 1. 6.1101] [ 1. 5.5277] [ 1. 8.5186] ...
错误信息:
TypeError: unsupported operand type(s) for -: 'float' and 'str'
感谢我能得到的所有帮助。非常感谢
【问题讨论】:
-
哪一行导致了这个错误?
-
计算成本中的行 z = np.power(((X * theta.T)-Y), 2)
-
您需要检查
Xtheta和Y的数据类型,其中一个似乎是str而您希望它们是float -
认为你是对的 :)