【发布时间】:2021-09-23 14:59:56
【问题描述】:
与此问题类似 (ColumnTransformer fails with CountVectorizer in a pipeline) 我想在管道中使用 ColumnTransformer 将 CountVectorizer/HashingVectorizer 应用于具有文本功能的列。但我不只有一个文本功能,而是多个。如果我传递一个功能(而不是列表,就像另一个问题的解决方案中建议的那样)它工作正常,我该如何为多个做呢?
numeric_features = ['x0', 'x1', 'y0', 'y1']
categorical_features = []
text_features = ['text_feature', 'another_text_feature']
numeric_transformer = Pipeline(steps=[('scaler', StandardScaler())])
categorical_transformer = Pipeline(steps=[('encoder', OneHotEncoder())])
text_transformer = Pipeline(steps=[('hashing', HashingVectorizer())])
preprocessor = ColumnTransformer(transformers=[
('numeric', numeric_transformer, numeric_features),
('categorical', categorical_transformer, categorical_features),
('text', text_transformer, text_features)
])
steps = [('preprocessor', preprocessor),
('clf', SGDClassifier())]
pipeline = Pipeline(steps=steps)
pipeline.fit(X_train, y_train)
【问题讨论】:
标签: python scikit-learn pipeline