Trusted answers to developer questions

Sparse matrices in Python

Get Started With Machine Learning

Learn the fundamentals of Machine Learning with this free course. Future-proof your career by adding ML skills to your toolkit — or prepare to land a job in AI or Data Science.

Matrices that mostly contain zeroes are said to be sparse.

Sparse matrices are commonly used in applied machine learning (such as in data containing data-encodings that map categories to count) and even in whole subfields of machine learning such as natural language processing (NLP).

An example of a Sparse matrix
An example of a Sparse matrix

Sparse matrices contain only a few non-zero values. Storing such data in a two-dimensional matrix data structure is a waste of space. Also, it is computationally expensive to represent and work with sparse matrices as though they are dense. A significant improvement in performance can be achieved by using representations and operations that specifically handle matrix sparsity.

Sparse matrices in Python

Python’s SciPy provides tools for creating sparse matrices using multiple data structures, as well as tools for converting a dense matrix to a sparse matrix. The sparse matrix representation outputs the row-column tuple where the matrix contains non-zero values along with those values.

import numpy as np
from scipy.sparse import csr_matrix
# create a 2-D representation of the matrix
A = np.array([[1, 0, 0, 0, 0, 0], [0, 0, 2, 0, 0, 1],\
[0, 0, 0, 2, 0, 0]])
print("Dense matrix representation: \n", A)
# convert to sparse matrix representation
S = csr_matrix(A)
print("Sparse matrix: \n",S)
# convert back to 2-D representation of the matrix
B = S.todense()
print("Dense matrix: \n", B)

RELATED TAGS

sparse
matrix
python
machine
learning
Copyright ©2024 Educative, Inc. All rights reserved
Did you find this helpful?