Training the CatBoost ranker

CatBoost is the workhorse for tabular ranking on catalogues like this. It handles categorical columns natively, trains fast on CPU, and gives you feature importance for free. We wrap the model behind a thin factory and trainer so the script reads top to bottom.

recsys/training/ranking.py
python
class RankingModelFactory:
    @classmethod
    def build(cls):
        return CatBoostClassifier(
            learning_rate=settings.RANKING_LEARNING_RATE,
            iterations=settings.RANKING_ITERATIONS,
            depth=10,
            scale_pos_weight=settings.RANKING_SCALE_POS_WEIGHT,
            early_stopping_rounds=settings.RANKING_EARLY_STOPPING_ROUNDS,
            use_best_model=True,
        )

class RankingModelTrainer:
    def fit(self):
        self._model.fit(self._train_dataset, eval_set=self._eval_dataset)
        return self._model

    def evaluate(self, log=False):
        preds = self._model.predict(self._eval_dataset)
        precision, recall, fscore, _ = precision_recall_fscore_support(self._y_val, preds, average='binary')
        return {'precision': precision, 'recall': recall, 'fscore': fscore}

A CatBoost classifier with the typical retail ranker hyperparameters.

terminal
bash
make train-ranking

Run it.

Read it as a sanity check first. If the top feature is age and the bottom is garment_group_name, you have a problem. If the top features are the ones a fashion buyer would expect (product_type_name, index_group_name, garment_group_name), the model has learned something defensible. If a feature you cannot explain dominates, dig in before trusting the model.

Ordering exercise: Put the ranking workflow in order

Loading practice…