CandidateGenerator

class rectools.models.ranking.candidate_ranking.CandidateGenerator(model: ModelBase, num_candidates: int, keep_ranks: bool, keep_scores: bool, scores_fillna_value: Optional[float] = None, ranks_fillna_value: Optional[float] = None)[source]

Bases: object

A class responsible for generating recommendation candidates using a specified model. The generator can be configured to retain or discard ranks and scores, and it supports both training and recommendation modes.

Inherited-members

Parameters
  • model (ModelBase) –

  • num_candidates (int) –

  • keep_ranks (bool) –

  • keep_scores (bool) –

  • scores_fillna_value (Optional[float]) –

  • ranks_fillna_value (Optional[float]) –

Methods

fit(dataset, for_train)

Fit the model using the provided dataset, configuring the generator for either training or recommendation.

generate_candidates(users, dataset, ...[, ...])

Generate candidates for recommendations.

fit(dataset: Dataset, for_train: bool) None[source]

Fit the model using the provided dataset, configuring the generator for either training or recommendation.

Parameters
  • dataset (Dataset) – The dataset to fit the model with. This should contain the necessary data for training or recommending.

  • for_train (bool) – If True, configure the generator for training; otherwise, configure it for recommendation.

Return type

None

generate_candidates(users: Union[Sequence[Hashable], ndarray], dataset: Dataset, filter_viewed: bool, for_train: bool, items_to_recommend: Optional[Union[Sequence[Hashable], ndarray]] = None, on_unsupported_targets: Literal['ignore', 'warn', 'raise'] = 'raise') DataFrame[source]

Generate candidates for recommendations.

Parameters
  • users (ExternalIds) – The users for whom to generate recommendation candidates.

  • dataset (Dataset) – The dataset containing user-item interactions and additional data needed for recommendation.

  • filter_viewed (bool) – Whether to filter out items that have already been viewed by the user.

  • for_train (bool) – Whether the candidates are being generated for training purposes.

  • items_to_recommend (ExternalIds, optional, default None) – Specific items to recommend. If None, recommend from all available items.

  • on_unsupported_targets (ErrorBehaviour, default "raise") – Behavior when encountering unsupported targets. Can be “raise” to raise an error.

Returns

A DataFrame containing the generated recommendation candidates.

Return type

pd.DataFrame