了解 sklearn grid_search

Understanding sklearn grid_search

我很难理解 grid_search class 的工作原理。我想找到可以与 RandomForestClassifier 一起使用的最佳 max_depth 参数。我指定了我希望搜索到 运行 的可能选项,并且我希望模块输出 "best fitting" max_depth 选项。

from sklearn.datasets import load_iris
from sklearn.ensemble import RandomForestClassifier
from sklearn import grid_search

iris= load_iris()    
forest_parameters = {'max_depth': [1,2,3,4]}
forest = RandomForestClassifier()
explorer = grid_search.GridSearchCV(forest, forest_parameters)
explorer.fit(iris['data'], iris['target'])

我希望我的 explorer 网格搜索模块 return 具有最好的 max_depth 参数,给定一组可能的选项 [1,2,3,4]。为什么仍然使用 None 的默认值?如何使用 grid_search 查找 "best-fitting" 参数?

Out[13]: 
GridSearchCV(cv=None, error_score='raise',
       estimator=RandomForestClassifier(bootstrap=True, class_weight=None, criterion='gini',
       ---> max_depth=None, max_features='auto', max_leaf_nodes=None,
            min_samples_leaf=1, min_samples_split=2,
            min_weight_fraction_leaf=0.0, n_estimators=10, n_jobs=1,
            oob_score=False, random_state=None, verbose=0,
            warm_start=False),
       fit_params={}, iid=True, loss_func=None, n_jobs=1,
       param_grid={'max_depth': [1, 2, 3, 4]}, pre_dispatch='2*n_jobs',
       refit=True, score_func=None, scoring=None, verbose=0)

这些只是调用网格搜索时使用的参数。要确定最佳参数,请使用 explorer.best_params_,或者您可以使用 explorer.best_estimator_ 找到估算器,前提是 refit 已启用。