【发布时间】:2020-03-18 07:59:30
【问题描述】:
我正在尝试通过功能联合将我的不同管道全部部署到一起,除了一个问题外,一切正常。
在我的 DataFrame 中,我有一个列 ID,我希望在所有管道中保持不变。 我必须把它交给管道,因为我应用了一些热编码和其他东西,我不能在最后把它合并回来。
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler, OneHotEncoder
from sklearn.impute import SimpleImputer
scaler_pipeline = Pipeline([
('selector', DataFrameSelector(col_scalar)),
('imputer', SimpleImputer(strategy="median")),
('std_scaler', StandardScaler())
])
one_hot_pipeline = Pipeline([
('selector', DataFrameSelector(col_one_hot)),
('imputer', SimpleImputer(strategy="most_frequent")),
('one_hot', OneHotEncoder())
])
full_pipeline = FeatureUnion(transformer_list=[
("DataFrameSelector", DataFrameSelector(immutable_col)),
("scaler_pipeline", scaler_pipeline),
("one_hot_pipeline", one_hot_pipeline),
])
我的 DataFrameSelector 是这样的:
class DataFrameSelector(BaseEstimator, TransformerMixin):
def __init__(self, attribute_names):
self.attribute_names = attribute_names
def fit(self, X, y=None):
return self
def transform(self, X):
return X[self.attribute_names]
在“full_pipeline”的开头,我想选择一些列(此处为 ID)并保持不变。
现在我得到这个错误
TypeError: 不支持类型转换:(dtype('O'), dtype('float64'), dtype('float64'))
【问题讨论】:
标签: python scikit-learn pipeline