Python:CountVectorizer 忽略一个字母单词 "I"

标签 python scikit-learn countvectorizer

我有一个名为 dictionary1 的列表。我使用以下代码获取文本的稀疏计数矩阵:

cv1 = sklearn.feature_extraction.text.CountVectorizer(stop_words=None)  
cv1.fit_transform(dictionary1)

不过我注意到

list(set(dictionary1)-set(cv1.get_feature_names()))

结果在 ['i'] 中。所以“i”在我的字典中,但 CountVectorizer 忽略了它(可能是某些默认设置丢弃了一个字符的单词)。在documentation我找不到这样的选项。有人可以指出我的问题吗?事实上,我想在我的分析中保留“i”,因为它可以指代更个人化的语言。

最佳答案

一个可行的解决方法是直接将字典作为词汇传递(实际上我不知道为什么我一开始不这样做)。即

cv1 = sklearn.feature_extraction.text.CountVectorizer(stop_words=[], vocabulary=dictionary1)
cv1._validate_vocabulary()

list(set(dictionary1)-set(cv1.get_feature_names())) 然后返回 []

在我原来的帖子中,我应该提到 dictionary1 已经是唯一标记的列表。

关于Python:CountVectorizer 忽略一个字母单词 "I",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51497938/

相关文章:

python - 如何在 Python 中编写有效的类装饰器?

python - 在 Python 中将字符串列表转换为字典

python-2.7 - 如何计算骰子系数以测量python中图像分割的准确性

python - scikit-learn CountVectorizer。词汇_

pandas - CountVectorizer 方法 get_feature_names() 生成代码,但不生成单词

python - 如何在 Python 2.7 中复制类及其列表成员而不复制引用?

python - 使用 Scikit-learn 中的 Column Transformer 时如何找出 StandardScaling 参数 .mean_ 和 .scale_?

python - 如何绘制仅具有一个特征的 svm 超平面

python - sklearn模型数据转换错误: CountVectorizer - Vocabulary wasn't fitted

python utf-8列表和utf-8字符串的交集