首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ColumnTransformer在管道中使用CountVectorizer/HashingVectorizer失败(多个文本特性)

ColumnTransformer在管道中使用CountVectorizer/HashingVectorizer失败(多个文本特性)
EN

Stack Overflow用户
提问于 2021-07-14 15:53:57
回答 1查看 191关注 0票数 0

类似于这个问题(ColumnTransformer fails with CountVectorizer in a pipeline),我希望在具有文本特性的列上应用CountVectorizer/HashingVectorizer,使用管道中的ColumnTransformer。但是我没有一个文本功能,而是多个。如果我传递一个特性(不像解决另一个问题的方法中所建议的列表那样),它可以正常工作,如何对多个特性进行传递?

代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-07-14 16:28:28

只需为每个文本特性使用单独的转换器即可。

代码语言:javascript
复制
preprocessor = ColumnTransformer(transformers=[
    ('numeric', numeric_transformer, numeric_features), 
    ('categorical', categorical_transformer, categorical_features),
    ('text', text_transformer, 'text_feature'),
    ('more_text', text_transformer, 'another_text_feature'),
])

(变压器在安装过程中被克隆,所以您将有两个独立的text_transformer副本,一切都很好。如果需要像这样两次指定相同的转换器,则可以在指定ColumnTransformer之前手动复制/克隆它。)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68381331

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档