1. 程式人生 > >Python-Numpy: operands could not be broadcast together with shapes

Python-Numpy: operands could not be broadcast together with shapes

BUG樣式:

ValueError: operands could not be broadcast together with shapes (353,) (355,) 

舉個栗子:

import numpy

numpy_a=numpy.array([1, 2, 3, 4, 5])
numpy_b=numpy.array([1, 2, 3])

print(numpy_a + numpy_b)

原因:numpy_a與numpy_b的shape不同, 違反了ufunc的廣播機制, 所以報錯,解決方法。維度調成相同即可

如果大家不知道numpy多維陣列的概念,特點,以及使用方法,主頁有介紹,祝大家生活愉快!