Search code examples
pythonscipysparse-matrix

scipy sparse matrices, copy argument


What is the function of copy argument in construction of scipy sparse arrays?

scipy.sparse.lil_matrix(arg1, shape=None, dtype=None, copy=False)

It doesn't seem to do anything!

When I construct a sparse matrix from another one and explicitly set copy=False, changing one matrix does not change the other.

import scipy.sparse as sp
import numpy as np
A = sp.csc_matrix(np.array([[1,0],[0,0]]))
B = sp.csr_matrix(A, copy=False)
B[1,1] = 1 #editing B should change A but it does not 
print A.data, B.data #these values are different

Thanks


Solution

  • Both CSC and CSR matrices are internally represented as three 1D arrays. These three arrays will in general be different for different formats, even if they represent the exact same data. There is therefore no way that you can have to sparse matrix objects pointing to the same data but accessing it in different formats.

    What the copy argument can allow you to do is having two sparse matrix objects of the same format pointing to the same data. For example:

    a = sps.csr_matrix([[1, 0], [0, 0]])
    b = sps.csr_matrix(a, copy=False)
    
    >>> a.data
    array([1])
    >>> b.data
    array([1])
    >>> a[0, 0] = 2
    >>> a.data
    array([2])
    >>> b.data
    array([2])
    

    This also has limitations, for instance for the CSR (and CSC) format(s), breaking the sparsity structure will break the commonality of data, as it requires instantiating new arrays, not simply changing the values in the existing ones:

    >>> a[1, 1] = 5
    >>> a.data
    array([2, 5])
    >>> b.data
    array([2])