主题
排名:从分数到名次
排序告诉你"谁排第几",但排名(ranking)解决的是一个更微妙的问题:当分数相同时,名次怎么算?这在 LLM 评测排行榜中是一个必须明确的问题——两个模型 MMLU 都是 88.7 分,它们是并列第一还是分列第一第二?
六种排名方法
python
import pandas as pd
scores = pd.Series([95, 90, 90, 85, 85, 85, 70], name='score')
result = pd.DataFrame({'原始': scores})
result['average'] = scores.rank(method='average', ascending=False)
result['min'] = scores.rank(method='min', ascending=False)
result['max'] = scores.rank(method='max', ascending=False)
result['first'] = scores.rank(method='first', ascending=False)
result['dense'] = scores.rank(method='dense', ascending=False)
print(result)输出:
原始 average min max first dense
0 95 1.0 1.0 1.0 1.0 1.0
1 90 2.5 2.0 3.0 2.0 2.0
2 90 2.5 2.0 3.0 3.0 2.0
3 85 5.0 4.0 6.0 4.0 3.0
4 85 5.0 4.0 6.0 5.0 3.0
5 85 5.0 4.0 6.0 6.0 3.0
6 70 7.0 7.0 7.0 7.0 4.0六种方法的区别在于如何处理并列值:
- average(默认):并列者平分名次。90 分的两个模型分别得到 2.5 和 2.5——最符合统计学惯例
- min:并列者都取最优名次。两个 90 分都是第 2 名——下一个分数直接跳到第 4 名
- max:并列者都取最差名次。两个 90 分都是第 3 名
- first:按出现顺序分配不同名次——适合需要区分先后顺序的场景
- dense:类似 min 但名次连续不跳跃。三个 85 分都是第 3 名,70 分是第 4 名(不是第 7 名)
在 LLM 排行榜中推荐用 method='average' 或 method='dense'——前者是学术界的标准做法,后者生成的名次更紧凑易读。
实战:自动生成模型排行榜
python
import pandas as pd
import numpy as np
np.random.seed(42)
models = ['GPT-4o', 'Claude', 'Llama', 'Qwen', 'DeepSeek']
benchmarks = ['MMLU', 'HumanEval', 'MATH']
rows = []
for m in models:
for b in benchmarks:
base = {'GPT-4o':88,'Claude':89,'Llama':84,'Qwen':83,'DeepSeek':87}[m]
rows.append({'model':m, 'benchmark':b, 'score':base+np.randn()*5})
df = pd.DataFrame(rows)
pivot = df.pivot_table(index='model', columns='benchmark', values='score')
pivot['mean'] = pivot.mean(axis=1).round(1)
pivot['rank'] = pivot['mean'].rank(ascending=False, method='dense').astype(int)
pivot = pivot.sort_values('rank')
print("=== 模型排行榜 ===")
print(pivot.round(1))