# How to create a sparse Matrix in Python?

In this article, we will show you what is a sparse matrix and how to create a sparse matrix in python.

### What is a sparse matrix?

A sparse matrix is one in which most of the elements are 0. That is, the matrix only contains data in a few positions. And most of the memory consumed by a sparse matrix is made up of zeroes.

For example −

M = [
[1, 0, 0, 0],
[0, 0, 3, 0],
[0, 0, 0, 0],
[0, 0, 0, 2]
]


The usage of a 2D array to represent a sparse matrix wastes a lot of memory because the zeroes in the matrix are useless in most scenarios. As a result, rather than keeping zeroes alongside non-zero elements, we just store non-zero elements. This involves using triples to store non-zero elements (Row, Column, value).

Natural language processing(NLP) and data encoding both make massive use of sparse matrices. If the majority of the matrix elements are 0, storing all of the matrix elements becomes expensive in terms of storage.

This is because we only have a few data points and the majority of the storage is occupied by redundant zeros.

The following are the two primary advantages of using a sparse matrix rather than a simple matrix −

• Storage − Because there are fewer non-zero elements than zeros, less memory can be used to store solely those elements.

• Computing time − Computing time can be saved by logically creating a data structure that traverses only non-zero elements.

## How to create a sparse matrix in python?

SciPy in Python provides tools for creating sparse matrices using various data structures, as well as for converting a dense matrix to a sparse matrix.

In Python, We can create a sparse matrix using the following functions −

• csr_matrix() function − creates a sparse matrix in compressed sparse row format,

• csc_matrix() function − creates a sparse matrix in compressed sparse column format.,,

## Method 1. Creating a sparse matrix using csr_matrix() function

It creates a sparse matrix in compressed sparse row format.

### Syntax

scipy.sparse.csr_matrix(shape=None, dtype=None)


### parameters

• shape − It is the shape of the matrix

• dtype − It is the datatype of the matrix

### Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

• Use the import keyword, to import the numpy module with an alias name(np).

• Use the import keyword, to import the csr_matrix function from scipy module.

• Use csr_matrix() function to create a 3 * 3 sparse matrix(row format) of int datatype and convert into array using the toarray() function.

• Print the resultant sparse matrix.

### Example

The following program returns the sparse matrix (3x3) using the csr_matrix() function −

# importing numpy module with an alias name
import numpy as np

# importing csr_matrix function from scipy module
from scipy.sparse import csr_matrix

# Using csr_matrix function to create a 3 * 3 sparse matrix of int datatype
# and converting into array
sparse_matrix = csr_matrix((3, 3), dtype = np.int8).toarray()

# printing the resultant sparse matrix
print("The resultant sparse matrix:\n", sparse_matrix)


### Output

On executing, the above program will generate the following output −

The resultant sparse matrix:
[[0 0 0]
[0 0 0]
[0 0 0]]


## Method 2. Creating a sparse matrix using csr_matrix() function of given Numpy Array

### Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

• Use the import keyword, to import the numpy module with an alias name(np).

• Use the import keyword, to import the csr_matrix function from scipy module.

• Create the array using the numpy.array() function(returns an ndarray. The ndarray is an array object that satisfies the given requirements)

### Example

# importing numpy module with alias name
import numpy as np

# importing csr_matrix function from scipy module
from scipy.sparse import csr_matrix

# Giving rows and columns values
rows = np.array([0, 1, 0, 2, 1, 1])
columns = np.array([1, 0, 0, 2, 1, 2])

# Giving array data
arrayData = np.array([1, 3, 2, 5, 7, 6])

# Using csr_matrix function to create a 3x3 sparse matrix
sparse_matrix = csr_matrix((arrayData, (rows, columns)),
shape = (3, 3)).toarray()

# print the resultant sparse matrix
print("The resultant sparse matrix:\n", sparse_matrix)


### Output

On executing, the above program will generate the following output −

The resultant sparse matrix:
[[2 1 0]
[3 7 6]
[0 0 5]]


## Method 3. Creating a sparse matrix using csc_matrix() function

It creates a sparse matrix in compressed sparse column format.

### Syntax

scipy.sparse.csc_matrix(shape=None, dtype=None)


### parameters

• shape − It is the shape of the matrix

• dtype − It is the datatype of the matrix

### Algorithm

Following are the Algorithm/steps to be followed to perform the desired task −

• Use the import keyword, to import the numpy module with an alias name(np).

• Use the import keyword, to import the csc_matrix function from scipy module.

• Use csc_matrix() function to create a 3 * 3 sparse matrix(column format) of int datatype and convert into array using the toarray() function.

• Print the resultant sparse matrix.

### Example

The following program returns the sparse matrix (3x3) in column format using the csc_matrix() function −

# importing numpy module with an alias name
import numpy as np

# importing csc_matrix function from scipy module
from scipy.sparse import csc_matrix

# Using csc_matrix function to create a 3 * 3 sparse matrix of int datatype
# and converting into array
sparse_matrix = csc_matrix((3, 3), dtype = np.int8).toarray()

# printing the resultant sparse matrix
print("The resultant sparse matrix:\n", sparse_matrix)


### Output

On executing, the above program will generate the following output −

The resultant sparse matrix:
[[0 0 0]
[0 0 0]
[0 0 0]]


## Method 4. Creating a sparse matrix using csc_matrix() function of given Numpy array

### Example

The following program returns the sparse matrix (3x3) in column format of integers using the csc_matrix() function −

import numpy as np
# importing csc_matrix function from scipy module
from scipy.sparse import csc_matrix

# Giving rows and columns values
rows = np.array([0, 1, 0, 2, 1, 1])
columns = np.array([1, 0, 0, 2, 1, 2])

# Giving array data
arrayData = np.array([1, 3, 2, 5, 7, 6])

# Using csc_matrix function to create a 3x3 sparse matrix in column format
sparse_matrix = csc_matrix((arrayData, (rows, columns)),
shape = (3, 3)).toarray()

# print the resultant sparse matrix
print("The resultant sparse matrix:\n", sparse_matrix)


### Output

On executing, the above program will generate the following output −

The resultant sparse matrix:
[[2 1 0]
[3 7 6]
[0 0 5]]


### Conclusion

In this tutorial, we learnt four distinct ways to generate a sparse matrix in Python. We also learned how to generate a sparse matrix from a numpy array.

Updated on: 20-Oct-2022

5K+ Views