lenskit.data.Vocabulary#
- class lenskit.data.Vocabulary(keys=None, name=None, *, reorder=True)#
Bases:
object
Vocabularies of entity identifiers for the LensKit data model.
This class supports bidirectional mappings between key-like data and congiguous nonnegative integer indices. Its key use is to facilitate the entitie ID vocabularies in
Dataset
, but it can also be used for things like item tags.IDs in a vocabulary must be unique. Constructing a vocabulary with
reorder=True
ensures uniqueness (and sorts the IDs), but does not preserve the order of IDs in the original input.It is currently a wrapper around
pandas.Index
, but this fact is not part of the stable public API.- Parameters:
- Stability:
- Caller (see Stability Levels).
- __init__(keys=None, name=None, *, reorder=True)#
Methods
__init__
([keys, name, reorder])id
(num)Alias for
term()
for greater readability for entity ID vocabularies.ids
([nums])Alias for
terms()
for greater readability for entity ID vocabularies.number
()Look up the number for a vocabulary term.
numbers
(terms[, missing])Look up the numbers for an array of terms or IDs.
term
(num)Look up the term with a particular number.
terms
([nums])Get a list of terms, optionally for an array of term numbers.
Attributes
The property as a Pandas index.
Current vocabulary size.
The name of the vocabulary (e.g. “user”, “item”).
- number(term: object, missing: Literal['error'] = 'error') int #
- number(term: object, missing: Literal['none'] | None) int | None
Look up the number for a vocabulary term.
- numbers(terms, missing='error')#
Look up the numbers for an array of terms or IDs.
- term(num)#
Look up the term with a particular number. Negative indexing is not supported.
- terms(nums=None)#
Get a list of terms, optionally for an array of term numbers.
- Parameters:
nums (list[int] | ndarray[Any, dtype[integer]] | Series | None) – The numbers (indices) for of terms to retrieve. If
None
, returns all terms.- Returns:
The terms corresponding to the specified numbers, or the full array of terms (in order) if
nums=None
.- Return type:
ndarray[tuple[int], dtype[integer[Any] | str_ | bytes_ | object_]]