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.
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)2Practical Example
Here is a real-world application of sparse.csc_matrix() showing how it is used in production SciPy code.
from scipy import sparse
csr = sparse.csr_matrix([[0, 1], [2, 0]])
csc = csr.tocsc()
print(type(csc).__name__)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.
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)