lenskit.data.matrix.SparseRowArray#

class lenskit.data.matrix.SparseRowArray#

Bases: pyarrow.ExtensionArray

An array of sparse rows (a compressed sparse row matrix).

Stability: Internal

This API is at the internal or experimental stability level: it may change at any time, and breaking changes will not necessarily be described in the release notes. See Stability Levels for details.

type: SparseRowType | SparseIndexListType#
classmethod from_arrays(offsets, indices, values=None, *, shape=None)#
Parameters:
  • offsets (numpy.typing.ArrayLike)

  • indices (numpy.typing.ArrayLike)

  • values (numpy.typing.ArrayLike | None)

  • shape (tuple[int, int] | None)

Return type:

SparseRowArray

classmethod from_array(array, dimension=None)#

Interpret an Arrow array as a sparse row array, if possible. Handles legacy layouts without the extension types.

Parameters:
  • array (pyarrow.Array) – The array to convert.

  • dimension (int | None) – The dimensionality of the sparse rows, if known from an external source.

Return type:

SparseRowArray

classmethod from_scipy(matrix, *, values=True, large=None)#

Create a sparse row array from a SciPy sparse matrix.

Parameters:
  • csr – The SciPy sparse matrix (in CSR format).

  • values (bool) – Whether to include the values or create a structure-only array.

  • large (bool | None) – True to force creation of a pa.LargeListArray.

  • matrix (scipy.sparse.sparray)

Returns:

The sparse row array.

Return type:

SparseRowArray

to_scipy()#

Convert this sparse row array to a SciPy sparse array.

Return type:

scipy.sparse.csr_array[Any, tuple[int, int]]

to_torch()#

Convert this sparse row array to a Torch sparse tensor.

Return type:

torch.Tensor

to_coo()#

Convert this array to table representing the array in COO format.

Return type:

pyarrow.Table

property dimension: int#

Get the number of columns in the sparse matrix.

Return type:

int

property shape: tuple[int, int]#
Return type:

tuple[int, int]

property has_values: bool#
Return type:

bool

property offsets: pyarrow.Int32Array#
Return type:

pyarrow.Int32Array

property indices: pyarrow.Int32Array#
Return type:

pyarrow.Int32Array

property values: pyarrow.Array | None#
Return type:

pyarrow.Array | None

property nnz: int#
Return type:

int

structure()#

Get the structure of this matrix (without values).

Return type:

SparseRowArray

transpose()#

Get the transpose of this sparse matrix.

Return type:

SparseRowArray

row_extent(row)#

Get the start and end of a row.

Parameters:

row (int)

Return type:

tuple[int, int]

row_indices(row)#

Get the index array for a compressed sparse row.

Parameters:

row (int)

Return type:

pyarrow.Int32Array