XGBoosting Home | About | Contact | Examples

Configure XGBoost "rank:pairwise" Objective

XGBoost’s “rank:pairwise” objective is a powerful tool for tackling learning to rank problems, where the goal is to optimize the ordering of a list of items.

This objective transforms the ranking task into a pairwise classification problem, learning to predict which item in a pair should be ranked higher.

It’s particularly useful in applications like search engines, recommender systems, and ad ranking, where presenting results in the most relevant order is crucial.

This example demonstrates how to configure XGBoost with the “rank:pairwise” objective to train a model on a synthetic dataset for a ranking task.

import numpy as np
import xgboost as xgb
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split

# Generate a synthetic dataset for ranking
X, y = make_classification(n_samples=1000, n_classes=10, n_informative=5, n_clusters_per_class=1, random_state=42)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Convert data into DMatrix format
dtrain = xgb.DMatrix(X_train, label=y_train)
dtest = xgb.DMatrix(X_test, label=y_test)

# Define XGBoost parameters with "rank:pairwise" objective
params = {
    'objective': 'rank:pairwise',
    'eval_metric': 'map',
    'eta': 0.1,
    'max_depth': 6,
    'min_child_weight': 1,
    'lambda': 1

# Train the model
bst = xgb.train(params, dtrain, num_boost_round=100)

# Make predictions on the test set
preds = bst.predict(dtest)
print("Predicted rankings:", preds)

When using the “rank:pairwise” objective, it’s important to keep in mind that XGBoost transforms the ranking problem into a pairwise classification task under the hood.

This means that for each pair of items (i, j), the model learns to predict whether i should be ranked higher than j. Constructing meaningful item pairs is therefore crucial for effective learning.

To optimize a model using the “rank:pairwise” objective, consider tuning the following key parameters:

When evaluating your ranking model, be sure to use ranking-specific metrics such as Mean Average Precision (MAP), Normalized Discounted Cumulative Gain (NDCG), or Expected Reciprocal Rank (ERR). These metrics better capture the quality of the ranked list compared to traditional classification metrics.

By leveraging XGBoost’s “rank:pairwise” objective and following best practices for feature engineering and model tuning, you can build highly effective learning to rank models to power your search, recommendation, or ranking systems.

See Also