Compressed Sparse formats CSR and CSC in Python
Last Updated :
28 Jun, 2024
In Python, two of the most commonly used sparse matrix formats are Compressed Sparse Row (CSR) and Compressed Sparse Column (CSC). This article will explore the differences between CSR and CSC formats, their use cases, and how to work with them using Python's SciPy library.
Difference Between CSR and CSC Formats
Characteristics | CSR Formats | CSC Formats |
---|
Storage Orientation | Row-wise compression | Column-wise compression |
---|
Efficient Operations | Row slicing, matrix-vector multiplication | Column slicing, matrix transposition |
---|
Indexing | Uses row pointers to the store start of each row
| Uses column pointers to the store start of each column
|
---|
Applications | Solving the linear systems, iterative methods, and sparse row operations
| The Matrix factorizations, eigenvalue problems, and sparse column operations |
---|
Conversion | Can be converted to/from other formats like CSC easily | Can be converted to/from other formats like CSR easily
|
---|
Space Efficiency | Depends on the sparsity pattern
| Depends on the sparsity pattern
|
---|
Example Usage in Python | csr_matrix from the SciPy
| csc_matrix from the SciPy
|
---|
What is CSR Format?
The CSR format is optimized for fast row slicing and matrix-vector products. It compresses the sparse matrix by storing only the non-zero elements and the corresponding row indices. The structure is divided into three one-dimensional arrays:
- Data: The Stores the non-zero values.
- Indices: The Stores the column indices of the elements in the data array.
- Indptr: Stores the index pointers to the start of the each row in the data array.
Example
Here’s how to create and manipulate a sparse matrix in the CSR format using the Python's SciPy library:
Python
import numpy as np
from scipy.sparse import csr_matrix
# Example sparse matrix
matrix = np.array([[0, 0, 1],
[4, 0, 0],
[0, 0, 3]])
# Create a CSR matrix
csr = csr_matrix(matrix)
# Display the CSR matrix
print("CSR matrix:")
print(csr)
# Components of CSR matrix
print("\nData:", csr.data)
print("Indices:", csr.indices)
print("Index pointer:", csr.indptr)
Output :
CSR matrix:
(0, 2) 1
(1, 0) 4
(2, 2) 3
Data: [1 4 3]
Indices: [2 0 2]
Index pointer: [0 1 2 3]
What is CSC Format?
The CSC format is optimized for fast column slicing and efficient arithmetic operations. It is similar to CSR but compresses the matrix by storing only the non-zero elements and the corresponding column indices. The structure is divided into three one-dimensional arrays:
- Data: Stores the non-zero values.
- Indices: The Stores the row indices of the elements in the data array.
- Indptr: Stores the index pointers to the start of the each column in the data array.
Example
Here’s how to create and manipulate a sparse matrix in CSC format using the Python's SciPy library:
Python
import numpy as np
from scipy.sparse import csc_matrix
# Example sparse matrix
matrix = np.array([[0, 0, 1],
[4, 0, 0],
[0, 0, 3]])
# Create a CSC matrix
csc = csc_matrix(matrix)
# Display the CSC matrix
print("CSC matrix:")
print(csc)
# Components of CSC matrix
print("\nData:", csc.data)
print("Indices:", csc.indices)
print("Index pointer:", csc.indptr)
Output
CSC matrix:
(1, 0) 4
(0, 2) 1
(2, 2) 3
Data: [4 1 3]
Indices: [1 0 2]
Index pointer: [0 1 1 3]
Conclusion
Understanding the differences between the CSR and CSC formats is crucial for the selecting the appropriate the storage method based on the specific computational needs. The CSR is optimized for the row operations while CSC excels in the column operations. Both formats significantly enhance the efficiency of the sparse matrix computations making them indispensable tools in the scientific computing.
Similar Reads
SciPy CSGraph â Compressed Sparse Graph Graphs are powerful mathematical structures used to represent relationships between entities in various fields, including computer science, social networks, transportation systems, and more. Analyzing and computing graphs is a fundamental task in many applications, but it can be challenging, especia
9 min read
bz2.compress(s) in Python With the help of bz2.compress(s) method, we can get compress the bytes of string by using bz2.compress(s) method. Syntax : bz2.compress(string) Return : Return compressed string. Example #1 : In this example we can see that by using bz2.compress(s) method, we are able to compress the string in the b
1 min read
gzip.decompress(s) in Python With the help of gzip.decompress(s) method, we can decompress the compressed bytes of string into original string by using gzip.decompress(s) method. Syntax : gzip.decompress(string) Return : Return decompressed string. Example #1 : In this example we can see that by using gzip.decompress(s) method,
1 min read
gzip.compress(s) in Python With the help of gzip.compress(s) method, we can get compress the bytes of string by using gzip.compress(s) method. Syntax : gzip.compress(string) Return : Return compressed string. Example #1 : In this example we can see that by using gzip.compress(s) method, we are able to compress the string in t
1 min read
bz2.decompress(s) in Python With the help of bz2.decompress(s) method, we can decompress the compressed bytes of string into original string by using bz2.decompress(s) method. Syntax : bz2.decompress(string) Return : Return decompressed string. Example #1 : In this example we can see that by using bz2.decompress(s) method, we
1 min read