python-3.x - 如何在 Scikit-Learn 中获取 GridSearchCV() 的 OneVsRestClassifier(LinearSVC()) 的估计器键引用?
问题描述
我正在对GridSearchCV
scikit-learn 中的超参数进行网格搜索。
这就是我准备 ML 算法及其要搜索的相关参数的方式。LogisticRegression()
和RandomForestClassifier()
分别用它们正确的估计键和logisticregression__
指定randomforestclassifier__
。
ml_algo_param_dict = \
{ 'LR_OVR': {'clf': LogisticRegression(),
'param': [{
'logisticregression__solver': ['lbfgs', 'liblinear'],
'logisticregression__penalty': ['l2'],
'logisticregression__C': [0.1, 1, 10],
'logisticregression__class_weight': [None],
'logisticregression__multi_class': ['ovr'],
'logisticregression__max_iter': [1000, 4000],
}, {
'logisticregression__solver': ['newton-cg'],
'logisticregression__penalty': ['l2'],
'logisticregression__C': [0.1, 1, 10],
'logisticregression__class_weight': [None],
'logisticregression__multi_class': ['ovr'],
'logisticregression__max_iter': [1000, 4000],
}]},
'RF_OVR': {'clf': RandomForestClassifier(),
'param': [{
'randomforestclassifier__n_estimators': [100],
'randomforestclassifier__max_depth': [150, 200],
'randomforestclassifier__random_state': [888],
}]},
'SVC_OVR': {'clf': OneVsRestClassifier(LinearSVC()),
'param': [{
'onevsrestclassifier_linearsvc__C': [100],
'onevsrestclassifier_linearsvc__max_iter': [400, 6000],
}]},
但是呢OneVsRestClassifier(LinearSVC())
?我尝试了很多方法(即,,,onevsrestclassifier_linearsvc__
)onevsrestclassifier__
,linearsvc__
但不断收到错误Check the list of available parameters with estimator.get_params().keys()
。如何找到正确的估算器密钥?
添加了以下代码以显示如何使用 dict
transformer_num = Pipeline(steps=[
('imputer', SimpleImputer(strategy='median')),
('scaler', StandardScaler())])
transformer_cat = Pipeline(steps=[
('imputer', SimpleImputer(strategy='constant', fill_value='')),
('onehotencoder', OneHotEncoder(handle_unknown='ignore'))])
preprocessor = ColumnTransformer(
transformers=[
('num', transformer_num, feature_list_num),
('cat', transformer_cat, feature_list_cat),
])
for algo_key, algo_val in ml_algo_param_dict.items():
f1 = make_scorer(f1_score , average='micro')
pipe = make_pipeline(preprocessor, algo_val['clf'])
grid = GridSearchCV(pipe, algo_val['param'], n_jobs=-1, cv=5, scoring=f1, refit=True)
grid.fit(X_train, y_train)
我试过'onevsrestclassifier_linearsvc__C', onevsrestclassifier_linearsvc_estimator__C', 'onevsrestclassifier__C', 'linearsvc__C', 'onevsrestclassifier__linearsvc__C', 'onevsrestclassifier-linearsvc__C', 'onevsrestclassifier_linearsvc_estimator__C', 'estimator__C'
了,但都给了我同样的错误Check the list of available parameters with "estimator.get_params().keys()"
。
解决方案
以下是正常工作的引用命名:
'SVC_OVR': {'clf': OneVsRestClassifier(LinearSVC()),
'param': [{
'onevsrestclassifier__estimator__C': [1, 10],
'onevsrestclassifier__estimator__max_iter': [10000],
}]},
推荐阅读
- sql - 在插入数据行时创建触发器并检查具有给定范围内的值的特定列
- elixir - 如何在 Elixir Credo 中匹配两条线
- javascript - 我无法使用 css 放大对象```div``` html
- graph - 古道的解决方案加一个超级节点有什么用
- sql - 根据以往任务的表现寻找最佳工作团队
- react-icons - 无法更改反应图标中的 GR 颜色
- automated-tests - Pact 部署后合同验证
- ios - 如何定位 SCNCamera 以使对象“刚好适合”视图?
- dropdown - 带有exams2moodle的多项选择题的下拉菜单
- php - 如何在 PHP 中通过 OTP 添加 Aadhaar 身份验证 API