XGBoost › Ranking Example

Michael BrenndoerferJuly 18, 20255 min read
Machine Learning from ScratchAll chapters
Part 5 of 9Tree-Based Models
Chapter 44XGBoost › Ranking Example
Reading section 6 of 10 · Ranking Example · 5 minSection 6 of 10 · 5 min

Book contents

Section 6 of 10 · 5 min read

XGBoost › Ranking ExampleOne section · Sections in this chapter6 / 10 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

Let's work through a concrete ranking example to demonstrate how XGBoost handles learning-to-rank problems. We'll use a search engine scenario where we want to rank documents for a given query.

Consider a query "machine learning tutorial" with 4 documents that need to be ranked:

DocumentFeaturesRelevance Score
Doc 1[0.8, 0.9, 0.7]3 (Highly relevant)
Doc 2[0.6, 0.5, 0.8]1 (Not relevant)
Doc 3[0.9, 0.8, 0.6]2 (Somewhat relevant)
Doc 4[0.7, 0.7, 0.9]2 (Somewhat relevant)

Step 1: Initialize predictions We start with initial predictions (typically zeros or small random values):

  • Doc 1: y^1(0)=0\hat{y}^{(0)}_1 = 0
  • Doc 2: y^2(0)=0\hat{y}^{(0)}_2 = 0
  • Doc 3: y^3(0)=0\hat{y}^{(0)}_3 = 0
  • Doc 4: y^4(0)=0\hat{y}^{(0)}_4 = 0

Step 2: Calculate pairwise ranking loss For the pairwise ranking loss, we consider all pairs where one document should be ranked higher than another based on relevance scores.

Relevant pairs (Doc i should rank higher than Doc j):

  • (Doc 1, Doc 2): relevance 3 > 1
  • (Doc 1, Doc 3): relevance 3 > 2
  • (Doc 1, Doc 4): relevance 3 > 2
  • (Doc 3, Doc 2): relevance 2 > 1
  • (Doc 4, Doc 2): relevance 2 > 1

The pairwise loss for each pair (i,j)(i,j) is:

Lij=log⁡(1+exp⁡(−(y^i−y^j)))L_{ij} = \log(1 + \exp(-(\hat{y}_i - \hat{y}_j)))

Since all initial predictions are 0, the loss for each pair is:

Lij=log⁡(1+exp⁡(−(0−0)))=log⁡(1+1)=log⁡(2)≈0.693L_{ij} = \log(1 + \exp(-(0 - 0))) = \log(1 + 1) = \log(2) \approx 0.693

Step 3: Calculate gradients for ranking For ranking problems, the gradient calculation is more complex as it involves the pairwise relationships. The gradient for document ii is:

gi=∑j:reli>reljexp⁡(−(y^i−y^j))1+exp⁡(−(y^i−y^j))−∑j:relj>reliexp⁡(−(y^j−y^i))1+exp⁡(−(y^j−y^i))g_i = \sum_{j: rel_i > rel_j} \frac{\exp(-(\hat{y}_i - \hat{y}_j))}{1 + \exp(-(\hat{y}_i - \hat{y}_j))} - \sum_{j: rel_j > rel_i} \frac{\exp(-(\hat{y}_j - \hat{y}_i))}{1 + \exp(-(\hat{y}_j - \hat{y}_i))}

With initial predictions of 0, this simplifies to:

  • Doc 1: g1=3×0.5−0=1.5g_1 = 3 \times 0.5 - 0 = 1.5 (should rank higher than 3 docs)
  • Doc 2: g2=0−3×0.5=−1.5g_2 = 0 - 3 \times 0.5 = -1.5 (should rank lower than 3 docs)
  • Doc 3: g3=1×0.5−1×0.5=0g_3 = 1 \times 0.5 - 1 \times 0.5 = 0 (should rank higher than 1, lower than 1)
  • Doc 4: g4=1×0.5−1×0.5=0g_4 = 1 \times 0.5 - 1 \times 0.5 = 0 (should rank higher than 1, lower than 1)

Step 4: Build the first ranking tree Let's say we create a tree that splits on the first feature < 0.75:

  • Left leaf (feature 1 < 0.75): Doc 2, Doc 4
  • Right leaf (feature 1 ≥ 0.75): Doc 1, Doc 3

For the left leaf:

wleft=−g2+g4h2+h4+λ=−−1.5+02+λw_{left} = -\frac{g_2 + g_4}{h_2 + h_4 + \lambda} = -\frac{-1.5 + 0}{2 + \lambda}

For the right leaf:

wright=−g1+g3h1+h3+λ=−1.5+02+λw_{right} = -\frac{g_1 + g_3}{h_1 + h_3 + \lambda} = -\frac{1.5 + 0}{2 + \lambda}

With λ=1\lambda = 1:

  • wleft=1.53=0.5w_{left} = \frac{1.5}{3} = 0.5
  • wright=−1.53=−0.5w_{right} = -\frac{1.5}{3} = -0.5

Step 5: Update predictions After the first tree:

  • Doc 1: y^1(1)=0+(−0.5)=−0.5\hat{y}^{(1)}_1 = 0 + (-0.5) = -0.5
  • Doc 2: y^2(1)=0+0.5=0.5\hat{y}^{(1)}_2 = 0 + 0.5 = 0.5
  • Doc 3: y^3(1)=0+(−0.5)=−0.5\hat{y}^{(1)}_3 = 0 + (-0.5) = -0.5
  • Doc 4: y^4(1)=0+0.5=0.5\hat{y}^{(1)}_4 = 0 + 0.5 = 0.5

The ranking after the first tree is: Doc 2, Doc 4 (tied), Doc 1, Doc 3 (tied). This is not the correct ranking yet, so subsequent trees will continue to refine the predictions to achieve the desired ranking order.

Out[13]:
Visualization
Bar chart showing document ranking evolution with XGBoost predictions.
Document ranking evolution showing initial predictions, predictions after the first XGBoost tree, and target relevance scores. The plot demonstrates how XGBoost learns to rank documents by adjusting prediction scores to match the desired relevance-based ordering.