🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEscipy

scipy Documentation

LOADING ENGINE...

sparse.csc_matrix()

AI & DATA SCIENCE // sparse-csc-matrix

scipy.sparse.csc_matrix() creates a Compressed Sparse Column matrix, a memory-efficient sparse matrix format optimized for fast column access and column-wise operations, the column-oriented counterpart to csr_matrix.

Syntax

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

Deep Dive Course

csc_matrix stores the exact same kind of data as csr_matrix, only the nonzero values plus indexing information, but organizes that indexing by column instead of by row, making column slicing and certain column-oriented linear algebra operations, like some sparse solvers, significantly faster, at the cost of being comparatively slower for row-wise operations, the reverse tradeoff from CSR. Choosing between CSR and CSC in practice usually comes down to which axis your specific algorithm accesses most frequently.

1Understanding sparse.csc_matrix()

csc_matrix stores the exact same kind of data as csr_matrix, only the nonzero values plus indexing information, but organizes that indexing by column instead of by row, making column slicing and certain column-oriented linear algebra operations, like some sparse solvers, significantly faster, at the cost of being comparatively slower for row-wise operations, the reverse tradeoff from CSR. Choosing between CSR and CSC in practice usually comes down to which axis your specific algorithm accesses most frequently.

💡

Choose CSC over CSR specifically when your algorithm predominantly accesses or slices columns rather than rows — many sparse linear solvers, for instance, are specifically documented to expect or perform better with CSC input.

editor.html
from scipy import sparse
import numpy as np

dense = np.array([[0, 0, 3], [4, 0, 0], [0, 5, 0]])
sparse_matrix = sparse.csc_matrix(dense)
print(sparse_matrix)
localhost:3000

2Practical Example

Here is a real-world application of sparse.csc_matrix() showing how it is used in production SciPy code.

editor.html
from scipy import sparse

csr = sparse.csr_matrix([[0, 1], [2, 0]])
csc = csr.tocsc()
print(type(csc).__name__)
localhost:3000

3Best Practices

Follow these guidelines when working with sparse.csc_matrix():

1. Choose CSC when column-wise access or column-oriented operations dominate your algorithm's actual usage pattern, and CSR when row-wise access dominates instead

2. Check a specific sparse solver or algorithm's documentation for which format it expects or performs best with, rather than assuming one format is universally superior

3. Convert between CSR and CSC with .tocsc()/.tocsr() as needed, rather than rebuilding a sparse matrix from scratch in the other format

⚠️

Tip: Choose CSC over CSR specifically when your algorithm predominantly accesses or slices columns rather than rows — many sparse linear solvers, for instance, are specifically documented to expect or perform better with CSC input.

editor.html
from scipy import sparse
import numpy as np

dense = np.array([[0, 0, 3], [4, 0, 0], [0, 5, 0]])
sparse_matrix = sparse.csc_matrix(dense)
print(sparse_matrix)
localhost:3000

Examples

Example 01Basic Usage
from scipy import sparse
import numpy as np

dense = np.array([[0, 0, 3], [4, 0, 0], [0, 5, 0]])
sparse_matrix = sparse.csc_matrix(dense)
print(sparse_matrix)
Example 02Advanced Example
from scipy import sparse

csr = sparse.csr_matrix([[0, 1], [2, 0]])
csc = csr.tocsc()
print(type(csc).__name__)

Best Practices

  • Choose CSC when column-wise access or column-oriented operations dominate your algorithm's actual usage pattern, and CSR when row-wise access dominates instead
  • Check a specific sparse solver or algorithm's documentation for which format it expects or performs best with, rather than assuming one format is universally superior
  • Convert between CSR and CSC with .tocsc()/.tocsr() as needed, rather than rebuilding a sparse matrix from scratch in the other format

Interview Question

Why does printing the same sparse matrix as CSR versus CSC show its nonzero entries listed in a different order?

Hint: Think about what order each format's internal indexing structure naturally iterates over the data in.

CSR organizes and stores its nonzero entries internally ordered row by row, so iterating over its stored data naturally visits entries in row-major order, top row first, left to right within each row. CSC instead organizes its entries column by column, so iterating over its internal storage naturally visits entries in column-major order, leftmost column first, top to bottom within each column. Both formats represent the exact same underlying matrix and the same set of nonzero values, but the physical order those values are stored in, and therefore printed in, differs based on which axis each format is organized around.

Exercises

MediumPractice using sparse.csc_matrix() in a real scenario.
View Solution
from scipy import sparse
import numpy as np

dense = np.array([[0, 0, 3], [4, 0, 0], [0, 5, 0]])
sparse_matrix = sparse.csc_matrix(dense)
print(sparse_matrix)

Frequently Asked Questions

Why does printing the same sparse matrix as CSR versus CSC show its nonzero entries listed in a different order?

CSR organizes and stores its nonzero entries internally ordered row by row, so iterating over its stored data naturally visits entries in row-major order, top row first, left to right within each row. CSC instead organizes its entries column by column, so iterating over its internal storage naturally visits entries in column-major order, leftmost column first, top to bottom within each column. Both formats represent the exact same underlying matrix and the same set of nonzero values, but the physical order those values are stored in, and therefore printed in, differs based on which axis each format is organized around.

Related Functions

sparse-csr-matrixsparse-coo-matrixndarray-t