使用 RandomForestRegressor 在 param_grid 上出现 GridSearchCV 错误
GridSearchCV Error on param_grid With RandomForestRegressor
ValueError: Invalid parameter estimator for estimator RandomForestRegressor().
Check the list of available parameters with `estimator.get_params().keys()`.
这是我在 RandomForestRegressor 模型上使用 GridSearchCV 时遇到的错误。
这是代码
%%time
from sklearn.model_selection import RandomizedSearchCV
rf_grid= {"estimator__n_estimators ": np.arange(10,100,10),
"estimator__max_depth ": [None,3,5,10],
"estimator__min_sample_split": np.arange(2,20,2),
"estimator__min_sample_leaf" : np.arange(1,20,2),
"estimator__max_features ": [0.5,1,'sqrt','auto'],
"estimator__max_samples" : [10000]
}
rfr_2=RandomForestRegressor()
rs_model= RandomizedSearchCV(estimator=rfr_2,
param_distributions=rf_grid,
n_iter=100,
cv=5,
verbose= True)
rs_model.fit(X_train,Y_train)
来自错误信息:
ValueError: Invalid parameter estimator for estimator RandomForestRegressor().
Check the list of available parameters with estimator.get_params().keys()
.
您可以看到您错误地指定了 rf_grid
中的参数。
使用:
rf_grid= {"n_estimators": np.arange(10,100,10),
"max_depth": [None,3,5,10],
"min_samples_split": np.arange(2,20,2),
"min_samples_leaf" : np.arange(1,20,2),
"max_features": [0.5,1,'sqrt','auto'],
"max_samples" : [10000]
}
建议来自:
rfr_2.get_params().keys()
dict_keys(['bootstrap', 'ccp_alpha', 'criterion', 'max_depth',
'max_features', 'max_leaf_nodes', 'max_samples',
'min_impurity_decrease', 'min_impurity_split', 'min_samples_leaf',
'min_samples_split', 'min_weight_fraction_leaf', 'n_estimators',
'n_jobs', 'oob_score', 'random_state', 'verbose', 'warm_start'])
首先,为了了解您应该如何在字典中命名参数,您可以像这样预先打印它们:
print(rfr_2.get_params())
打印完所有参数后,现在您可以选择要将哪些参数传递到网格字典中
你应该做的是去掉你放在每个参数前面的“estimator__”。
此外,一旦你这样做,你会遇到另一个错误。即,参数 max_samples 可以取 1 到 232 之间的值,您输入的值 (10000) 太高了。
下面是修改后的代码块,应该可以工作!
rf_grid= {"n_estimators": np.arange(10,100,10),
"max_depth": [None,3,5,10],
"min_samples_split": np.arange(2,20,2),
"min_samples_leaf" : np.arange(1,20,2),
"max_features": [0.5,1,'sqrt','auto'],
"max_samples" : [100]
原来我应该使用单引号 ' ' 而不是双引号 " "。
%%time
from sklearn.model_selection import RandomizedSearchCV
rf_grid= {'n_estimators': np.arange(10,100,10),
'max_depth': [None,3,5,10],
'min_samples_split': np.arange(2,20,2),
'min_samples_leaf' : np.arange(1,20,2),
'max_features': [0.5,1,'sqrt','auto'],
'max_samples' : [100]
}
rs_model= RandomizedSearchCV(rfr,
param_distributions=rf_grid,
n_iter=100,
cv=5,
verbose= True)
rs_model.get_params()
}
ValueError: Invalid parameter estimator for estimator RandomForestRegressor().
Check the list of available parameters with `estimator.get_params().keys()`.
这是我在 RandomForestRegressor 模型上使用 GridSearchCV 时遇到的错误。 这是代码
%%time
from sklearn.model_selection import RandomizedSearchCV
rf_grid= {"estimator__n_estimators ": np.arange(10,100,10),
"estimator__max_depth ": [None,3,5,10],
"estimator__min_sample_split": np.arange(2,20,2),
"estimator__min_sample_leaf" : np.arange(1,20,2),
"estimator__max_features ": [0.5,1,'sqrt','auto'],
"estimator__max_samples" : [10000]
}
rfr_2=RandomForestRegressor()
rs_model= RandomizedSearchCV(estimator=rfr_2,
param_distributions=rf_grid,
n_iter=100,
cv=5,
verbose= True)
rs_model.fit(X_train,Y_train)
来自错误信息:
ValueError: Invalid parameter estimator for estimator RandomForestRegressor(). Check the list of available parameters with
estimator.get_params().keys()
.
您可以看到您错误地指定了 rf_grid
中的参数。
使用:
rf_grid= {"n_estimators": np.arange(10,100,10),
"max_depth": [None,3,5,10],
"min_samples_split": np.arange(2,20,2),
"min_samples_leaf" : np.arange(1,20,2),
"max_features": [0.5,1,'sqrt','auto'],
"max_samples" : [10000]
}
建议来自:
rfr_2.get_params().keys()
dict_keys(['bootstrap', 'ccp_alpha', 'criterion', 'max_depth', 'max_features', 'max_leaf_nodes', 'max_samples', 'min_impurity_decrease', 'min_impurity_split', 'min_samples_leaf', 'min_samples_split', 'min_weight_fraction_leaf', 'n_estimators', 'n_jobs', 'oob_score', 'random_state', 'verbose', 'warm_start'])
首先,为了了解您应该如何在字典中命名参数,您可以像这样预先打印它们:
print(rfr_2.get_params())
打印完所有参数后,现在您可以选择要将哪些参数传递到网格字典中
你应该做的是去掉你放在每个参数前面的“estimator__”。
此外,一旦你这样做,你会遇到另一个错误。即,参数 max_samples 可以取 1 到 232 之间的值,您输入的值 (10000) 太高了。 下面是修改后的代码块,应该可以工作!
rf_grid= {"n_estimators": np.arange(10,100,10),
"max_depth": [None,3,5,10],
"min_samples_split": np.arange(2,20,2),
"min_samples_leaf" : np.arange(1,20,2),
"max_features": [0.5,1,'sqrt','auto'],
"max_samples" : [100]
原来我应该使用单引号 ' ' 而不是双引号 " "。
%%time
from sklearn.model_selection import RandomizedSearchCV
rf_grid= {'n_estimators': np.arange(10,100,10),
'max_depth': [None,3,5,10],
'min_samples_split': np.arange(2,20,2),
'min_samples_leaf' : np.arange(1,20,2),
'max_features': [0.5,1,'sqrt','auto'],
'max_samples' : [100]
}
rs_model= RandomizedSearchCV(rfr,
param_distributions=rf_grid,
n_iter=100,
cv=5,
verbose= True)
rs_model.get_params()
}