2015-12-25 41 views
1

我很努力去理解grid_search類是如何工作的。我想找到最好的max_depth參數,我可以使用RandomForestClassifier。我指定了可能的選項,我希望搜索能夠通過,我期望模塊輸出「最佳擬合」max_depth選項。理解sklearn grid_search

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網格搜索模塊用最好的max_depth參數返回,給出的一組可能的選項[1,2,3,4]。爲什麼None的默認值仍在使用?我如何使用grid_search找到「最佳擬合」參數?

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) 

回答

1

這些只是網格搜索被調用的參數。要確定最佳參數,請使用explorer.best_params_,或者您可以使用explorer.best_estimator_找到估算器,前提是啓用了refit

+0

如何訪問這些值? 'est_estimator_'和'best_params_'不是[列出的方法](http://scikit-learn.org/stable/modules/generated/sklearn.grid_search.GridSearchCV.html#sklearn.grid_search.GridSearchCV)。在我的例子中,當我檢查'explorer.best_params_'和'explorer.best_estimator_'時,我得到了屬性錯誤。 – kilojoules

+1

它們列在您鏈接到的頁面的屬性部分。在調用'fit'後,他們應該在那裏。 –