python - NLP:将 CountVectorizer 应用于包含特征列表的列
问题描述
我想申请CountVectorizer
包含单词和短语列表的列。换句话说,语料库不是一个字符串,而是一个列表。问题是CountVectorizer
我遇到的任何其他相关函数都需要一个字符串作为输入。将列表加入一个字符串并进行标记是没有意义的,因为某些短语包含 2 个单词。有任何想法吗?
例子:
ID corpus
1 ["Harry Potter","Batman"]
2 ["Batman", "Superman", "Lord of the Rings"]
期望的结果:
ID Harry Potter Batman Superman Lord of the Rings
1 1 1 0 0
2 0 1 1 1
解决方案
由于您已经对句子进行了标记,CountVectorizer
因此可能不需要这样做。
我在MultiLabelCounter()
这里写了一个,可以解决你的问题。
import pandas as pd
x = [["Harry Potter","Batman"], ["Batman", "Superman", "Lord of the Rings"]]
mlc = MultiLabelCounter()
mlc.fit_transform(x)
# [[1, 1, 0, 0], [1, 0, 1, 1]]
mlc.classes_
# ['Batman', 'Harry Potter', 'Lord of the Rings', 'Superman']
推荐阅读
- javascript - 用javascript提取2个字符之间的字符串
- android - 部分设备无法读取 NfcA 卡
- ios - Swift - 更改 iPhone 背景
- python - 如何将环境变量传递给我的烧瓶应用程序
- ios - 如何从超级视图中获取所有元素(如:UiLabel,UITextfield)并设置标签文本颜色和文本字段占位符颜色
- excel - 为什么列表框不显示 Excel VBA 用户窗体中的最新条目?
- react-native - Redux React-Native:更新状态后未定义的对象
- c++ - "int* p=+s;" 是什么意思?做?
- jquery - 获取对预检请求的响应未通过访问控制检查错误 - Spring Security +Oauth2 Rest Api
- java - 使用流收集到 Map