December 2017
Intermediate to advanced
386 pages
10h 42m
English
Converting a matrix into its sparse form
Sparse matrices are built using the functions available in scipy.sparse.
In the following example, we will be using the function named csr_matrix to convert a given matrix into its sparse form.
import numpy as npfrom scipy import sparse
A = np.array([[1,2,0],[0,0,3],[1,0,4]])
sA = sparse.csr_matrix(A)
sA<3x3 sparse matrix of type '<class 'numpy.int32'>' with 5 stored elements in Compressed Sparse Row format>
Note that the preceding output mentions that there are five stored elements, which is the same as having ...
Read now
Unlock full access