Skip to content
AI360Xpert
Core ML

Learning to Rank (LTR)

Instead of training a model to predict an absolute score (like '4.2 stars'), LTR trains a model to understand relative ordering ('Item A is better than Item B').

Pointwise predicts absolute scores. Pairwise learns that A > B. Listwise looks at the entire output and optimizes the global sorting order.
Pointwise predicts absolute scores. Pairwise learns that A > B. Listwise looks at the entire output and optimizes the global sorting order.

Why Does This Exist?

In the Recommender System Architecture, the final stage is Ranking. We have 500 items, and we need to pick the best 10 and display them in the perfect order.

Historically, companies did this by predicting an absolute score. They would train a standard regression model to predict exactly how many seconds a user would watch a video.

  • Video A: Predicted 120 seconds.
  • Video B: Predicted 90 seconds.
  • Video C: Predicted 130 seconds. Then, they just sorted the list by the predicted seconds: CABC \rightarrow A \rightarrow B.

This is called the Pointwise approach. It works, but it fundamentally misunderstands the goal of a search engine.

A search engine doesn't care if a user watches a video for 120 seconds or 121 seconds. The search engine only cares that the best video is in the #1 slot, the second-best is in the #2 slot, and the worst video is buried on page 4. Learning to Rank (LTR) completely abandons predicting absolute numbers, and instead trains the model to directly optimize the order of the list.

Think of It Like This

Think of It Like This

Imagine you are judging a dog show.

Pointwise (Standard ML): You look at Dog A and write down "87.4 points". You look at Dog B and write down "91.2 points". This is very hard because you have to invent an absolute mathematical scale in your head.

Pairwise (Learning to Rank): You look at Dog A and Dog B standing next to each other. You say "Dog B is prettier than Dog A." You don't know their exact scores, and you don't care. You just know B>AB > A.

Ranking based on relative comparisons is vastly easier and more accurate than ranking based on absolute scores.

How It Actually Works

Learning to Rank algorithms are categorized by their Loss Functions (how they calculate their mistakes during training).

1. Pointwise (The Old Way)

The model looks at one item at a time. It tries to predict an exact rating (e.g., MSE loss). It is largely obsolete for ranking tasks because it doesn't understand the concept of a "list".

2. Pairwise (The Standard Way)

The model looks at two items at a time: a positive item (the user clicked it) and a negative item (the user ignored it). The loss function (like BPR - Bayesian Personalized Ranking) punishes the model only if it scores the negative item higher than the positive item. The actual numerical scores don't matter, as long as Score(Positive)>Score(Negative)Score(Positive) > Score(Negative).

3. Listwise (The State of the Art)

The model looks at the entire list of 10 items simultaneously. It calculates the theoretical perfect order, and compares it to the model's predicted order. It optimizes directly for ranking metrics like NDCG (Normalized Discounted Cumulative Gain).

Listwise is the most powerful because it understands position bias. It knows that putting a bad item in Slot #1 is a catastrophic failure, but putting a bad item in Slot #10 doesn't really matter.

Show Me the Code

Gradient Boosted Decision Trees (like XGBoost or LightGBM) are the undisputed kings of tabular ranking. You can use LightGBM's built-in lambdarank objective to perform Listwise Learning to Rank.

import lightgbm as lgbimport numpy as np
# 1. Prepare the data# X: The features of the items (e.g., price, category, past click rate)# y: The relevance score (e.g., 0 = ignored, 1 = clicked, 2 = bought)X_train = np.random.rand(100, 10) y_train = np.random.randint(0, 3, size=100)
# 2. Define the "Groups" (Queries)# This is critical! LTR needs to know which items belong to which search query.# Here, the first 20 items belong to Query 1, the next 30 to Query 2, etc.group_train = [20, 30, 15, 35]
# 3. Create the LightGBM Datasettrain_data = lgb.Dataset(X_train, label=y_train, group=group_train)
# 4. Train a Listwise LTR Modelparams = {    'objective': 'lambdarank', # The magic word for Listwise Ranking    'metric': 'ndcg',          # Optimize directly for NDCG    'ndcg_eval_at': [5, 10],   # We care most about the Top 5 and Top 10 slots    'learning_rate': 0.1,}
model = lgb.train(params, train_data, num_boost_round=50)
# 5. Predict the ranking order for a new query# The raw output numbers are meaningless absolute values, but their RELATIVE order is perfect.predictions = model.predict(X_new_query)

Watch Out For

Watch Out For

Position Bias ruins training data. If you put a terrible video in Slot #1 on YouTube, people will still click it because it's the first thing they see. If you put the greatest video ever made in Slot #50, nobody will click it. When you train an LTR model on this historical data, the model will falsely learn that the terrible video is highly relevant. You must explicitly de-bias your training data to account for the UI layout, often by using "Position" as a feature during training and then setting it to a neutral constant during inference.

The Quick Version

  • Pointwise models predict absolute scores (like predicting exact watch time).
  • Pairwise models learn relative preferences (Item A is better than Item B).
  • Listwise models look at the entire list at once and optimize directly for the final display order.
  • Learning to Rank (LTR) is the final stage of a recommender funnel, taking the surviving candidates and finding the absolute optimal sorting order for the user interface.

Related concepts