向稀疏矩阵添加非零标量

Adding a non-zero scalar to sparse matrix

我想为我的稀疏矩阵中的每个非零元素添加一个值。谁能给我一个方法。

y=sparse.csc_matrix((df[column_name].values,(df['user_id'].values, df['anime_id'].values)),shape=(rows, cols))
x=np.random.laplace(0,scale)
y=y+x

上面的代码给我一个错误。

已提供但未发表评论:

In [166]: from scipy import sparse
In [167]: M = sparse.random(5,5,.2,'csc')
In [168]: M
Out[168]: 
<5x5 sparse matrix of type '<class 'numpy.float64'>'
    with 5 stored elements in Compressed Sparse Column format>
In [169]: M.A
Out[169]: 
array([[0.24975586, 0.        , 0.        , 0.        , 0.        ],
       [0.        , 0.        , 0.        , 0.        , 0.        ],
       [0.        , 0.        , 0.        , 0.6863175 , 0.        ],
       [0.43488131, 0.19245474, 0.26190903, 0.        , 0.        ],
       [0.        , 0.        , 0.        , 0.        , 0.        ]])


In [171]: x=np.random.laplace(0,10)
In [172]: x
Out[172]: 0.4773577605565098
In [173]: M+x
Traceback (most recent call last):
  Input In [173] in <cell line: 1>
    M+x
  File /usr/local/lib/python3.8/dist-packages/scipy/sparse/_base.py:464 in __add__
    raise NotImplementedError('adding a nonzero scalar to a '
NotImplementedError: adding a nonzero scalar to a sparse matrix is not supported

这是您最初应该显示的错误消息。

In [174]: M.data += x
In [175]: M.A
Out[175]: 
array([[0.72711362, 0.        , 0.        , 0.        , 0.        ],
       [0.        , 0.        , 0.        , 0.        , 0.        ],
       [0.        , 0.        , 0.        , 1.16367526, 0.        ],
       [0.91223907, 0.6698125 , 0.73926679, 0.        , 0.        ],
       [0.        , 0.        , 0.        , 0.        , 0.        ]])