Reranker

class rectools.models.ranking.candidate_ranking.Reranker(model: Union[ClassifierBase, RankerBase], fit_kwargs: Optional[Dict[str, Any]] = None)[source]

Bases: object

A class used to re-rank candidates from first stage using ranking model. The model can be either a classifier or a ranker.

Inherited-members

Parameters

Methods

fit(candidates_with_target)

Fit the model using the provided candidates with target labels.

predict_scores(candidates)

Predict scores for the provided candidates using the fitted model.

prepare_fit_kwargs(candidates_with_target)

Prepare the keyword arguments for fitting the model, based on the provided candidates with targets.

recommend(scored_pairs, k[, add_rank_col])

Generate top-k recommendations for each user based on the provided scores.

fit(candidates_with_target: DataFrame) None[source]

Fit the model using the provided candidates with target labels.

Parameters

candidates_with_target (pd.DataFrame) – A DataFrame containing the features and target labels for the candidates.

Return type

None

predict_scores(candidates: DataFrame) ndarray[source]

Predict scores for the provided candidates using the fitted model.

Parameters

candidates (pd.DataFrame) – A DataFrame containing the features for the candidates.

Returns

An array containing the predicted scores for each candidate. If the model is a classifier, the scores represent probabilities for the positive class.

Return type

np.ndarray

prepare_fit_kwargs(candidates_with_target: DataFrame) Dict[str, Any][source]

Prepare the keyword arguments for fitting the model, based on the provided candidates with targets.

Parameters

candidates_with_target (pd.DataFrame) – A DataFrame containing the features and target labels for the candidates.

Returns

A dictionary containing the features (X) and target labels (y) for fitting the model.

Return type

dict(str -> any)

classmethod recommend(scored_pairs: DataFrame, k: int, add_rank_col: bool = True) DataFrame[source]

Generate top-k recommendations for each user based on the provided scores.

Parameters
  • scored_pairs (pd.DataFrame) – A DataFrame containing user-item pairs with associated scores. The DataFrame must have columns Columns.User and Columns.Score.

  • k (int) – The number of top items to recommend for each user.

  • add_rank_col (bool, default True) – Whether to add a rank column to the resulting DataFrame, indicating the rank of each item within the user’s recommendations.

Returns

A DataFrame containing the top-k recommended items for each user. If add_rank_col is True, the DataFrame will include an additional column Columns.Rank for the rank of each item.

Return type

pd.DataFrame