Search code examples
pythondataframesparse-matrix

Adding a non-zero scalar to sparse matrix


I want to add a value to each non-zero element in my sparse matrix. Can someone give me a method to do that.

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

The above code is giving me an error.


Solution

  • Offered without comment:

    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
    

    This is the error message you should have shown initially.

    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.        ]])