机器学习与深度学习 / 系统课时
用 GBDT 建立排序基线
用 GBDT 建立排序基线
在上深度排序模型之前,先训练一个 GBDT 基线,可以快速判断现有特征到底有没有预测价值,并给后续复杂模型一个明确参照。本节完成样本组织、类别特征处理、AUC 与 NDCG@K 验证,以及特征重要性检查。
学习目标
- 用数值特征和召回来源特征训练一个可复现的 GBDT 排序基线
- 同时观察 AUC 与按请求计算的 NDCG@K,避免只做二分类评估
- 用特征重要性快速识别有效特征、弱特征和潜在数据问题
基线模型要回答什么
排序基线的任务不是追求最终效果,而是回答三个工程问题:当前标签是否可学、现有特征是否含有有效信号、复杂模型相对简单树模型究竟多带来多少收益。GBDT 对数值尺度不敏感,又能自动学习非线性交互,很适合做这一层快速体检。
推荐排序通常仍把每个候选样本表示为一行,包含用户与物品匹配度、历史点击统计、召回分、时效性、召回来源等特征,以及点击或转化标签。离线评估除了全局 AUC,还应该按一次请求或一个用户候选集合计算 NDCG@K,因为线上真正关心的是前几名顺序。
正确处理类别特征
source 这样的召回来源是类别变量,不能简单映射成 0、1、2 后直接交给树模型,否则数字大小会制造不存在的顺序关系。下面使用 One-Hot 编码,并把预处理和模型封装进同一个 Pipeline,避免训练和验证阶段的特征处理不一致。
示例把验证集划成两个请求,每个请求包含 6 个候选。AUC 在全部候选上计算,NDCG@3 则分别在两个请求内部计算后取平均。
import numpy as np
import pandas as pd
from sklearn.compose import ColumnTransformer
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.metrics import ndcg_score, roc_auc_score
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import OneHotEncoder
train = pd.DataFrame([
(1, .92, .18, .90, .80, "cf"),
(1, .88, .16, .84, .75, "cf"),
(1, .82, .14, .79, .70, "content"),
(1, .76, .13, .72, .68, "content"),
(1, .70, .12, .66, .62, "hot"),
(1, .65, .11, .60, .58, "hot"),
(0, .72, .07, .58, .52, "cf"),
(0, .68, .06, .55, .50, "content"),
(0, .60, .09, .50, .45, "content"),
(0, .55, .05, .46, .40, "hot"),
(0, .48, .08, .44, .35, "cf"),
(0, .42, .04, .38, .32, "hot"),
(0, .35, .03, .32, .28, "content"),
(0, .30, .05, .30, .25, "cf"),
(0, .24, .02, .25, .20, "hot"),
(0, .18, .01, .20, .15, "content"),
(0, .12, .03, .18, .12, "cf"),
(0, .08, .01, .12, .10, "hot"),
], columns=[
"label", "match", "ctr",
"source_score", "freshness", "source",
])
validation = pd.DataFrame([
(1, .90, .17, .88, .78, "cf"),
(0, .73, .08, .62, .60, "cf"),
(0, .80, .15, .76, .72, "content"),
(1, .58, .05, .48, .42, "hot"),
(0, .40, .04, .35, .30, "content"),
(0, .20, .02, .22, .20, "hot"),
(1, .84, .15, .81, .74, "cf"),
(0, .69, .07, .57, .55, "content"),
(1, .74, .12, .70, .66, "content"),
(0, .52, .06, .45, .39, "hot"),
(0, .33, .03, .30, .27, "cf"),
(0, .15, .01, .16, .14, "hot"),
], columns=train.columns)
numeric_features = ["match", "ctr", "source_score", "freshness"]
categorical_features = ["source"]
preprocess = ColumnTransformer([
("num", "passthrough", numeric_features),
(
"cat",
OneHotEncoder(handle_unknown="ignore"),
categorical_features,
),
])
model = GradientBoostingClassifier(
n_estimators=30,
max_depth=2,
learning_rate=0.08,
random_state=7,
)
pipeline = Pipeline([
("preprocess", preprocess),
("model", model),
])
pipeline.fit(
train[numeric_features + categorical_features],
train["label"],
)
scores = pipeline.predict_proba(
validation[numeric_features + categorical_features]
)[:, 1]
auc = roc_auc_score(validation["label"], scores)
request_ndcg = []
for start in (0, 6):
labels = validation["label"].iloc[start:start + 6].to_numpy()
request_scores = scores[start:start + 6]
request_ndcg.append(
ndcg_score([labels], [request_scores], k=3)
)
feature_names = (
pipeline.named_steps["preprocess"].get_feature_names_out()
)
importances = pipeline.named_steps["model"].feature_importances_
print(f"AUC={auc:.3f}")
print(f"NDCG@3={np.mean(request_ndcg):.3f}")
for name, importance in sorted(
zip(feature_names, importances),
key=lambda item: -item[1],
):
print(f"{name}={importance:.3f}")预期输出
AUC=0.781
NDCG@3=0.750
num__source_score=0.472
num__ctr=0.325
num__freshness=0.204
num__match=0.000
cat__source_cf=0.000
cat__source_content=0.000
cat__source_hot=0.000这组小数据里,source_score、ctr 和 freshness 提供了主要分裂收益,而 match 与来源 One-Hot 没有被树使用。不能据此直接删除零重要性特征:树的重要性会受样本量、相关特征和分裂顺序影响,真实数据应再结合 permutation importance、消融实验和多时间窗稳定性判断。
看指标时不要只盯 AUC
AUC 衡量正例总体排在负例前面的概率,适合判断模型有没有全局区分能力;NDCG@K 更关注同一请求内前 K 个位置,因此更接近首页或推荐流前排体验。一个模型完全可能 AUC 上升但 NDCG@10 不升,原因往往是它优化了大量尾部容易样本,却没有改善最接近决策边界的候选。
验证时应保证同一次请求的候选不跨集合拆分,并按时间划分训练集与验证集。如果特征来自点击率、转化率或召回分,还要确认统计窗口只使用样本时刻之前的数据,否则 GBDT 很容易把泄漏特征学成“神特征”。
把 GBDT 当作后续模型的参照线
记录基线时至少固定数据版本、特征版本、采样策略、AUC、NDCG@K、训练耗时和单条推理耗时。后续换成 Wide & Deep、DeepFM、DIN 或其他深度排序模型时,只有在同一切分和同一候选集上比较,收益才有意义。
如果深度模型只带来极小的 NDCG 提升,却显著增加训练资源、在线延迟和特征维护成本,GBDT 可能已经是更划算的方案;反过来,如果 GBDT 很快达到瓶颈,而交叉特征和序列行为明显重要,就有了继续投入复杂模型的证据。