【发布时间】:2014-01-22 14:32:10
【问题描述】:
我正在尝试使用 Python 的 Tfidf 来转换文本语料库。 但是,当我尝试 fit_transform 时,我得到一个值错误 ValueError: empty words;也许文档只包含停用词。
In [69]: TfidfVectorizer().fit_transform(smallcorp)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-69-ac16344f3129> in <module>()
----> 1 TfidfVectorizer().fit_transform(smallcorp)
/Users/maxsong/anaconda/lib/python2.7/site-packages/sklearn/feature_extraction/text.pyc in fit_transform(self, raw_documents, y)
1217 vectors : array, [n_samples, n_features]
1218 """
-> 1219 X = super(TfidfVectorizer, self).fit_transform(raw_documents)
1220 self._tfidf.fit(X)
1221 # X is already a transformed view of raw_documents so
/Users/maxsong/anaconda/lib/python2.7/site-packages/sklearn/feature_extraction/text.pyc in fit_transform(self, raw_documents, y)
778 max_features = self.max_features
779
--> 780 vocabulary, X = self._count_vocab(raw_documents, self.fixed_vocabulary)
781 X = X.tocsc()
782
/Users/maxsong/anaconda/lib/python2.7/site-packages/sklearn/feature_extraction/text.pyc in _count_vocab(self, raw_documents, fixed_vocab)
725 vocabulary = dict(vocabulary)
726 if not vocabulary:
--> 727 raise ValueError("empty vocabulary; perhaps the documents only"
728 " contain stop words")
729
ValueError: empty vocabulary; perhaps the documents only contain stop words
我在这里阅读了 SO 问题:Problems using a custom vocabulary for TfidfVectorizer scikit-learn 并尝试了 ogrisel 的建议,即使用 TfidfVectorizer(**params).build_analyzer()(dataset2) 来检查文本分析步骤的结果和这似乎按预期工作:下面的sn-p:
In [68]: TfidfVectorizer().build_analyzer()(smallcorp)
Out[68]:
[u'due',
u'to',
u'lack',
u'of',
u'personal',
u'biggest',
u'education',
u'and',
u'husband',
u'to',
还有什么我做错了吗?我正在喂它的语料库只是一根巨大的长字符串,中间有换行符。
谢谢!
【问题讨论】:
-
我遇到了同样的问题,从 v0.19 降级到 0.18
标签: python pandas scikit-learn tf-idf