In [1]:
import numpy as np
from scipy.sparse import csr_matrix, lil_matrix

In [2]:
l = [[0, 1, 2],
     [3, 0, 4],
     [0, 0, 0]]

In [3]:
csr = csr_matrix(l)
lil = lil_matrix(l)

In [4]:
print(csr.sum())


10

In [5]:
print(csr.mean())


1.111111111111111

In [6]:
print(csr.max())


4

In [7]:
print(csr.min())


0

In [8]:
# print(lil.max())
# AttributeError: max not found

In [9]:
print(csr.sqrt().toarray())


[[0.         1.         1.41421356]
 [1.73205081 0.         2.        ]
 [0.         0.         0.        ]]

In [10]:
print(csr.sin().toarray())


[[ 0.          0.84147098  0.90929743]
 [ 0.14112001  0.         -0.7568025 ]
 [ 0.          0.          0.        ]]

In [11]:
print(csr.tan().toarray())


[[ 0.          1.55740772 -2.18503986]
 [-0.14254654  0.          1.15782128]
 [ 0.          0.          0.        ]]

In [12]:
# print(lil.sqrt())
# AttributeError: sqrt not found

In [13]:
csr_ = csr.copy()

In [14]:
print(csr_.data)


[1 2 3 4]

In [15]:
print(type(csr_.data))


<class 'numpy.ndarray'>

In [16]:
csr_.data = np.cos(csr_.data)

In [17]:
print(csr_.toarray())


[[ 0.          0.54030231 -0.41614684]
 [-0.9899925   0.         -0.65364362]
 [ 0.          0.          0.        ]]

In [18]:
csr_ = csr.copy()
csr_.data = csr_.data ** 2
print(csr_.toarray())


[[ 0  1  4]
 [ 9  0 16]
 [ 0  0  0]]

In [19]:
print(lil.data)


[list([1, 2]) list([3, 4]) list([])]

In [20]:
print(csr)


  (0, 1)	1
  (0, 2)	2
  (1, 0)	3
  (1, 2)	4

In [21]:
r, c = csr.nonzero()
print(r, c)


[0 0 1 1] [1 2 0 2]

In [22]:
print(csr.count_nonzero())


4

In [23]:
print(csr.nnz)


4

In [24]:
csr[0, 1] = 0
print(csr)


  (0, 1)	0
  (0, 2)	2
  (1, 0)	3
  (1, 2)	4

In [25]:
print(csr.count_nonzero())


3

In [26]:
print(csr.nnz)


4

In [27]:
r, c = csr.nonzero()
print(r, c)


[0 1 1] [2 0 2]

In [28]:
print(lil)


  (0, 1)	1
  (0, 2)	2
  (1, 0)	3
  (1, 2)	4

In [29]:
print(lil.nnz)


4

In [30]:
lil[0, 1] = 0

In [31]:
print(lil)


  (0, 2)	2
  (1, 0)	3
  (1, 2)	4

In [32]:
print(lil.nnz)


3