固定随机状态后随机森林的不同结果
Different results from random forest after fixing the random state
我有以下代码。我已经设置了 运行dom 状态。每次我进行交叉验证时,它都会给我一组新的最佳参数。这对我来说没有意义。为什么会这样?
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
为了获得可重现的结果,您必须为代码中涉及随机性的每个操作设置种子。在这里你为 GridSearchCV
和 KFold
做,但不是为你的 RandomForestClassifier
;你应该将它初始化为
pipe_steps = [('rfc',RandomForestClassifier(random_state=rs))]
我有以下代码。我已经设置了 运行dom 状态。每次我进行交叉验证时,它都会给我一组新的最佳参数。这对我来说没有意义。为什么会这样?
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
为了获得可重现的结果,您必须为代码中涉及随机性的每个操作设置种子。在这里你为 GridSearchCV
和 KFold
做,但不是为你的 RandomForestClassifier
;你应该将它初始化为
pipe_steps = [('rfc',RandomForestClassifier(random_state=rs))]