调整超参数时出现 GridSearchCV 错误

Getting Error for GridSearchCV while tuning hyperparameters

我在为 GridSearch 进行超参数调整时遇到奇怪的错误。 我从 randomizedsearchcv 得到了一些最好的参数,我正在尝试将这些参数放入网格搜索 cv 中。 我遇到错误

参数(标准)的参数网格需要是列表或 numpy 数组,但得到了 ()。单个值需要用一个元素包装在列表中。

代码如下

from sklearn.model_selection import GridSearchCV
clf=RandomForestClassifier()
n_estimators=rf_random_tuned.best_params_['n_estimators']
criterion=rf_random_tuned.best_params_['criterion']
max_depth=rf_random_tuned.best_params_['max_depth']
min_samples_split=rf_random_tuned.best_params_['min_samples_split']
min_samples_leaf=rf_random_tuned.best_params_['min_samples_leaf']
max_features=rf_random_tuned.best_params_['max_features']
param_grid_1={'n_estimators':[n_estimators-100,n_estimators,n_estimators+100],
           'criterion':criterion,
           'max_depth':[max_depth-1,max_depth-0.5,max_depth,max_depth+0.5,max_depth+1],
           'min_samples_split':[min_samples_split-14,min_samples_split,min_samples_split+14],
           'min_samples_leaf':[min_samples_leaf-0.16,min_samples_leaf,min_samples_leaf+0.16],
           'max_features':max_features
          }
rf_grid=GridSearchCV(estimator=clf,param_grid=param_grid_1,cv=5)
rf_grid.fit(X_train,y_train)

来自documentation

param_grid: dict or list of dictionaries

Dictionary with parameters names (str) as keys and lists of parameter settings to try as values, or a list of such dictionaries, in which case the grids spanned by each dictionary in the list are explored. This enables searching over any sequence of parameter settings.

基本上,它抱怨键 'criterion''max_features',其值应作为列表传递。