pyarrow.SparseCSFTensor#
- class pyarrow.SparseCSFTensor#
Bases:
_WeakrefableA sparse CSF tensor.
CSF is a generalization of compressed sparse row (CSR) index.
CSF index recursively compresses each dimension of a tensor into a set of prefix trees. Each path from a root to leaf forms one tensor non-zero index. CSF is implemented with two arrays of buffers and one arrays of integers.
Examples
>>> import pyarrow as pa >>> import numpy as np >>> # Create a 3D sparse tensor >>> dense_tensor = np.zeros((2, 3, 2), dtype=np.float32) >>> dense_tensor[0, 1, 0] = 1.0 >>> dense_tensor[1, 2, 1] = 2.0 >>> sparse_csf = pa.SparseCSFTensor.from_dense_numpy(dense_tensor) >>> sparse_csf <pyarrow.SparseCSFTensor> type: float shape: (2, 3, 2)
- __init__(*args, **kwargs)#
Methods
__init__(*args, **kwargs)dim_name(self, i)Returns the name of the i-th tensor dimension.
equals(self, SparseCSFTensor other)Return true if sparse tensors contains exactly equal data
from_dense_numpy(cls, obj[, dim_names])Convert numpy.ndarray to arrow::SparseCSFTensor
from_numpy(data, indptr, indices, shape[, ...])Create arrow::SparseCSFTensor from numpy.ndarrays
from_tensor(obj)Convert arrow::Tensor to arrow::SparseCSFTensor
to_numpy(self)Convert arrow::SparseCSFTensor to numpy.ndarrays with zero copy
to_tensor(self)Convert arrow::SparseCSFTensor to arrow::Tensor
Attributes
- dim_name(self, i)#
Returns the name of the i-th tensor dimension.
- dim_names#
- equals(self, SparseCSFTensor other)#
Return true if sparse tensors contains exactly equal data
- Parameters:
- other
SparseCSFTensor The other tensor to compare for equality.
- other
- classmethod from_dense_numpy(cls, obj, dim_names=None)#
Convert numpy.ndarray to arrow::SparseCSFTensor
- Parameters:
- obj
numpy.ndarray Data used to populate the rows.
- dim_names
list[str], optional Names of the dimensions.
- obj
- Returns:
- static from_numpy(data, indptr, indices, shape, axis_order=None, dim_names=None)#
Create arrow::SparseCSFTensor from numpy.ndarrays
- Parameters:
- data
numpy.ndarray Data used to populate the sparse tensor.
- indptr
numpy.ndarray The sparsity structure. Each two consecutive dimensions in a tensor correspond to a buffer in indices. A pair of consecutive values at indptr[dim][i] indptr[dim][i + 1] signify a range of nodes in indices[dim + 1] who are children of indices[dim][i] node.
- indices
numpy.ndarray Stores values of nodes. Each tensor dimension corresponds to a buffer in indptr.
- shape
tuple Shape of the matrix.
- axis_order
list, optional the sequence in which dimensions were traversed to produce the prefix tree.
- dim_names
list, optional Names of the dimensions.
- data
- static from_tensor(obj)#
Convert arrow::Tensor to arrow::SparseCSFTensor
- Parameters:
- obj
Tensor The dense tensor that should be converted.
- obj
- is_mutable#
- ndim#
- non_zero_length#
- shape#
- size#
- to_numpy(self)#
Convert arrow::SparseCSFTensor to numpy.ndarrays with zero copy
- to_tensor(self)#
Convert arrow::SparseCSFTensor to arrow::Tensor
- type#