【发布时间】: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