ColumnTransformer fails with CountVectorizer/HashingVectorizer in a pipeline (multiple textfeatures)(ColumnTransformer失败,管道中有CountVectorizer/HashingVectorizer(多个文本功能))
本文介绍了ColumnTransformer失败,管道中有CountVectorizer/HashingVectorizer(多个文本功能)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
类似于此问题(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)
推荐答案
只需为每个文本功能使用单独的转换器。
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之前手动复制/克隆它。)
这篇关于ColumnTransformer失败,管道中有CountVectorizer/HashingVectorizer(多个文本功能)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
编程基础网
本文标题为:ColumnTransformer失败,管道中有CountVectorizer/HashingVectorizer(多个文本功能)
基础教程推荐
猜你喜欢
- 将 x 轴刻度更改为自定义字符串 2022-01-01
- 尝试制作WhatsApp机器人 2022-01-01
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
- Discord.py 缺少必需的参数 2022-01-01
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
- 用 Python 编写 Fortran 无格式文件 2022-01-01
