pyarrow.SparseCSRMatrix#

class pyarrow.SparseCSRMatrix#

Bases: _Weakrefable

A sparse CSR (Compressed Sparse Row) matrix.

CSR format stores a sparse matrix by compressing the row information. It uses three arrays: data (non-zero values), indices (column indices), and indptr (row pointers that indicate where each row starts in the data array).

Examples

>>> import pyarrow as pa
>>> import numpy as np
>>> dense_matrix = np.array([[1, 0, 2], [0, 0, 3]], dtype=np.float64)
>>> sparse_csr = pa.SparseCSRMatrix.from_dense_numpy(dense_matrix)
>>> sparse_csr
<pyarrow.SparseCSRMatrix>
type: double
shape: (2, 3)
__init__(*args, **kwargs)#

Methods

__init__(*args, **kwargs)

dim_name(self, i)

Returns the name of the i-th tensor dimension.

equals(self, SparseCSRMatrix other)

Return true if sparse tensors contains exactly equal data.

from_dense_numpy(cls, obj[, dim_names])

Convert numpy.ndarray to arrow::SparseCSRMatrix

from_numpy(data, indptr, indices, shape[, ...])

Create arrow::SparseCSRMatrix from numpy.ndarrays.

from_scipy(obj[, dim_names])

Convert scipy.sparse.csr_array or scipy.sparse.csr_matrix to arrow::SparseCSRMatrix.

from_tensor(obj)

Convert arrow::Tensor to arrow::SparseCSRMatrix.

to_numpy(self)

Convert arrow::SparseCSRMatrix to numpy.ndarrays with zero copy.

to_scipy(self)

Convert arrow::SparseCSRMatrix to scipy.sparse.csr_array.

to_tensor(self)

Convert arrow::SparseCSRMatrix to arrow::Tensor.

Attributes

dim_name(self, i)#

Returns the name of the i-th tensor dimension.

Parameters:
iint

The physical index of the tensor dimension.

Returns:
str
dim_names#
equals(self, SparseCSRMatrix other)#

Return true if sparse tensors contains exactly equal data.

Parameters:
otherSparseCSRMatrix

The other tensor to compare for equality.

classmethod from_dense_numpy(cls, obj, dim_names=None)#

Convert numpy.ndarray to arrow::SparseCSRMatrix

Parameters:
objnumpy.ndarray

The dense numpy array that should be converted.

dim_nameslist, optional

The names of the dimensions.

Returns:
pyarrow.SparseCSRMatrix
static from_numpy(data, indptr, indices, shape, dim_names=None)#

Create arrow::SparseCSRMatrix from numpy.ndarrays.

Parameters:
datanumpy.ndarray

Data used to populate the sparse matrix.

indptrnumpy.ndarray

Range of the rows, The i-th row spans from indptr[i] to indptr[i+1] in the data.

indicesnumpy.ndarray

Column indices of the corresponding non-zero values.

shapetuple

Shape of the matrix.

dim_nameslist, optional

Names of the dimensions.

static from_scipy(obj, dim_names=None)#

Convert scipy.sparse.csr_array or scipy.sparse.csr_matrix to arrow::SparseCSRMatrix.

Parameters:
objscipy.sparse.csr_array or scipy.sparse.csr_matrix

The scipy matrix that should be converted.

dim_nameslist, optional

Names of the dimensions.

static from_tensor(obj)#

Convert arrow::Tensor to arrow::SparseCSRMatrix.

Parameters:
objTensor

The dense tensor that should be converted.

is_mutable#
ndim#
non_zero_length#
shape#
size#
to_numpy(self)#

Convert arrow::SparseCSRMatrix to numpy.ndarrays with zero copy.

to_scipy(self)#

Convert arrow::SparseCSRMatrix to scipy.sparse.csr_array.

to_tensor(self)#

Convert arrow::SparseCSRMatrix to arrow::Tensor.

type#