0
0
SciPydata~5 mins

Sparse matrix operations in SciPy

Choose your learning style9 modes available
Introduction

Sparse matrices save memory by storing only non-zero values. Operations on them help handle big data efficiently.

When working with large datasets that have many zero values, like text data or graphs.
When you want to save memory and speed up calculations by ignoring zeros.
When performing matrix math on data with mostly empty or zero entries.
When building recommendation systems where user-item interactions are mostly missing.
When analyzing networks or social graphs with many nodes but few connections.
Syntax
SciPy
from scipy.sparse import csr_matrix

# Create sparse matrix
sparse_matrix = csr_matrix(data)

# Basic operations
result = sparse_matrix + other_sparse_matrix
result = sparse_matrix.dot(other_sparse_matrix)
result = sparse_matrix.multiply(other_sparse_matrix)

# Convert to dense
dense_matrix = sparse_matrix.toarray()

csr_matrix is a common sparse matrix format for fast arithmetic and slicing.

Use .toarray() to see the full matrix but it uses more memory.

Examples
This creates a sparse matrix with values 1, 2, 3 at positions (0,0), (1,2), and (2,1).
SciPy
from scipy.sparse import csr_matrix

# Create a 3x3 sparse matrix with some values
data = [1, 2, 3]
rows = [0, 1, 2]
cols = [0, 2, 1]
sparse = csr_matrix((data, (rows, cols)), shape=(3,3))
print(sparse.toarray())
Adds two sparse matrices element-wise.
SciPy
from scipy.sparse import csr_matrix

# Add two sparse matrices
A = csr_matrix([[0, 0, 1], [2, 0, 0]])
B = csr_matrix([[1, 0, 0], [0, 3, 0]])
C = A + B
print(C.toarray())
Performs matrix multiplication on sparse matrices.
SciPy
from scipy.sparse import csr_matrix

# Multiply sparse matrices (dot product)
A = csr_matrix([[1, 0], [0, 2]])
B = csr_matrix([[0, 3], [4, 0]])
C = A.dot(B)
print(C.toarray())
Sample Program

This program creates two sparse matrices and shows addition, matrix multiplication, and element-wise multiplication. It prints the dense form to see results clearly.

SciPy
from scipy.sparse import csr_matrix

# Create two sparse matrices
A = csr_matrix(([4, 5, 7], ([0, 1, 2], [1, 2, 0])), shape=(3, 3))
B = csr_matrix(([1, 2, 3], ([0, 1, 2], [1, 0, 2])), shape=(3, 3))

# Add matrices
C = A + B

# Multiply matrices
D = A.dot(B)

# Element-wise multiply
E = A.multiply(B)

# Print results
print("Matrix A:\n", A.toarray())
print("Matrix B:\n", B.toarray())
print("A + B:\n", C.toarray())
print("A dot B:\n", D.toarray())
print("A element-wise multiply B:\n", E.toarray())
OutputSuccess
Important Notes

Sparse matrices save memory but converting to dense with .toarray() can use a lot of memory if the matrix is large.

Use csr_matrix for fast row slicing and arithmetic operations.

Element-wise multiplication uses .multiply() or *.

Summary

Sparse matrices store only non-zero values to save memory.

Use csr_matrix from scipy.sparse to create and operate on sparse matrices.

You can add, multiply (dot product), and element-wise multiply sparse matrices efficiently.