Sitemap
Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

Sparse Data Vs Sparse Matrix

Here’s a head-to-head descriptive explanation using python

6 min readJan 8, 2025

--

Press enter or click to view image in full size
Image edited by author, generated by ideogram.ai

Data sparsity and sparse matrices are critical concepts in computational science and data analysis, it is used particularly for large datasets with many zero or missing values.

In the case of Python’s SciPy, it makes efficient provision for sparse storage and operations of matrices, enabling faster computations despite reduced memory usage and understanding their contrasts and applications becomes paramount for optimized performance in fields such as machine learning, graph theory and scientific computing.

Table of Content

  • Sparse data and sparse matrix
  • Block sparse row matrix
  • Coordinate list matrix
  • Compressed sparse column matrix
  • Compressed sparse row matrix
  • Diagonal sparse matrix
  • Dictionary of keys sparse matrix
  • Row-based linked list sparse matrix

Sparse Data and Sparse Matrix

The unused elements which do not carry information are

coined as sparse data.

We can understand that each dataset
will have unused data which will trouble us, this will stop the
flow of algorithms in machine learning.

Observe the given

array for sparse data [11,0,0,0,0,12,0,0,0,13,0,0,0].

Now here, I can show,
the legal values are 11, 12 and 13. So, the dataset having
more items as zero is the sparse data.

In a dense array, most of the values are non-zero which means they will have
some information.

Get A.I Hub’s stories in your inbox

Join Medium for free to get updates from this writer.

It provides functions to deal with sparse data, scipy has a
module named as scipy.sparse and we will be discussing its categories too.

1. Block Sparse Row Matrix

  • The Block Sparse Row matrix format in scipy is
    made to handle sparse matrices with a block structure
    quickly and effectively.
  • It is used to effectively store and
    handle block-structured matrices, where each block

    represents a dense sub-matrix.

Parameters

  • shape
  • dtype
  • copy
  • blocksize

Now, we implement practically, so that we can crystal clear our concept.

import numpy as mynp
import scipy.sparse as myscpy

# Creating a dense matrix
mydense_matrix = mynp.array([[11, 12, 0, 0],
[13, 14, 15, 0],
[0, 16, 71, 18],
[0, 0, 19, 100]])

# Converting the dense matrix to BSR format
mybsr_matrix = myscpy.bsr_matrix(mydense_matrix, blocksize=(2, 2))
print(mybsr_matrix)

Output

 (0, 0) 11
(0, 1) 12
(1, 0) 13
(1, 1) 14
(0, 2) 0
(0, 3) 0
(1, 2) 15
(1, 3) 0
(2, 0) 0
(2, 1) 16
(3, 0) 0
(3, 1) 0
(2, 2) 71
(2, 3) 18
(3, 2) 19
(3, 3) 100

2. Coordinate List Matrix

  • When only a small portion of a matrixʼs elements are non-zero and their positions are known, the Coordinate List Matrix can be used to efficiently store and handle
    the sparse matrix.
  • For sparse matrices where only non-zero
    values are stored along with their row and column indices,
    the COO list matrix is frequently used.
import numpy as mynp
import scipy.sparse as myscpy

myrows = mynp.array([0, 0, 0, 1, 1, 1])
mycols = mynp.array([0, 1, 2, 0, 1, 2])
mydata = mynp.array([11, 12, 13, 14, 15, 16])

mymatrix = myscpy.coo_matrix((mydata, (myrows, mycols)))
print(mymatrix)

Output

 (0, 0) 11
(0, 1) 12
(0, 2) 13
(1, 0) 14
(1, 1) 15
(1, 2) 16

3. Compress Sparse Column Matrix

  • The Compressed Sparse Column matrix in scipy is
    a sparse matrix stored in a column major format.
  • It implies
    that the matrix’s columns are stored consecutively in
    memory while the row indices of non-zero elements are
    stored in a separate array.
  • The CSC format is widely

    preferred for sparse matrices due to its high efficiency in

    column-oriented operations like matrix multiplication and

    vector matrix multiplication.
import scipy.sparse as myscpy

# Creating a 3x3 CSC matrix
mydata = [11, 12, 13, 14, 15, 16] # non-zero values in column-wise order
myrow_indices = [0, 1, 2, 0, 1, 2] # row indices of non-zero values

mycol_pointers = [0, 2, 4, 6] # index pointers to start of columns
mycsc_matrix = myscpy.csc_matrix((mydata, myrow_indices, mycol_pointers), shape=(3, 3))
print(mycsc_matrix)

Output

 (0, 0) 11
(1, 0) 12
(2, 1) 13
(0, 1) 14
(1, 2) 15
(2, 2) 16

4. Compressed Sparse Row Matrix

  • The non-zero elements of the SciPy Compressed Sparse
    Row
    matrix are kept in three different arrays, data,
    indices and indptr.
  • The non-zero values of the matrix are
    contained in the data array in row-major order.
  • The column indices for each non-zero value are kept in the indices array.
  • The starting and ending indices of rows in the indices and

    data arrays are indicated by the indptr array.
import numpy as mynp
import scipy.sparse as myscpy

# print(help(myscpy.csr_matrix))
myarray = mynp.array([0,0,1,0,0,2,0,3,0])
print(myscpy.csr_matrix(myarray))
print('-'*50)

myarray2 = mynp.array([[0,0,1],[0,0,2],[0,3,0]])
print('Viewing non-zero items using data property')
print(myscpy.csr_matrix(myarray2).data)

print('-'*50)
print('Counting non-zero items using count_nonzero method')
print(myscpy.csr_matrix(myarray2).count_nonzero())

print('-'*50)
print('Eliminating zero items using eliminate_zeros method')

myvar = myscpy.csr_matrix(myarray2)
myvar.eliminate_zeros()
print(myvar)

Output

 (0, 2) 1
(0, 5) 2
(0, 7) 3

--------------------------------------------------
Viewing non-zero items using data property [1 2 3]
-------------------------------------------------
Counting non-zero items using count_nonzero method 3
--------------------------------------------------
Eliminating zero items using eliminate_zeros
method

(0, 2) 1
(1, 2) 2
(2, 1) 3

5. Diagonal Sparse Matrix

  • Diagonal Sparse matrix format in scipy is used to
    store matrices with a large percentage of zero-valued members and non-zero components that are only found on
    the diagonal. For diagonal matrices, this format is extremely
    beneficial.
import scipy.sparse as myscpy

# Create a diagonal matrix
mydiagonal_values = [11, 12, 13, 14]
mydiagonal_offsets = [0] # The diagonal is at offset 0

mydia_matrix = myscpy.dia_matrix((mydiagonal_values, mydiagonal_offsets), shape=(4, 4))
print(mydia_matrix)

Output

 (0, 0) 11
(1, 1) 12
(2, 2) 13
(3, 3) 14

6. Dictionary of Key Sparse Matrix

  • One of the sparse matrix representations in the library is the
    Dictionary Of Keys where elements with non-zero
    values are stored in a dictionary.
  • It is implemented using a

    dictionary data structure in which the keys correspond to

    row and column indices and the values represent the corresponding element values.
import numpy as mynp
import scipy.sparse as myscpy

# Create an empty DOK matrix with shape (3, 3)
mymatrix = myscpy.dok_matrix((3, 3), dtype=mynp.float32)
# Set some non-zero elements
mymatrix[0, 1] = 12.5
mymatrix[1, 2] = 11.3
mymatrix[2, 0] = 14.7
# elements access
print(mymatrix)
# Converting DOK matrix to a dense matrix
mydense_matrix = mymatrix.toarray()
print(mydense_matrix)

Output

 (0, 1) 12.5
(1, 2) 11.3
(2, 0) 14.7
[[ 0. 12.5 0.]
[ 0. 0. 11.3]
[14.7 0. 0. ]]

7. Row Based Linked List Sparse Matrix

  • An additional sparse matrix representation in the scipy

    package is the row-based Linked List sparse matrix
    which is implemented using linked lists and is intended for

    efficient row-wise operations.
  • The LIL matrices are
    especially helpful when the matrix is built incrementally
    since they make element insertion and deletion quick and
    easy.
import numpy as mynp
import scipy.sparse as myscpy

# Create an empty LIL matrix with shape (3, 3)
mymatrix = myscpy.lil_matrix((3, 3), dtype=mynp.float32)
# Set some non-zero elements
mymatrix[0, 1] = 12.5
mymatrix[1, 2] = 11.3
mymatrix[2, 0] = 14.7
# elements access
print(mymatrix[0, 1]) # Output: 2.5
# Converting LIL matrix to a dense matrix
mydense_matrix = mymatrix.toarray()
print(mydense_matrix)

Output

12.5
[[ 0. 12.5 0. ]
[ 0. 0. 11.3]
[14.7 0. 0. ]]

Closing Thoughts

We will conclude this article by the descriptive understanding of sparse data and matrix and along with this approach, we can upgrade our mind of programming with the hands-on drive with python and Scipy.

If you read this article till the end, please consider the following

  • Follow the author to get updates of upcoming articles 🔔
  • If you like anything in this article, consider a clap 👏🏻
  • Highlight that inspired you
  • Comment your precious thoughts, if it is helpful for you
  • Share this article by helping others and showing your love and support ❤️

Thanks For Reading 😊

--

--

A.I Hub
A.I Hub

Written by A.I Hub

We writes about Data Science | Software Development | Machine Learning | Artificial Intelligence | Ethical Hacking and much more. Unleash your potential with us

Dev Genius
Dev Genius

Published in Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development