为什么 sklearn.grid_search.GridSearchCV 在每次执行时都返回随机结果?

dar*_*thy 5 machine-learning scikit-learn cross-validation

我试图在 Iris 数据集上使用sklearn.grid_search.GridSearchCV. 我使用 StratifiedKFold ( sklearn.cross_validation.StratifiedKFold) 进行交叉验证,因为我的数据有偏差。但是在每次执行 时GridSearchCV,它都会返回一组不同的参数。
鉴于数据和交叉验证每次都相同,它不应该返回相同的一组最佳参数吗?

源代码如下:

from sklearn.tree import DecisionTreeClassifier
from sklearn.grid_search import GridSearchCV

decision_tree_classifier = DecisionTreeClassifier()

parameter_grid = {'max_depth': [1, 2, 3, 4, 5],
                  'max_features': [1, 2, 3, 4]}

cross_validation = StratifiedKFold(all_classes, n_folds=10)

grid_search = GridSearchCV(decision_tree_classifier, param_grid = parameter_grid,
                          cv = cross_validation)

grid_search.fit(all_inputs, all_classes)

print "Best Score: {}".format(grid_search.best_score_)
print "Best params: {}".format(grid_search.best_params_)
Run Code Online (Sandbox Code Playgroud)

输出:

Best Score: 0.959731543624
Best params: {'max_features': 2, 'max_depth': 2}

Best Score: 0.973154362416
Best params: {'max_features': 3, 'max_depth': 5}

Best Score: 0.973154362416
Best params: {'max_features': 2, 'max_depth': 5}

Best Score: 0.959731543624
Best params: {'max_features': 3, 'max_depth': 3}
Run Code Online (Sandbox Code Playgroud)

这是我最近制作的 Ipython 笔记本的摘录,参考了Randal S Olson 的笔记本,可以在这里找到。

编辑:它不是random_state参数StratifiedKFold这会导致不同的结果,而是对random_state的参数DecisionTreeClassifer,其随机初始化树,并给出不同的结果(参见文档)。至于StratifiedKFold,只要shuffle参数设置为False(默认),它就会生成相同的训练-测试分割(请参阅文档)。

Bos*_*ang 1

对于每次运行,CV 随机分割训练集和验证集,因此每次运行的结果都会不同。