lenskit.data.MatrixRelationshipSet#

class lenskit.data.MatrixRelationshipSet(ds, name, schema, table)#

Bases: RelationshipSet

Two-entity relationships without duplicates, accessible in matrix form.

Parameters:
__init__(ds, name, schema, table)#
Parameters:

Methods

__init__(ds, name, schema, table)

arrow(*[, attributes, ids])

Get these relationships and their attributes as a PyArrow table.

col_stats()

count()

csr_structure()

Get the compressed sparse row structure of this relationship matrix.

matrix(*[, combine])

Convert this relationship set into a matrix, coalescing duplicate observations.

pandas(*[, attributes, ids])

Get these relationship and their attributes as a PyArrow table.

row_items([id, number])

Get a single row of this interaction matrix as an item list. Only valid when the column entity class is ``item''.

row_stats()

row_table([id, number])

Get a single row of this interaction matrix as a table.

scipy([attribute, layout, legacy])

Get this relationship matrix as a SciPy sparse matrix.

torch([attribute, layout])

Get this relationship matrix as a PyTorch sparse tensor.

Attributes

attribute_names

is_interaction

Query whether these relationships represent interactions.

row_vocabulary

row_type

col_vocabulary

col_type

name

The name of the relationship class for these relationships.

schema

matrix(*, combine=None)#

Convert this relationship set into a matrix, coalescing duplicate observations.

Parameters:

combine (Literal['count', 'sum', 'mean', 'first', 'last'] | dict[str, ~typing.Literal['count', 'sum', 'mean', 'first', 'last']] | None) – The method for combining attribute values for repeated relationships or interactions. Can either be a single strategy or a mapping from attribute names to combination strategies.

Return type:

MatrixRelationshipSet

csr_structure()#

Get the compressed sparse row structure of this relationship matrix.

Return type:

CSRStructure

scipy(attribute=None, *, layout='csr', legacy=False)#

Get this relationship matrix as a SciPy sparse matrix.

Parameters:
  • attribute (str | None) – The attribute to return, or None to return an indicator-only sparse matrix (all observed values are 1).

  • layout (Literal['csr', 'coo']) – The matrix layout to return.

  • legacy (bool)

Returns:

The sparse matrix.

Return type:

sparray | spmatrix

torch(attribute=None, *, layout='csr')#

Get this relationship matrix as a PyTorch sparse tensor.

Parameters:
  • attribute (str | None) – The attribute to return, or None to return an indicator-only sparse matrix (all observed values are 1).

  • layout (Literal['csr', 'coo']) – The matrix layout to return.

Returns:

The sparse matrix.

Return type:

Tensor

row_table(id=None, *, number=None)#

Get a single row of this interaction matrix as a table.

Parameters:
Return type:

Table | None

row_items(id=None, *, number=None)#

Get a single row of this interaction matrix as an item list. Only valid when the column entity class is ``item’’.

Parameters:
Return type:

ItemList | None