I added a tag scipy, this is a problem scipy.sparse, not np.matrix.
In [250]: y=sparse.csr_matrix([[0,1],[1,0]])
In [251]: x=np.arange(2)
In [252]: y+x
Out[252]:
matrix([[0, 2],
[1, 1]])
sparse matrix + array =>
( , np.matrix np.ndarray. sparse.csr_matrix . numpy, ).
In [255]: x += y
In [256]: x
Out[256]:
matrix([[0, 2],
[1, 1]])
; x = x+y x, x.
y matrix, . 1d- 2d.
In [258]: x += y.todense()
...
ValueError: non-broadcastable output operand with shape (2,) doesn't match the broadcast shape (2,2)
x 2d - :
In [259]: x=np.eye(2)
In [260]: x
Out[260]:
array([[ 1., 0.],
[ 0., 1.]])
In [261]: x += y.todense()
In [262]: x
Out[262]:
array([[ 1., 1.],
[ 1., 1.]])
/ . . , . y+1, , .
, , - x+=..., y .
In [265]: x += y.A
In [266]: x
Out[266]:
array([[ 1., 2.],
[ 2., 1.]])
, .
( scipy github ).
scipy/sparse/compress.py csr. x+y x.__add__(y), y.__add__(x). x+=y x.__iadd__(y). __iadd__ ndarray.
:
def __add__(self,other):
if isscalarlike(other):
if other == 0:
return self.copy()
else:
raise NotImplementedError('adding a nonzero scalar to a '
'sparse matrix is not supported')
elif isspmatrix(other):
if (other.shape != self.shape):
raise ValueError("inconsistent shapes")
return self._binopt(other,'_plus_')
elif isdense(other):
return self.todense() + other
else:
return NotImplemented
, y+x y.todense() + x. x+y .
+=, , (array np.matrix) . , .
, , . y+y , . y+=y NotImplmenentedError sparse.base.__iadd__.
, , y (2,2).
In [348]: x=np.eye(2)
In [349]: x+y
Out[349]:
matrix([[ 1., 1.],
[ 1., 1.]])
In [350]: x+y.todense()
Out[350]:
matrix([[ 1., 1.],
[ 1., 1.]])
, x x ( )
In [351]: x[:] = x+y
In [352]: x
Out[352]:
array([[ 1., 1.],
[ 1., 1.]])
+= :
In [353]: x += y.todense()
In [354]: x
Out[354]:
array([[ 1., 2.],
[ 2., 1.]])
- +=sparse x
In [355]: x += y
In [356]: x
Out[356]:
matrix([[ 1., 3.],
[ 3., 1.]])
id(x) x.__array_interface__ , x += y x. , x np.matrix. , += inplace. x += y.todense() - inplace.