Project 17: Ranking Model Fitter
Scientific Computing
Builds on these lessons
Current Task
Objective
Final challenge: combine correlation analysis and optimization — measuring how well ranking predicts relevance, then fitting a simple model to it.
Task: compute the correlation between click rank and relevance, then fit a one-parameter weight that best predicts relevance from rank.
index.py
from scipy.optimize import minimize
from scipy.stats import pearsonr
import numpy as np
click_rank = np.array([1, 2, 3, 4, 5])
relevance = np.array([0.9, 0.7, 0.6, 0.4, 0.3])
correlation, _ = pearsonr(click_rank, relevance)
def ranking_cost(weights):
predicted = weights[0] * click_rank
return np.sum((predicted - relevance) ** 2)
result = minimize(ranking_cost, x0=[0])
print(round(correlation, 3))
print(result.x.round(3))
* Hint: Correct characters turn green, incorrect ones turn red.
Live Preview
🖼️
Verify your code to see the preview