Question:

When slicing a `numpy`

array we obtain a view on the corresponding data. However that doesn't seem to be the case with sparse matrices from <a href="https://docs.scipy.org/doc/scipy/reference/sparse.html" rel="nofollow">`scipy.sparse`

</a>. Although <a href="https://docs.scipy.org/doc/scipy/reference/sparse.html#usage-information" rel="nofollow">the docs</a> briefly mention slicing for the `lil_matrix`

class it's not clear how (or if) one can obtain views on the data.

At least by using the following sample script I wasn't successful in obtaining views of sparse matrices:

```
import numpy as np
from scipy.sparse import lil_matrix
def test(matrix):
print('\n=== Testing {} ==='.format(type(matrix)))
a = matrix[:, 0]
b = matrix[0, :]
a[0] = 100
M[0, 1] = 200
M[1, 0] = 200
print('a = '); print(a)
print('b = '); print(b)
M = np.arange(4).reshape(2, 2) + 1
S = lil_matrix(M)
test(M)
test(S)
```

Which outputs:

```
=== Testing <class 'numpy.ndarray'> ===
a =
[100 200]
b =
[100 200]
=== Testing <class 'scipy.sparse.lil.lil_matrix'> ===
a =
(0, 0) 100
(1, 0) 3
b =
(0, 0) 1
(0, 1) 2
```

<hr /><strong>Tested on</strong> `Python 3.6.6, numpy==1.14.5, scipy==1.1.0`

.

I'll eat my words - partially. There is a `lilmatrix`

`getrowview`

method (but not a `getcolview`

).

A `lil`

matrix has 2 object dtype array attributes, `data`

and `rows`

. Both contain lists, one for each row.

```
def getrow(self, i):
"""Returns a copy of the 'i'th row.
"""
i = self._check_row_bounds(i)
new = lil_matrix((1, self.shape[1]), dtype=self.dtype)
new.rows[0] = self.rows[i][:]
new.data[0] = self.data[i][:]
return new
def getrowview(self, i):
"""Returns a view of the 'i'th row (without copying).
"""
new = lil_matrix((1, self.shape[1]), dtype=self.dtype)
new.rows[0] = self.rows[i]
new.data[0] = self.data[i]
return new
```

A little testing shows that modifying elements of the row view does affect the parent, and v.v.

This `view`

works because an object array contains pointers. As with pointers in a list, they can be shared. And if done right, such a list can be modified in-place.

I found this by doing a page search for `view`

on the `lil_matrix`

documentation. I don't find anything similar for the other formats.

There are numerical functions on the `csr`

format that work directly with the `.data`

attribute. This is possible if you aren't changing sparsity, and only want to modify the nonzero values. And it is possible to modify that attribute in place. In limited cases it might be possible to construct a new sparse matrix that shares slices of the data attribute of another, but it would not be anything as general as `ndarray`

slicing.

```
In [88]: M = sparse.lil_matrix((4,10),dtype=int)
In [89]: M[0,1::2] = 1
In [90]: M[1,::2] = 2
In [91]: M1 = M.getrowview(0)
In [92]: M1[0,::2] = 3
In [94]: M.A
Out[94]:
array([[3, 1, 3, 1, 3, 1, 3, 1, 3, 1],
[2, 0, 2, 0, 2, 0, 2, 0, 2, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])
In [95]: M[0,1::2] = 4
In [97]: M1.A
Out[97]: array([[3, 4, 3, 4, 3, 4, 3, 4, 3, 4]])
```

Following this model I could make an 'advanced-index`view, something that`

ndarray` doesn't do:

```
In [98]: M2 = sparse.lil_matrix((2,10), dtype=int)
In [99]: M2.rows[:] = M.rows[[0,3]]
In [100]: M2.data[:] = M.data[[0,3]]
In [101]: M2.A
Out[101]:
array([[3, 4, 3, 4, 3, 4, 3, 4, 3, 4],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])
In [102]: M2[:,::2] *= 10
In [103]: M2.A
Out[103]:
array([[30, 4, 30, 4, 30, 4, 30, 4, 30, 4],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])
In [104]: M1.A
Out[104]: array([[30, 4, 30, 4, 30, 4, 30, 4, 30, 4]])
```