scikit-learn/scikit-learn — comment on commit b453e1e721966048a6d4f8224e254b24073914e4
File:  line 0
By GaelVaroquaux:

I don't think that this is a good idea. Indeed, it doesn't seem that `csr_matrix` will copy the data:

In [20]: data = np.array([[1,2,3,4],[1,2,3,4],[1,2,3,4]])

In [21]: diags = np.array([0,-1,2])

In [22]: X = sparse.spdiags(data, diags, 4, 4)

In [23]: A = sparse.csr_matrix(X)

In [24]: B = sparse.csr_matrix(A)

In [25]: np.may_share_memory(B.data, A.data)
Out[25]: True

In [28]: A.data
Out[28]: array([1, 3, 1, 2, 4, 2, 3, 3, 4])

In [29]: B.data *= 2

In [30]: A.data
Out[30]: array([2, 6, 2, 4, 8, 4, 6, 6, 8])

But I do think that we should alsways operate on a new sparse matrix X, the reason being that on line 98 the input sparse matrix will be modified. We shouldn't have side effects like this by default (we can have a `copy=False` attribute that switches on such a behavior, though).