lenskit.data.MatrixRelationshipSet#

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

Bases: RelationshipSet

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

Note

Client code does not need to construct this class; obtain instances from a relationship set’s matrix() method.

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()

coo_structure()

Get the compressed sparse row structure of this relationship matrix.

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.

sample_negatives(rows, *[, weighting, ...])

Sample negative columns (columns with no observation recorded) for an array of rows.

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.

n_cols

n_rows

row_vocabulary

row_type

col_vocabulary

col_type

rc_index

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

coo_structure()#

Get the compressed sparse row structure of this relationship matrix.

Return type:

COOStructure

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

sample_negatives(rows, *, weighting='uniform', verify=True, max_attempts=10, rng=None)#

Sample negative columns (columns with no observation recorded) for an array of rows. On a normal interaction matrix, this samples negative items for users.

Parameters:
  • rows (ndarray[int, dtype[int32]]) – The row numbers. Duplicates are allowed, and negative columns are sampled independently for each row. Must be a 1D array or tensor.

  • weighting (Literal['uniform', 'popularity']) – The weighting for sampled negatives; uniform samples them uniformly at random, while popularity samples them proportional to their popularity (number of occurrences).

  • verify (bool) – Whether to verify that the negative items are actually negative. Unverified sampling is much faster but can return false negatives.

  • max_attempts (int) – When verification is on, the maximum attempts before giving up and returning a possible false negative.

  • rng (Generator | None) – A random number generator to use.

Return type:

ndarray[Any, dtype[int32]]

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