ModelBase

class rectools.models.base.ModelBase(*args: Any, verbose: int = 0, **kwargs: Any)[source]

Bases: object

Base model class.

Warning: This class should not be used directly. Use derived classes instead.

Inherited-members

Parameters
  • args (Any) –

  • verbose (int) –

  • kwargs (Any) –

Methods

fit(dataset, *args, **kwargs)

Fit model.

recommend(users, dataset, k, filter_viewed)

Recommend items for users.

recommend_to_items(target_items, dataset, k)

Recommend items for target items.

Attributes

recommends_for_cold

recommends_for_warm

fit(dataset: Dataset, *args: Any, **kwargs: Any) T[source]

Fit model.

Parameters
  • dataset (Dataset) – Dataset with input data.

  • self (T) –

  • args (Any) –

  • kwargs (Any) –

Return type

self

recommend(users: Union[Sequence[Hashable], ndarray, Sequence[int]], dataset: Dataset, k: int, filter_viewed: bool, items_to_recommend: Optional[Union[Sequence[Hashable], ndarray, Sequence[int]]] = None, add_rank_col: bool = True, assume_external_ids: bool = True) DataFrame[source]

Recommend items for users.

To use this method model must be fitted.

Parameters
  • users (array-like) – Array of user ids to recommend for. User ids are supposed to be external if assume_external_ids is True (default). Internal otherwise.

  • dataset (Dataset) – Dataset with input data. Usually it’s the same dataset that was used to fit model.

  • k (int) – Derived number of recommendations for every user. Pay attention that in some cases real number of recommendations may be less than k.

  • filter_viewed (bool) – Whether to filter from recommendations items that user has already interacted with. Works only for “hot” users.

  • items_to_recommend (array-like, optional, default None) – Whitelist of item ids. If given, only these items will be used for recommendations. Otherwise all items from dataset will be used. Item ids are supposed to be external if assume_external_ids is True` (default). Internal otherwise.

  • add_rank_col (bool, default True) – Whether to add rank column to recommendations. If True column Columns.Rank will be added. This column contain integers from 1 to number of user recommendations. In any case recommendations are sorted per rank for every user. The lesser the rank the more recommendation is relevant.

  • assume_external_ids (bool, default True) – When True all input user and item ids are supposed to be external. Ids in returning recommendations table will be external as well. Internal otherwise. Works faster with False.

Returns

Recommendations table with columns Columns.User, Columns.Item, Columns.Score`[, `Columns.Rank]. External user and item ids are used by default. For internal ids set return_external_ids to False. 1st column contains user ids, 2nd - ids of recommended items sorted by relevance for each user, 3rd - score that model gives for the user-item pair, 4th (present only if add_rank_col is True) - integers from 1 to number of user recommendations.

Return type

pd.DataFrame

Raises
  • NotFittedError – If called for not fitted model.

  • TypeError, ValueError – If arguments have inappropriate type or value

  • ValueError – If some of given users are warm/cold and model doesn’t support such type of users.

recommend_to_items(target_items: Union[Sequence[Hashable], ndarray, Sequence[int]], dataset: Dataset, k: int, filter_itself: bool = True, items_to_recommend: Optional[Union[Sequence[Hashable], ndarray, Sequence[int]]] = None, add_rank_col: bool = True, assume_external_ids: bool = True) DataFrame[source]

Recommend items for target items.

To use this method model must be fitted.

Parameters
  • target_items (array-like) – Array of item ids to recommend for. Item ids are supposed to be external if assume_external_ids is True` (default). Internal otherwise.

  • dataset (Dataset) – Dataset with input data. Usually it’s the same dataset that was used to fit model.

  • k (int) – Derived number of recommendations for every target item. Pay attention that in some cases real number of recommendations may be less than k.

  • filter_itself (bool, default True) – If True, item will be excluded from recommendations to itself.

  • items_to_recommend (array-like, optional, default None) – Whitelist of item ids. If given, only these items will be used for recommendations. Otherwise all items from dataset will be used. Item ids are supposed to be external if assume_external_ids is True` (default). Internal otherwise.

  • add_rank_col (bool, default True) – Whether to add rank column to recommendations. If True column Columns.Rank will be added. This column contain integers from 1 to number of item recommendations. In any case recommendations are sorted per rank for every target item. Less rank means more relevant recommendation.

  • assume_external_ids (bool, default True) – When True all input item ids are supposed to be external. Ids in returning recommendations table will be external as well. Internal otherwise. Works faster with False.

Returns

Recommendations table with columns Columns.TargetItem, Columns.Item, Columns.Score`[, `Columns.Rank]. External item ids are used by default. For internal ids set return_external_ids to False. 1st column contains target item ids, 2nd - ids of recommended items sorted by relevance for each target item, 3rd - score that model gives for the target-item pair, 4th (present only if add_rank_col is True) - integers from 1 to number of recommendations.

Return type

pd.DataFrame

Raises
  • NotFittedError – If called for not fitted model.

  • TypeError, ValueError – If arguments have inappropriate type or value

  • KeyError – If some of given target items are not in dataset.item_id_map