ItemToItemAnnRecommender

class rectools.tools.ann.ItemToItemAnnRecommender(item_vectors: np.ndarray, item_id_map: tp.Union[IdMap, tp.Dict[ExternalId, InternalId]], index_top_k: int = 0, index_init_params: tp.Optional[tp.Dict[str, str]] = None, index_query_time_params: tp.Optional[tp.Dict[str, int]] = None, create_index_params: tp.Optional[tp.Dict[str, int]] = None, index: tp.Optional[nmslib.FloatIndex] = None)[source]

Bases: BaseNmslibRecommender

Class implements item-to-item ANN recommender.

Parameters
  • item_vectors (ndarray) – Ndarray of item latent features of size (N, K), where N is the number of items and K is the number of features.

  • item_id_map (dict(hashable, int) | rectools.datasets.IdMap) – Mappings from external item ids to internal item ids used by recommender. Values must be positive integers.

  • index_top_k (int, default 0) – Number of items to return per knn query (in addition to top_n passed to get_item_list_for_user, get_item_list_for_user_batch, get_item_list_for_item or get_item_list_for_item_batch). In this case nmslib index query. This might be important in order to account for filters. See self.index.knnQueryBatch in `_compute_sorted_similar’

  • index_init_params (optional(dict(str, str)) | rectools.dataset.IdMap) – NMSLIB initialization parameters. See nmslib documentation. In case of None defaults to reasonable parameters.

  • index_query_time_params (optional(dict(str, int)) | rectools.dataset.IdMap) – NMSLIB query time parameters. See nmslib documentation. In case of None defaults to reasonable parameters.

  • create_index_params (optional(dict(str, int)) | rectools.dataset.IdMap) – NMSLIB index creation parameters. See nmslib documentation. In case of None defaults to reasonable parameters.

  • index (FloatIndex, optional) – Optional instance of FloatIndex. Exists for outside initialization.

get_item_list_for_item()[source]

Part of public API. Given item id and available item ids, calculates recommendations via index query.

Parameters
  • item_id (Hashable) –

  • top_n (int) –

  • item_available_ids (Optional[Union[Sequence[Hashable], ndarray]]) –

Return type

Union[Sequence[Hashable], ndarray]

get_item_list_for_item_batch()[source]

Part of public API. Does exactly what get_item_list_for_item, but for a batch of item ids and available item ids.

Parameters
  • item_ids (Union[Sequence[Hashable], ndarray]) –

  • top_n (int) –

  • item_available_ids (Optional[Sequence[Union[Sequence[Hashable], ndarray]]]) –

Return type

Sequence[Union[Sequence[Hashable], ndarray]]

Inherited-members

Parameters
  • item_vectors (np.ndarray) –

  • item_id_map (tp.Union[IdMap, tp.Dict[ExternalId, InternalId]]) –

  • index_top_k (int) –

  • index_init_params (tp.Optional[tp.Dict[str, str]]) –

  • index_query_time_params (tp.Optional[tp.Dict[str, int]]) –

  • create_index_params (tp.Optional[tp.Dict[str, int]]) –

  • index (tp.Optional[nmslib.FloatIndex]) –

Methods

fit([verbose])

Create and fit nmslib index.

get_item_list_for_item(item_id, top_n[, ...])

Calculate top-n recommendations for a given item id and item list.

get_item_list_for_item_batch(item_ids, top_n)

Calculate top-n recommendations for given item ids and item lists.

get_item_list_for_item(item_id: Hashable, top_n: int, item_available_ids: Optional[Union[Sequence[Hashable], ndarray]] = None) Union[Sequence[Hashable], ndarray][source]

Calculate top-n recommendations for a given item id and item list. Item list defines which items are allowed to be recommended.

Parameters
  • item_id (hashable) – Item id used by external systems.

  • top_n (int) – How many items to return.

  • item_available_ids (optional(sequence(hashable)), default None) – List of allowed items. In case of None this function recommends without constraints

Returns

Sorted sequence of external ids.

Return type

sequence(hashable)

get_item_list_for_item_batch(item_ids: Union[Sequence[Hashable], ndarray], top_n: int, item_available_ids: Optional[Sequence[Union[Sequence[Hashable], ndarray]]] = None) Sequence[Union[Sequence[Hashable], ndarray]][source]

Calculate top-n recommendations for given item ids and item lists. Item lists define which items are allowed to be recommended.

Parameters
  • item_ids (sequence(hashable)) – List of user ids used by external systems.

  • top_n (int) – How many items to return.

  • item_available_ids (optional(sequence(sequence(hashable))), default None) – List of lists of allowed items for each item id from item_ids in that exact order. In case of None this function recommends without constraints.

Returns

Sequence of sorted sequences of external ids.

Return type

sequence(sequence(hashable))