因此,我正在为波士顿房屋数据集建立回归模型,并且由于某种原因,当我尝试在梯度下降算法中减去两个506x1矩阵时(prediction_error = np.subtract(y,prediction_function)它给了我一个506x506矩阵(prediction_error)。我之前做过两次相同的操作,没有发生任何错误。我尝试使用np.subtract而不是仅在python中使用常规减号,但没有任何更改。有人可以帮我吗?picture of the code along with the REPL message
您的数组y
的形状为(506,)
,另一个数组的形状为(506,1)
,并且python彼此广播。尝试将它们重塑为类似的形状,如下所示:
np.subtract(y,prediction_function.reshape(y.shape))
要查看效果,这里是一个示例代码,可以更好地理解:
A = np.arange(5).reshape(5,1)
B = np.arange(5).reshape(1,5)
np.subtract(B, A)
[[ 0 1 2 3 4]
[-1 0 1 2 3]
[-2 -1 0 1 2]
[-3 -2 -1 0 1]
[-4 -3 -2 -1 0]]
np.subtract(B, A.reshape(B.shape))
[[0 0 0 0 0]]