0
我想将numpy阵列中的值添加到numpy矩阵的特定行中的值。将阵列的值添加到numpy矩阵的行中
考虑:
A = [[0, 0], [0, 0]]
b = [1, 1]
我想在A. B加入A值的第一行中的预期结果是:
[[1, 1], [0, 0]]
我使用 “+” 操作符尝试过,但出现错误:
>>> import numpy
>>> a = numpy.zeros(shape=(2,2))
>>> a
array([[ 0., 0.],
[ 0., 0.]])
>>> b = numpy.ones(shape=(1,2))
>>> b
array([[ 1., 1.]])
>>> a[0, :] += b
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: non-broadcastable output operand with shape (2,) doesn't match the broadcast shape (1,2)
这样做的最佳方法是什么?