【问题标题】:XGBoost algorithm, question about the evaulate_model functionXGBoost算法,关于evaluate_model函数的问题
【发布时间】:2019-02-06 17:41:16
【问题描述】:

这个评估模型功能经常使用,我发现它在 IBM 使用过here。但我会在这里展示这个功能:

def evaluate_model(alg, train, target, predictors, useTrainCV=True , cv_folds=5, early_stopping_rounds=50):

    if useTrainCV:
        xgb_param = alg.get_xgb_params()
        xgtrain = xgb.DMatrix(train[predictors].values, target['Default Flag'].values)
        cvresult = xgb.cv(xgb_param, xgtrain, num_boost_round=alg.get_params()['n_estimators'], nfold=cv_folds,
            metrics='auc', early_stopping_rounds=early_stopping_rounds, verbose_eval=True)
        alg.set_params(n_estimators=cvresult.shape[0])

    #Fit the algorithm on the data
    alg.fit(train[predictors], target['Default Flag'], eval_metric='auc')

    #Predict training set:
    dtrain_predictions = alg.predict(train[predictors])
    dtrain_predprob = alg.predict_proba(train[predictors])[:,1]

    #Print model report:
    print("\nModel Report")
    print("Accuracy : %.6g" % metrics.accuracy_score(target['Default Flag'].values, dtrain_predictions))
    print("AUC Score (Train): %f" % metrics.roc_auc_score(target['Default Flag'], dtrain_predprob))  
    plt.figure(figsize=(12,12))
    feat_imp = pd.Series(alg.get_booster().get_fscore()).sort_values(ascending=False)
    feat_imp.plot(kind='bar', title='Feature Importance', color='g')
    plt.ylabel('Feature Importance Score')
    plt.show()

调整XGboost的参数后,我有

xgb4 = XGBClassifier(
    objective="binary:logistic", 
    learning_rate=0.10,  
    n_esimators=5000,
    max_depth=6,
    min_child_weight=1,
    gamma=0.1,
    subsample=0.8,
    colsample_bytree=0.8,
    reg_alpha=0.1,
    nthread=4,
    scale_pos_weight=1.0,
    seed=27)
features = [x for x in X_train.columns if x not in ['Default Flag','ID']]
evaluate_model(xgb4, X_train, y_train, features)

我得到的结果是

Model Report
Accuracy : 0.803236
AUC Score (Train): 0.856995

我的问题可能是消息不灵通的是,这个evaulate_model() 函数没有在我发现奇怪的数据的测试集上进行测试。当我在测试集上调用它时 (evaluate_model(xgb4, X_test, y_test, features)) 我明白了

Model Report
Accuracy : 0.873706
AUC Score (Train): 0.965286

鉴于测试集的准确度高于训练集,我想知道这两个模型报告是否完全相关。如果这个问题的结构呈现不佳,我深表歉意。

【问题讨论】:

  • 这个函数似乎在你给它的数据集上训练,并返回训练准确率和 AUC:因此这不是评估模型的可靠方法。您的测试分数较高是因为您的测试集较小,因此模型更容易过拟合。

标签: python machine-learning xgboost


【解决方案1】:

我会进一步完善我的答案:

此函数在您提供的数据集上进行训练,并返回训练精度和 AUC:因此这不是评估模型的可靠方法。

在您提供的链接中,据说此功能用于调整估算器的数量:

下面的函数执行以下操作来找到最好的 用于数据的提升树的数量:

  • 使用数据特征训练 XGBoost 模型。
  • 对模型执行 k 折交叉验证,使用准确度和 AUC 分数作为评估指标。
  • 返回每个提升轮次的输出,以便您了解模型的学习情况。您将在下一个
    中查看详细输出 部分。
  • 在交叉验证分数没有通过额外的增强轮显着提高后停止运行,从而为您提供
    模型的最佳估计器数量。

您不应该使用它来评估您的模型性能,而应该执行干净的交叉验证。

在这种情况下,您的测试分数较高,因为您的测试集较小,因此模型更容易过拟合。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多