【问题标题】:AttributeError: 'GridSearchCV' object has no attribute 'best_params_'AttributeError:“GridSearchCV”对象没有属性“best_params_”
【发布时间】:2020-07-02 07:15:30
【问题描述】:

网格搜索是一种从我们指定的组合中为任何模型找到最佳参数的方法。我以以下方式在我的模型上形成了网格搜索,并希望找到使用此网格搜索确定的最佳参数。

from sklearn.model_selection import GridSearchCV
# Create the parameter grid based on the results of random search 
param_grid = {
    'bootstrap': [True],'max_depth': [20,30,40, 100, 110],
    'max_features': ['sqrt'],'min_samples_leaf': [5,10,15],
    'min_samples_split': [40,50,60], 'n_estimators': [150, 200, 250]
}
# Create a based model
rf = RandomForestClassifier()
# Instantiate the grid search model
grid_search = GridSearchCV(estimator = rf, param_grid = param_grid, 
                          cv = 3, n_jobs = -1, verbose = 2)

现在我想找到gridsearch的最佳参数作为输出

grid_search.best_params_

错误

----> grid_search.best_params_
AttributeError: 'GridSearchCV' object has no attribute 'best_params_'

我错过了什么?

【问题讨论】:

    标签: python scikit-learn grid-search gridsearchcv


    【解决方案1】:

    如果不拟合数据,您将无法获得最佳参数。

    拟合数据

    grid_search.fit(X_train, y_train)
    

    现在找到最佳参数。

    grid_search.best_params_
    

    grid_search.best_params_ 将在适合X_trainy_train 后工作。

    【讨论】:

      猜你喜欢
      • 2021-10-03
      • 2020-07-02
      • 2016-03-15
      • 2017-05-22
      • 2020-04-20
      • 2020-03-13
      • 2019-03-29
      • 1970-01-01
      • 2020-01-31
      相关资源
      最近更新 更多