A sparse matrix stores only nonzeros. CSR is a common format. Print nbytes vs a dense array.
Goal
Build a diagonal CSR matrix and print density.
from scipy import sparse
A = sparse.diags([1, 2, 3, 4], format='csr')
print(A.toarray())
print('nnz', A.nnz)from scipy import sparse
n = 40
A = sparse.eye(n, format='csr')
dense = A.toarray()
print('sparse nbytes', A.data.nbytes)
print('dense nbytes', dense.nbytes)from scipy import sparse
A = sparse.csr_matrix([[0, 2, 0], [1, 0, 0], [0, 0, 3]])
print(A)
print(A.toarray())from scipy import sparse
print(sparse.random(4, 4, density=0.25, random_state=0).toarray().round(2))