You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何分层K折交叉验证与无交叉验证模型性能差异显著?

问题排查:分层K折交叉验证模型性能优于无交叉验证模型的原因

我开发了两个CatBoost分类模型,一个采用Stratified K-Fold交叉验证,另一个未使用交叉验证。意外的是,带交叉验证的模型性能显著优于无交叉验证版本,现需要排查可能的实现错误或导致差异的因素。


带分层K折交叉验证的代码及结果

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.metrics import roc_auc_score, accuracy_score, confusion_matrix, classification_report, roc_curve
from sklearn.model_selection import StratifiedKFold
from catboost import CatBoostClassifier

# 将x_train和y_train转换为numpy数组(若尚未转换)
x_train = np.array(x_train)
y_train = np.array(y_train)

# 设置折数
n_splits = 5

# 初始化存储各折评估指标的列表
auc_scores = []
accuracy_scores = []

# 初始化分层K折交叉验证器
skf = StratifiedKFold(n_splits=n_splits, shuffle=True, random_state=42)

# 执行分层K折交叉验证
for train_index, test_index in skf.split(x_train, y_train):
    # 划分当前折的训练集和测试集
    x_train_fold, x_test_fold = x_train[train_index], x_train[test_index]
    y_train_fold, y_test_fold = y_train[train_index], y_train[test_index]
    
    # 创建CatBoost分类器
    catboost = CatBoostClassifier(n_estimators=300, random_state=42, silent=True, learning_rate=0.1, max_depth=7)

    # 拟合模型
    catboost.fit(x_train_fold, y_train_fold)
    
    # 对当前折的测试集预测概率
    catboost_pred = catboost.predict_proba(x_test_fold)[:, 1]
    
    # 计算当前折的AUC
    auc = roc_auc_score(y_test_fold, catboost_pred)
    
    # 将概率转换为二分类预测结果
    y_pred = (catboost_pred > 0.5).astype(int)
    
    # 生成当前折的分类报告
    classification_rep = classification_report(y_test_fold, y_pred)
    
    # 打印当前折的评估指标
    print("Fold AUC:", auc)
  
    print("Fold Classification Report:\n", classification_rep)
    print("--------------------")

交叉验证模型的典型结果

0       0.77      0.87      0.82     14125
  1       0.85      0.74      0.79     14125

accuracy                      0.81     28250

无交叉验证的代码及结果

from catboost import CatBoostClassifier
from sklearn.metrics import roc_auc_score, accuracy_score, confusion_matrix, classification_report

# 创建CatBoost分类器
catboost = CatBoostClassifier(n_estimators=300, random_state=42, silent=True, learning_rate=0.1, max_depth=7)

# 拟合模型
catboost.fit(x_train, y_train)

# 对测试集预测概率
catboost_pred = catboost.predict_proba(x_test)[:, 1]

# 计算AUC
auc = roc_auc_score(y_test, catboost_pred)

# 将概率转换为二分类预测结果
y_pred = (catboost_pred > 0.5).astype(int)

# 生成分类报告
classification_rep = classification_report(y_test, y_pred)

# 打印评估指标和分类报告
print("AUC:", auc)
print("Classification Report:\n", classification_rep)

无交叉验证模型的结果

0       0.83      0.87      0.85     17551
       1       0.39      0.33      0.36      4545

accuracy                           0.76     22096

可能的原因分析

  • 数据集分布差异:交叉验证用的是x_train拆分出的验证集,类别分布均衡(两类各占50%);而无交叉验证模型的测试集类别1占比仅约20.6%,严重的类别不平衡导致模型在少数类上表现极差。
  • 泛化能力评估偏差:交叉验证结果是多次训练验证的平均,能准确反映模型在训练集分布内的泛化能力;无交叉验证模型仅在分布差异大的独立测试集上评估,表现自然被拉低。
  • 过拟合风险差异:交叉验证中每个模型仅用原训练集的4/5数据训练,降低了过拟合特定噪声或模式的概率;无交叉验证模型用全量训练集,更容易对训练集的独有特征过拟合,在陌生分布的测试集上失效。
  • 数据泄露隐患:检查无交叉验证代码是否存在预处理泄露——比如是否提前对全量数据(含测试集)做标准化等操作,导致测试集信息流入训练过程;而交叉验证中每个折独立预处理,避免了这类问题。

内容的提问来源于stack exchange,提问作者pooya ensafi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 10:18:07