【问题标题】:scikit-learn random forest: severe overfitting?scikit-learn 随机森林:严重过拟合?
【发布时间】:2019-04-01 05:44:22
【问题描述】:

我正在尝试应用 knn、逻辑回归、决策树和随机森林来预测二元响应变量。

前三个产生看似合理的准确率,但运行随机森林算法产生的准确率超过 99%(1127/1128 正确)。

vote_lst = list(range(1, 101))
rf_cv_scores = []
for tree_count in vote_lst:
    maple = RandomForestClassifier(n_estimators = tree_count, random_state = 1618)
    scores = cross_val_score(maple, x, y, cv = 10, scoring = 'accuracy') # 10-fold CV
    rf_cv_scores.append(scores.mean()) 

# find minimum error's index (i.e. optimal num. of estimators)
rf_MSE = [1 - x for x in rf_cv_scores]
min_error = rf_MSE[0]
for i in range(len(rf_MSE)):
    min_error = min_error
    if rf_MSE[i] < min_error:
        rf_min_index = i
        min_error = rf_MSE[i]
print(rf_min_index + 1) # error minimized w/ 66 estimators

我使用上面的代码调整了 rf 算法超参数n_estimators。然后,我将模型拟合到我的数据上:

# fit random forest classifier
forest_classifier = RandomForestClassifier(n_estimators = rf_min_index + 1, random_state = 1618)
forest_classifier.fit(x, y)

# predict test set
y_pred_forest = forest_classifier.predict(x)

我担心这里发生了一些严重的过度拟合:有什么想法吗?

【问题讨论】:

  • 可能是这样。调整超参数后,不要在整个数据集上拟合最佳模型,而是使用交叉验证。如果您仍然获得 99% 的 WITH 交叉验证,您可以将其解释为一个完美的分类器。尝试使用 KFolds 与 K = 10
  • 而且即使您使用交叉验证来为每个模型挑选出最佳参数,该数据集上的性能也将是泛化误差的有偏估计,如果目标是模型选择,这很不方便.您需要在此处查看嵌套交叉验证。

标签: python machine-learning scikit-learn random-forest cross-validation


【解决方案1】:

我担心这里发生了一些严重的过度拟合:有什么想法吗?

您正在对上面训练过的同一数据集进行预测:

y_pred_forest = forest_classifier.predict(x)

【讨论】:

    猜你喜欢
    • 2018-06-07
    • 2015-03-28
    • 2015-03-31
    • 2015-04-13
    • 2016-03-01
    • 2013-04-26
    • 2015-09-16
    • 2015-04-16
    • 2015-11-21
    相关资源
    最近更新 更多