🚀 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.coo_matrix()

AI & DATA SCIENCE // sparse-coo-matrix

scipy.sparse.coo_matrix() creates a Coordinate-format sparse matrix, storing each nonzero value alongside its explicit (row, column) coordinates — the simplest sparse format, and the easiest one to construct incrementally.

Syntax

scipy.sparse.coo_matrix((data, (row, col)), shape=None)

Deep Dive Course

COO format stores three parallel arrays: the nonzero data values, and their corresponding row and column indices, with no particular ordering requirement and no restriction against duplicate (row, col) entries, which are automatically summed together when the matrix is used or converted. This makes COO the easiest sparse format to build up incrementally, especially when adding entries in an arbitrary, non-sorted order, but it doesn't support efficient direct arithmetic or slicing the way CSR/CSC do, which is why COO matrices are typically converted to CSR or CSC before being used in actual computations.

1Understanding sparse.coo_matrix()

COO format stores three parallel arrays: the nonzero data values, and their corresponding row and column indices, with no particular ordering requirement and no restriction against duplicate (row, col) entries, which are automatically summed together when the matrix is used or converted. This makes COO the easiest sparse format to build up incrementally, especially when adding entries in an arbitrary, non-sorted order, but it doesn't support efficient direct arithmetic or slicing the way CSR/CSC do, which is why COO matrices are typically converted to CSR or CSC before being used in actual computations.

💡

Build a sparse matrix in COO format when constructing it from scratch, especially from separate lists of row indices, column indices, and values, then convert it to CSR or CSC with .tocsr()/.tocsc() before doing any actual arithmetic or slicing with it.

editor.html
from scipy import sparse

row = [0, 1, 2]
col = [2, 0, 1]
data = [3, 4, 5]
matrix = sparse.coo_matrix((data, (row, col)), shape=(3, 3))
print(matrix.toarray())
localhost:3000

2Practical Example

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

editor.html
from scipy import sparse

row = [0, 0, 1]
col = [1, 1, 0]
data = [2, 3, 4]
matrix = sparse.coo_matrix((data, (row, col)), shape=(2, 2))
print(matrix.toarray())
localhost:3000

3Best Practices

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

1. Use COO format specifically for initial construction of a sparse matrix, especially from three parallel arrays of row indices, column indices, and values

2. Convert a COO matrix to CSR or CSC with .tocsr()/.tocsc() before performing arithmetic or slicing, since COO itself doesn't support those operations efficiently

3. Take advantage of COO's automatic summing of duplicate (row, col) entries when building a matrix that naturally accumulates values at the same position, like a co-occurrence count matrix

⚠️

Tip: Build a sparse matrix in COO format when constructing it from scratch, especially from separate lists of row indices, column indices, and values, then convert it to CSR or CSC with .tocsr()/.tocsc() before doing any actual arithmetic or slicing with it.

editor.html
from scipy import sparse

row = [0, 1, 2]
col = [2, 0, 1]
data = [3, 4, 5]
matrix = sparse.coo_matrix((data, (row, col)), shape=(3, 3))
print(matrix.toarray())
localhost:3000

Examples

Example 01Basic Usage
from scipy import sparse

row = [0, 1, 2]
col = [2, 0, 1]
data = [3, 4, 5]
matrix = sparse.coo_matrix((data, (row, col)), shape=(3, 3))
print(matrix.toarray())
Example 02Advanced Example
from scipy import sparse

row = [0, 0, 1]
col = [1, 1, 0]
data = [2, 3, 4]
matrix = sparse.coo_matrix((data, (row, col)), shape=(2, 2))
print(matrix.toarray())

Best Practices

  • Use COO format specifically for initial construction of a sparse matrix, especially from three parallel arrays of row indices, column indices, and values
  • Convert a COO matrix to CSR or CSC with .tocsr()/.tocsc() before performing arithmetic or slicing, since COO itself doesn't support those operations efficiently
  • Take advantage of COO's automatic summing of duplicate (row, col) entries when building a matrix that naturally accumulates values at the same position, like a co-occurrence count matrix

Interview Question

What happens if you construct a COO matrix with the same (row, col) coordinate listed twice with different values?

Hint: Think about how COO format handles duplicate coordinate entries when the matrix is actually used.

COO format explicitly allows duplicate coordinate entries during construction — it doesn't check for or reject them — and when the matrix is converted to another format or used in a computation, all the values listed at the same (row, col) coordinate are automatically summed together into a single combined value at that position. This is actually a useful, intentional feature for building matrices that naturally accumulate contributions at the same position, like counting how many times a particular pair of items co-occurs, rather than being treated as an error condition.

Exercises

MediumPractice using sparse.coo_matrix() in a real scenario.
View Solution
from scipy import sparse

row = [0, 1, 2]
col = [2, 0, 1]
data = [3, 4, 5]
matrix = sparse.coo_matrix((data, (row, col)), shape=(3, 3))
print(matrix.toarray())

Frequently Asked Questions

What happens if you construct a COO matrix with the same (row, col) coordinate listed twice with different values?

COO format explicitly allows duplicate coordinate entries during construction — it doesn't check for or reject them — and when the matrix is converted to another format or used in a computation, all the values listed at the same (row, col) coordinate are automatically summed together into a single combined value at that position. This is actually a useful, intentional feature for building matrices that naturally accumulate contributions at the same position, like counting how many times a particular pair of items co-occurs, rather than being treated as an error condition.

Related Functions

sparse-csr-matrixsparse-csc-matrixnp-array