Title: Create A Sparse Matrix
Slug: create_a_sparse_matrix
Summary: How to create a sparse matrix in Python.
Date: 2017-09-03 12:00
Category: Machine Learning
Tags: Vectors Matrices Arrays
Authors: Chris Albon
In [1]:
# Load libraries
import numpy as np
from scipy import sparse
In [2]:
# Create a matrix
matrix = np.array([[0, 0],
[0, 1],
[3, 0]])
In [ ]:
# Create compressed sparse row (CSR) matrix
matrix_sparse = sparse.csr_matrix(matrix)
Note: There are many types of sparse matrices. In the example above we use CSR but the type we use should reflect our use case.