【问题标题】:Different results from random forest after fixing the random state固定随机状态后随机森林的不同结果
【发布时间】:2021-03-25 00:28:26
【问题描述】:

我有以下代码。我已经设置了随机状态。每次我进行交叉验证时,它都会给我一组新的最优参数。这对我来说没有意义。为什么会这样?

rs = 5
param_range = np.arange(1,150,10,dtype=int)
param_range2 = np.arange(5,20,5,dtype=int)
pipe_steps = [('rfc',RandomForestClassifier())]
check_params = {
    'rfc__n_estimators':param_range,
    'rfc__max_depth':param_range2
}


pipeline = Pipeline(pipe_steps)

print('-------------------------- CV Start - Fitting training data --------------------------')
for K in [5,8,10]:
    create_grid = GridSearchCV(pipeline,param_grid=check_params,cv=KFold(n_splits=K, random_state=rs, shuffle=True))
    create_grid.fit(X_train,y_train)
    print('********************* Pipeline %d fold CV *********************' % (K))
    print(create_grid.best_params_)
    print("test score:= %3.2f" % (create_grid.score(X_test,y_test)))
print("CV End")

第一次,我运行代码,它会在下面给我

-------------------------- CV Start - Fitting training data --------------------------
********************* Pipeline 5 fold CV *********************
{'rfc__max_depth': 10, 'rfc__n_estimators': 21}
test score:= 0.53
********************* Pipeline 8 fold CV *********************
{'rfc__max_depth': 10, 'rfc__n_estimators': 101}
test score:= 0.61
********************* Pipeline 10 fold CV *********************
{'rfc__max_depth': 5, 'rfc__n_estimators': 81}
test score:= 0.68
CV End

第二次,我跑了代码,最优参数变了。

-------------------------- CV Start - Fitting training data --------------------------
********************* Pipeline 5 fold CV *********************
{'rfc__max_depth': 10, 'rfc__n_estimators': 81}
test score:= 0.55
********************* Pipeline 8 fold CV *********************
{'rfc__max_depth': 15, 'rfc__n_estimators': 71}
test score:= 0.53
********************* Pipeline 10 fold CV *********************
{'rfc__max_depth': 15, 'rfc__n_estimators': 81}
test score:= 0.63
CV End

【问题讨论】:

    标签: python machine-learning scikit-learn random-forest gridsearchcv


    【解决方案1】:

    为了获得可重现的结果,您必须为代码中涉及随机性的每个操作设置种子。在这里,您为GridSearchCVKFold 执行此操作,但不是为您的RandomForestClassifier;你应该把它初始化为

    pipe_steps = [('rfc',RandomForestClassifier(random_state=rs))]
    

    【讨论】:

      猜你喜欢
      • 2016-08-10
      • 2018-04-12
      • 2016-09-16
      • 2020-03-30
      • 2018-02-05
      • 2021-12-31
      • 2021-05-29
      • 2015-09-16
      • 2019-09-05
      相关资源
      最近更新 更多