|
ValueError: Invalid parameter XXX for estimator Pipeline Check the list of available parameters with `estimator.get_params().keys()
在使用Pipeline与GridSearchCV进行网格搜索调参时出现错误
代码如下:
- from sklearn.model_selection import GridSearchCV
- def poly_logistic_reg():
- return Pipeline(steps=[
- ('poly', PolynomialFeatures()),
- ('std_scaler', StandardScaler()),
- ('log_reg', LogisticRegression())
- ])
- param_grid = [
- {
- 'degree': [d for d in range(1, 21)],
- 'C': [0.001, 0.01, 0.1, 1.0, 5.0, 10.0, 100.0],
复制代码
原因是:pipeline的目的:当设置不同的参数时可以组合几个交叉验证的步骤。
可以使用它们的名字和属性参数来设置不同步骤的参数(需要在参数前面加__(两个下划线)来连接)
修改为:
- from sklearn.model_selection import GridSearchCV
- def poly_logistic_reg():
- return Pipeline(steps=[
- ('poly', PolynomialFeatures()),
- ('std_scaler', StandardScaler()),
- ('log_reg', LogisticRegression())
- ])
- param_grid = [
- {
- 'poly__degree': [d for d in range(1, 21)],
- 'log_reg__C': [0.001, 0.01, 0.1, 1.0, 5.0, 10.0, 100.0],
- 'log_reg__penalty': ['l1', 'l2']
- }
- ]
- poly_log_regularization_gs = GridSearchCV(estimator=poly_logistic_reg(), param_grid=param_grid, cv=5, n_jobs=-1)
复制代码
|
|