AttributeError: 'GridSearchCV' 对象没有属性 'best_params_'

noo*_*oob 5 python scikit-learn grid-search gridsearchcv

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

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)
Run Code Online (Sandbox Code Playgroud)

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

grid_search.best_params_
Run Code Online (Sandbox Code Playgroud)

错误

----> grid_search.best_params_
AttributeError: 'GridSearchCV' object has no attribute 'best_params_'
Run Code Online (Sandbox Code Playgroud)

我错过了什么?

noo*_*oob 12

不拟合数据就无法获得最佳参数。

拟合数据

grid_search.fit(X_train, y_train)
Run Code Online (Sandbox Code Playgroud)

现在找到最好的参数。

grid_search.best_params_
Run Code Online (Sandbox Code Playgroud)

grid_search.best_params_将在安装X_train和后工作y_train