Rox*_*lia 3 random-forest scikit-learn gridsearchcv
当我尝试使用 Pipeline 和 param_grid 运行 RandomForestClassifier 时:
pipeline = Pipeline([("scaler" , StandardScaler()),
("rf",RandomForestClassifier())])
from sklearn.model_selection import GridSearchCV
param_grid = {
'max_depth': [4, 5, 10],
'max_features': [2, 3],
'min_samples_leaf': [3, 4, 5],
'n_estimators': [100, 200, 300]
}
# initialize
grid_pipeline = GridSearchCV(pipeline,param_grid,n_jobs=-1, verbose=1, cv=3, scoring='f1')
# fit
grid_pipeline.fit(X_train,y_train)
grid_pipeline.best_params_
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
ValueError: Invalid parameter max_depth for estimator Pipeline(memory=None,
steps=[('scaler',
StandardScaler(copy=True, with_mean=True, with_std=True)),
('rf',
RandomForestClassifier(bootstrap=True, ccp_alpha=0.0,
class_weight=None, criterion='gini',
max_depth=None, max_features='auto',
max_leaf_nodes=None, max_samples=None,
min_impurity_decrease=0.0,
min_impurity_split=None,
min_samples_leaf=1, min_samples_split=2,
min_weight_fraction_leaf=0.0,
n_estimators=100, n_jobs=None,
oob_score=False, random_state=None,
verbose=0, warm_start=False))],
verbose=False). Check the list of available parameters with `estimator.get_params().keys()`.
Run Code Online (Sandbox Code Playgroud)
尽管我已经查看了 scikit learn 文档和几篇文章,但我在代码中找不到错误。
当您使用管道时,GridSearchCV()必须在参数键中包含名称。只需用双下划线将名称与参数名称分开即可。在你的情况下:
param_grid = {
'rf__max_depth': [4, 5, 10],
'rf__max_features': [2, 3],
'rf__min_samples_leaf': [3, 4, 5],
'rf__n_estimators': [100, 200, 300]
}
Run Code Online (Sandbox Code Playgroud)
sklearn 文档中的示例:https://scikit-learn.org/stable/tutorial/statistical_inference/putting_together.html
| 归档时间: |
|
| 查看次数: |
13071 次 |
| 最近记录: |