CSC format helps store big sparse matrices efficiently by saving only non-zero values and their positions. This saves memory and speeds up calculations.
CSC format (Compressed Sparse Column) in SciPy
from scipy.sparse import csc_matrix csc = csc_matrix((data, (row_indices, col_indices)), shape=(rows, cols))
data is a list or array of non-zero values.
row_indices and col_indices specify the positions of these values.
from scipy.sparse import csc_matrix data = [4, 5, 7] rows = [0, 1, 2] cols = [0, 2, 2] csc = csc_matrix((data, (rows, cols)), shape=(3, 3)) print(csc.toarray())
import numpy as np from scipy.sparse import csc_matrix arr = np.array([[0, 0, 1], [2, 0, 0], [0, 3, 0]]) csc = csc_matrix(arr) print(csc.data)
This program creates a 3x3 sparse matrix with four non-zero values placed at specific row and column positions. It then prints the full matrix as a normal 2D array.
from scipy.sparse import csc_matrix # Define non-zero values and their positions data = [10, 20, 30, 40] row_indices = [0, 2, 2, 0] col_indices = [0, 0, 1, 2] # Create a 3x3 sparse matrix in CSC format matrix = csc_matrix((data, (row_indices, col_indices)), shape=(3, 3)) # Print the dense form to see the full matrix print(matrix.toarray())
CSC format stores data column by column, which makes column operations faster.
To access rows efficiently, consider CSR format instead.
You can convert between sparse formats easily using tocsc() and tocsr() methods.
CSC format stores only non-zero values and their row positions, organized by columns.
It is memory efficient for large sparse matrices and speeds up column-based operations.
Use scipy.sparse.csc_matrix to create and work with CSC matrices in Python.
Practice
scipy.sparse primarily store for a sparse matrix?Solution
Step 1: Understand CSC format storage
CSC stands for Compressed Sparse Column, which stores only non-zero values and their row indices, grouped by columns.Step 2: Compare options with CSC definition
Only non-zero values and their row indices, organized by columns matches the CSC format definition exactly, while others include zeros or wrong organization.Final Answer:
Only non-zero values and their row indices, organized by columns -> Option AQuick Check:
CSC stores non-zero values by columns = A [OK]
- Thinking CSC stores zero values
- Confusing CSC with CSR (row-based)
- Assuming all values are stored
arr in Python using SciPy?Solution
Step 1: Identify correct SciPy function for CSC
The function to create a CSC matrix isscipy.sparse.csc_matrix().Step 2: Check options for syntax correctness
scipy.sparse.csc_matrix(arr)uses the correct function and syntax.scipy.sparse.csr_matrix(arr)creates CSR, not CSC. Options C and D are invalid functions.Final Answer:
scipy.sparse.csc_matrix(arr) -> Option AQuick Check:
Use csc_matrix() to create CSC format [OK]
- Using csr_matrix instead of csc_matrix
- Trying non-existent functions like dense_matrix
- Missing _matrix suffix in function name
print(csc_matrix.indptr)?
import numpy as np
from scipy.sparse import csc_matrix
arr = np.array([[0, 0, 3],
[4, 0, 0],
[0, 5, 6]])
csc_matrix = csc_matrix(arr)Solution
Step 1: Understand indptr in CSC format
indptr array shows column start indices in data arrays. For 3 columns, length is 4.Step 2: Count non-zero elements per column
Column 0 has 1 non-zero (4), column 1 has 1 non-zero (5), column 2 has 2 non-zeros (3,6). So indptr = [0,1,2,4].Final Answer:
[0 1 2 4] -> Option DQuick Check:
indptr counts cumulative non-zeros per column = B [OK]
- Counting rows instead of columns
- Mixing up indptr with indices array
- Off-by-one errors in cumulative counts
from scipy.sparse import csc_matrix arr = [[1, 0], [0, 2]] csc = csc_matrix(arr, shape=(3,3)) print(csc.toarray())
Solution
Step 1: Check input array and shape parameter
Input array is 2x2 but shape is set to 3x3, causing dimension mismatch.Step 2: Understand effect of shape mismatch
Specifying a larger shape than data size is invalid and raises an error.Final Answer:
Shape parameter is larger than input array size causing mismatch -> Option CQuick Check:
Shape must match input size or smaller [OK]
- Assuming lists are invalid input
- Thinking data type must be float
- Believing toarray() is missing
Solution
Step 1: Understand CSC format advantage
CSC stores data column-wise, making column operations like sum efficient.Step 2: Choose best method for column sums
Using.sum(axis=0)on CSC matrix efficiently sums columns without converting to dense.Final Answer:
Convert to CSC format and sum non-zero values column-wise using .sum(axis=0) -> Option BQuick Check:
CSC + sum(axis=0) = fast column sums [OK]
- Using CSR which is row-based for column sums
- Converting to dense wastes memory
- Manually iterating instead of vectorized sum
Ready to prove it?
Pass the test and this pattern moves unknown
