• Skip to secondary menu
  • Skip to main content
  • Skip to primary sidebar

Python and R Tips

Learn Data Science with Python and R

  • Home
  • Python
  • Pandas
    • Pandas 101
  • tidyverse
    • tidyverse 101
  • R
  • Linux
  • Conferences
  • Python Books
  • About
    • Privacy Policy
You are here: Home / Python / Sparse Matrix in Python / 3 ways to create sparse matrix / 3 Ways To Create Sparse Matrix in COO Format with SciPy

3 Ways To Create Sparse Matrix in COO Format with SciPy

January 21, 2019 by cmdlinetips

How To Create Sparse Matrix in COO Format?
How To Create Sparse Matrix in COO Format?

Sparse matrices greatly help dealing with large matrices with a lot of missing data. Sparse matrix efficiently store data set with a lot sparsity in matrix. It offers a much smaller memory foot print to store and access than the full matrix. With SciPy’s Sparse module, one can directly use sparse matrix for common arithmetic operations, like addition, subtraction, multiplication, division, and more complex matrix operations.

Among the many types of sparse matrices available in Python SciPy package, we will see examples of creating sparse matrix in Coordinate Format or COO format.

Coordinate list format or COO format stores data as a list of tuple with three elements; row, column, value. The first element is row index, the second element is column index, and the third element is the value to be stored in the row and column. As you you can imagin, the tuple is present only for non-zero elements. The biggest advantages of sparse matrix in COO format is that one can construct the sparse matrix really fast and can convert the COO sparse matrix to other sparse matrix formats like Compressed Sparse Row matrix (CSR) and Compressed Sparse Column matrix (CSC).

Let us load Sparse matrix module for SciPy to access the sparse matrix functions. Let us also load NumPy and we will use NumPy’s random module to generate random numbers.

# load coo_matrix from Scipy.sparse module
from scipy.sparse import coo_matrix
# import numpy
import numpy as np

1. How To Construct an Empty Sparse Matrix in COO format?

We can construct empty sparse matrix in COO format using the coo_matrix() from scipy.sparse. To create a empty COO matrix of size 4×5, we can do as

# create empty COO matrix
A = coo_matrix((4, 5))
print(A)

When we print the empty matrix we will see nothing, as there are no non-zero elements in the sparse matrix.

>A.toarray()
array([[0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.]])

Similarly, we can also use todense() function to get all the content of a sparse matrix.

A.todense()
matrix([[0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.]])

We can also specify the data type of the elements in the empty sparse matrix with dtype. Here we construct empty sparse matrix of size 3×4 with integers

coo_matrix((3, 4), dtype=np.int8).toarray()
array([[0, 0, 0, 0],
       [0, 0, 0, 0],
       [0, 0, 0, 0]], dtype=int8)


2. How To Construct COO matrix from a Dense Matrix?

Sometimes we may have the data already as a dense matrix and we might like to convert the dense matrix into a sparse one so that we can store the data efficiently.

Let us create a dense matrix with ones and zeroes using NumPy’s random module.

A_dense = np.random.randint(2, size=(3, 4))

We can print the dense matrix and see its content.

>print(A_dense)
[[1 1 1 0]
 [1 0 0 1]
 [0 1 0 0]]

We can use the coo_matrix() function to convert the dense matrix to

A_coo = coo_matrix(A_dense)

And we can see the content of sparse matrix and it will print the (i,j,k) tuples for the elements with non-zero values

>print(A_coo)
(0, 0)	1
  (0, 1)	1
  (0, 2)	1
  (1, 0)	1
  (1, 3)	1
  (2, 1)	1

3. How To Construct COO matrix from data in (i,j,v) format?

Sometimes you have the sparse data in co-ordinate list format as tuples like row, col, and value format, where row and col correspond to row and column indices. With Scipy’s sparse module we can easily create sparse matrix in COO format.

Let us first create some data in (i,j,v) format. The row, col, and data elements are stored as numpy arrays.

# Constructing a matrix using ijv format
row  = np.array([0, 3, 1, 2, 3, 2])
col  = np.array([0, 1, 1, 2, 0, 1])
data = np.array([10, 3, 88, 9, 2,6])

Let us provide row, col, data arrays as input argument to coo_matrix function and also specify the dimention of sparse matrix

>B = coo_matrix((data, (row, col)), shape=(4, 4))

When we print the COO matrix we will see the data in sparse (row, col, val) format.

>print(B)
  (0, 0)	10
  (3, 1)	3
  (1, 1)	88
  (2, 2)	9
  (3, 0)	2
  (2, 1)	6

If we want to see the data in matrix form

>B.toarray()
array([[10,  0,  0,  0],
       [ 0, 88,  0,  0],
       [ 0,  6,  9,  0],
       [ 2,  3,  0,  0]])

Scipy’s sparse module also has a lot of utility functions to work with sparse matrices. For example, scipy.sparse.isparse can tell if the matrix is sparse or not.

>from scipy.sparse import isspmatrix_coo, isspmatrix
>scipy.sparse.issparse(B)
True

We can also specifically check if it is COO matrix or not with isspmatrix_coo() function.

>isspmatrix_coo(B)
True

Share this:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on X (Opens in new window) X

Related posts:

Default ThumbnailHow To Slice Rows and Columns of Sparse Matrix in Python? Default ThumbnailIntroduction to Sparse Matrices in Python with SciPy Default ThumbnailHow To Create Random Sparse Matrix of Specific Density? Default ThumbnailHow To Visualize Sparse Matrix in Python?

Filed Under: 3 ways to create sparse matrix, COO Sparse Matrix, SciPy Sparse Matrices, Sparse Matrix in Python Tagged With: 3 ways to create sparse matrix, COO Sparse Matrix, Sparse Matrix in Python

Primary Sidebar

Subscribe to Python and R Tips and Learn Data Science

Learn Pandas in Python and Tidyverse in R

Tags

Altair Basic NumPy Book Review Data Science Data Science Books Data Science Resources Data Science Roundup Data Visualization Dimensionality Reduction Dropbox Dropbox Free Space Dropbox Tips Emacs Emacs Tips ggplot2 Linux Commands Linux Tips Mac Os X Tips Maximum Likelihood Estimation in R MLE in R NumPy Pandas Pandas 101 Pandas Dataframe Pandas Data Frame pandas groupby() Pandas select columns Pandas select_dtypes Python Python 3 Python Boxplot Python Tips R rstats R Tips Seaborn Seaborn Boxplot Seaborn Catplot Shell Scripting Sparse Matrix in Python tidy evaluation tidyverse tidyverse 101 Vim Vim Tips

RSS RSS

  • How to convert row names to a column in Pandas
  • How to resize an image with PyTorch
  • Fashion-MNIST data from PyTorch
  • Pandas case_when() with multiple examples
  • An Introduction to Statistical Learning: with Applications in Python Is Here
  • 10 Tips to customize ggplot2 title text
  • 8 Plot types with Matplotlib in Python
  • PCA on S&P 500 Stock Return Data
  • Linear Regression with Matrix Decomposition Methods
  • Numpy’s random choice() function

Copyright © 2025 · Lifestyle Pro on Genesis Framework · WordPress · Log in

Go to mobile version