【发布时间】:2018-01-04 00:52:53
【问题描述】:
我正在尝试将 sklearn 的 CountVectorizer 与给定的词汇一起使用。我的词汇是:
['humanitarian crisis', 'vacations for the anti-cruise crowd', 'school textbook', "b'cruise vacations for the anti-cruise", 'budget deal', "b'public school", 'u.n. announces', 'wrong petrol', 'vacations for the anti-cruise', "b'cruise vacations for the anti-cruise crowd"]
矢量化的输入取自 pandas 数据帧。我从带有pd.read_csv 和encoding='utf8' 的csv 中读到这个:
29371 b'9 quirky and brilliant paris boutiques'
20525 b'public school textbook filled with muslim bi...
2871 b'congress focuses on averting shutdown, but t...
29902 b'yarmouk siege: u.n. announces trip to syria ...
45596 b'fracking protesters arrested for gluing them...
6266 b'cruise vacations for the anti-cruise crowd'
调用CountVectorizer(vocabulary=vocabulary).fit_transform() 后,我得到一个全为零的矩阵:
(<6x10 sparse matrix of type '<type 'numpy.int64'>'
with 0 stored elements in Compressed Sparse Row format>, <class 'scipy.sparse.csr.csr_matrix'>)
这是因为字符串类型的问题,还是我调用 CountVectorizer 的问题?我不确定如何转换字符串类型;我在 python2.7 和 pandas 中尝试了多次不同的调用 encode 和 decode。任何建议将不胜感激。
【问题讨论】:
-
如果您将 iris 数据集转储到 csv 中,然后使用您的代码读取并拟合转换物种列,您会得到同样的错误吗?
-
显示完整代码。
vocabulary是什么?你如何将数据传递到fit_transform() -
Vocabulary 是
CountVectorizer学习的单个单词,或者当输入文档被空格 (' ') 分割时要使用的单词。所以我担心,您的词汇表(包含短语而不是单词)将不匹配给定数据中的任何单词,因此结果是 0 个元素。阅读有关词汇的工作原理。
标签: python python-2.7 pandas scikit-learn countvectorizer