Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

SciPy · Class

sparse.csc_matrix

scipy.sparse.csc_matrix creates compressed sparse column matrices. It accepts dense, sparse, coordinate-form, shape-only, and explicit compressed-storage inputs.

How it works

csc_matrix stores a sparse matrix by columns. In its standard representation, each column's row indices occupy a slice of indices selected by adjacent indptr values, while the corresponding numerical entries are in data; it can also be built from dense input, another sparse matrix, dimensions, coordinate triples, or these storage arrays.

Syntax

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

Parameters

NameTypeDescription
arg1objectConstructor input: a 2-D ndarray, sparse array or matrix, shape tuple, coordinate-form tuple, or compressed-form tuple.
shapetuple[int, int] or NoneOptional matrix shape, written as (M, N).
dtypedtype or NoneOptional element data type; for an empty matrix it defaults to dtype='d'.
copyboolWhether to copy input data when applicable.

Returns

A scipy.sparse.csc_matrix object.

Key points

  • csc_matrix represents a matrix in compressed sparse column format.
  • A two-dimensional NumPy array can be used as the constructor input.
  • A shape tuple constructs an empty sparse matrix, with an optional dtype.
  • Coordinate-form construction accepts values together with row and column index arrays.
  • Each coordinate entry assigns the k-th value to the indexed row and column.
  • The indices slice bounded by adjacent indptr entries contains the row positions for a column.
  • The same boundary slice in data contains the values associated with those row positions.

Common mistakes

  • When constructing from coordinate or compressed arrays, omit shape only when the index information unambiguously determines the intended dimensions.
  • Passing an existing sparse object converts it to CSC format rather than preserving its original storage format.

Example: Create and inspect a CSC matrix

import numpy as np
from scipy.sparse import csc_matrix

A = csc_matrix(np.array([[1, 0, 2], [0, 3, 0]]))
print(A.toarray())
print(A.indptr)
print(A.indices)
print(A.data)

Creates a CSC matrix from a two-dimensional NumPy array and inspects its dense form and three storage arrays.

When to use it

Memory-efficient sparse storage

Store matrices with many zeros while retaining column-oriented compressed storage.

Coordinate-form assembly

Build a matrix from nonzero values and their row and column coordinates.

Compressed representation control

Construct or inspect explicit CSC arrays for interoperability with sparse algorithms.

Interview questions

QWhat is scipy.sparse.csc_matrix?

It is a compressed sparse column matrix. Its compressed representation uses data for values, indices for row positions, and indptr to delimit each column's segment in the first two arrays.

Follow-up: How do you locate the stored entries of column j? Use indices[indptr[j]:indptr[j+1]] for row positions and data[indptr[j]:indptr[j+1]] for their values.

QWhat input forms does csc_matrix accept?

You can pass a two-dimensional ndarray, another sparse object, a shape tuple, coordinate-form data with row and column indices, or the compressed triple data, indices, indptr. The optional shape specifies matrix dimensions where needed.

Follow-up: What relationship must coordinate-form inputs satisfy? For each k, the entry at row_ind[k], col_ind[k] equals data[k].

QHow does the standard CSC representation encode columns?

For column i, row indices are stored in indices[indptr[i]:indptr[i+1]], and matching values are stored in data[indptr[i]:indptr[i+1]]. Thus indptr provides the boundaries for each column's compressed data.

Follow-up: How can you inspect or validate the representation? Compare each column's boundary slice in indices and data, and ensure indptr has the expected column boundary structure.

  • scipy.sparse.csr_matrix
  • scipy.sparse.coo_matrix
  • scipy.sparse.dia_matrix
  • scipy.sparse.bsr_matrix

Source: scipy.sparse.csc_matrix. Checked 2026-10-15.