Sparse n-dimensional arrays

0.18.0 · active · verified Sat Apr 11

Sparse is a Python library that provides n-dimensional arrays for the PyData ecosystem, optimized for data with a large number of zero or 'fill' values. It aims to offer a drop-in replacement for NumPy arrays with support for N-dimensional operations, following the Array API standard. The current version is 0.18.0, and it maintains an active development cycle with frequent releases.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create sparse arrays using the `COO` format from a dictionary of coordinates and values, or by converting a dense NumPy array. It also shows a basic arithmetic operation and how to convert a sparse array back to a dense NumPy array.

import sparse
import numpy as np

# Create a sparse array from a dictionary of coordinates and values
x = sparse.COO({(0, 0): 1, (1, 2): 2}, shape=(3, 3))
print("Sparse array x:\n", x)
print("Dense representation of x:\n", x.todense())

# Create a sparse array from a NumPy array
y = np.arange(9).reshape((3, 3))
z = sparse.COO.from_numpy(y)
print("Sparse array z from NumPy:\n", z)

# Perform an operation (addition) and convert to dense
print("Dense representation of (x + z):\n", (x + z).todense())

view raw JSON →