【问题标题】:sklearn cross-validation R^2 score does not match with manual check using trained model on training and validation datasklearn 交叉验证 R^2 分数与使用训练模型对训练和验证数据进行的手动检查不匹配
【发布时间】:2023-03-29 05:14:01
【问题描述】:

对以下问题的任何帮助将不胜感激。下面,X 是输入描述符(大小为 (10000, 72)),Y 是输出标签,一个列向量。应用随机森林模型。举个简单的例子,网格搜索仅在一个迭代器上进行,并执行一个交叉验证拆分。在最后训练模型之前,会收集训练和测试(更准确地说是验证数据)数据点。

param_grid = {'randomforestregressor__min_samples_split':[5]}

clf = pipeline.make_pipeline(RandomForestRegressor(random_state=1))
cv = modsel.ShuffleSplit(n_splits=1, test_size=0.5, random_state=1)
gs = modsel.GridSearchCV(clf, cv=cv, param_grid=param_grid, scoring='r2', return_train_score=True, verbose=False)

for train_index, test_index in cv.split(X):
  Xtrain=X[train_index]; Ytrain=Y[train_index]
  Xtest=X[test_index]; Ytest=Y[test_index]

gs.fit(X, Y)
print(gs.cv_results_)

根据 cv_results,mean_train_score 为 0.85863713,mean_test_score(这应该是验证分数)为 0.41913632。然后将训练好的模型应用于 Xtrain 和 Xtest。

predictedYtrain=gs.best_estimator_.predict(Xtrain)
predictedYtest=gs.best_estimator_.predict(Xtest)

从 predictYtrain 与 Ytrain 或 predictYtest 与 Ytest 线性图,我观察到这两种情况的 R^2 都在 0.9 左右。这是怎么回事?我期待找到〜0.85和0.42。有人可以解释一下差异在哪里吗?

【问题讨论】:

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


    【解决方案1】:

    您没有控制ShuffleSplit 对象的随机状态,因此您每次都可能得到不同的结果。从您发布的示例中,尚不清楚 python 解释器是否在训练阶段和测试短语之间重新启动,但您正在酸洗的事实让我相信它是。

    尝试控制模型的随机状态:

    cv = modsel.ShuffleSplit(n_splits=1, test_size=0.5, random_state=1)
    

    或调整脚本,使其一次性运行,而无需停止解释器

    【讨论】:

    • 感谢您的回复。我编辑了我的问题以介绍 random_state 并按照您的建议使其一次性运行。还是同样的问题。您认为这是随机森林模型的一些怪癖,还是我误解了什么?
    猜你喜欢
    • 2018-09-01
    • 2014-05-01
    • 2021-12-10
    • 2018-05-03
    • 2016-01-29
    • 2021-01-19
    • 2020-05-11
    • 2011-12-16
    • 2020-10-28
    相关资源
    最近更新 更多