问题描述
我想在我的 scaler
中测试不同的 pipeline
,所以我创建了一个 class
,它可以将我想要的 scaler
(标准、MinMax 等)作为参数。 )
它工作正常。但我想在我的 param_grid
中指定在没有缩放器的情况下进行测试。我在我的管道估算器中添加了“passthrough”,但它不起作用,我收到以下错误:
AttributeError: 'str' 对象没有属性 'set_params'
我知道这可能与我的类的构建有关,但我找不到解决方法。
这是您可以运行以进行测试的代码。
# import dependencies
from sklearn.tree import DecisionTreeClassifier
from sklearn.pipeline import Pipeline
from sklearn.compose import ColumnTransformer
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import gridsearchcv
from sklearn.preprocessing import StandardScaler,MinMaxScaler,RobustScaler
from sklearn.datasets import load_breast_cancer
from sklearn.base import BaseEstimator,TransformerMixin
import pandas as pd
class ScalerSelector(BaseEstimator,TransformerMixin):
def __init__(self,scaler=StandardScaler()):
super().__init__()
self.scaler = scaler
def fit(self,X,y=None):
return self.scaler.fit(X)
def transform(self,y=None):
return self.scaler.transform(X)
data = load_breast_cancer()
features = data["data"]
target = data["target"]
data = pd.DataFrame(data['data'],columns=data['feature_names'])
col_names = data.columns.tolist()
# scaler and encoder options
my_scaler = ScalerSelector()
preprocessor = ColumnTransformer(transformers = [('numerical',my_scaler,col_names)
])
# combine the preprocessor with LogisticRegression() using Pipeline
full_pipeline = Pipeline(steps = [('preprocessor',preprocessor),('log_reg',LogisticRegression())
])
# set params combination I want to try
scaler_options = {'preprocessor': ['passthrough'],'preprocessor__numerical__scaler':[StandardScaler(),RobustScaler(),MinMaxScaler()]}
# : ['passthrough',ScalerSelector()]
# initialize gridsearchcv using full_pipeline as final estimator
grid_cv = gridsearchcv(full_pipeline,param_grid = scaler_options)
# fit the data
grid_cv.fit(data,target)
# best params :
grid_cv.best_params_
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)