Python:ValueError:操作数无法与形状一起广播(101)(2)
运行下面显示的代码时,我收到以下错误消息:“ValueError:操作数无法与形状(101)(2)一起广播” 全文如下:Python:ValueError:操作数无法与形状一起广播(101)(2),python,numpy,Python,Numpy,运行下面显示的代码时,我收到以下错误消息:“ValueError:操作数无法与形状(101)(2)一起广播” 全文如下: Traceback (most recent call last): File "/Users/Bob/Desktop/MachineLearning/machLearn.py", line 47, in <module> ppn.fit(X,y) File "/Users/Bob/Desktop/MachineLearning/machLearn
Traceback (most recent call last):
File "/Users/Bob/Desktop/MachineLearning/machLearn.py", line 47, in <module>
ppn.fit(X,y)
File "/Users/Bob/Desktop/MachineLearning/machLearn.py", line 18, in fit
self.w_[1:] += update * xi
ValueError: operands could not be broadcast together with shapes (101) (2)
[Finished in 0.8s with exit code 1]
注意:上面的代码示例来自Python机器学习(Sebastian Raschka,2015,第26页)
self.w_[1:
具有大小X.shape[1]
,我假设是102
,而xi
将具有大小X.shape[0]
,我认为是2
您可能要更改此行:
for xi, target in zip(X, y):
到
这将给出
xi
X.shape[1]以匹配self.w_[1:][/code>这三个项目中的一个self.w_[1:],更新,xi
具有形状(101,),另一个是(2,)-即一个项目中有101个,另一个项目中有2个。哪些数字有意义?您可能需要验证这一点。
for xi, target in zip(X, y):
for xi, target in zip(X.T, y):