Python TfidfVectorizer 抛出:空词汇;也许文档只包含停用词”

问题描述 投票:0回答:5

我正在尝试使用Python的Tfidf来转换文本语料库。 但是,当我尝试对其进行 fit_transform 时,出现值错误 ValueError:emptyvocabulary;也许这些文档只包含停用词。

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问题:使用TfidfVectorizer scikit-learn的自定义词汇表的问题并尝试了ogrisel的使用TfidfVectorizer(**params).build_analyzer()(dataset2)的建议来检查文本分析步骤的结果 这似乎按预期工作:下面的代码片段:

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',

我还有什么做错的地方吗?我正在提供的语料库只是一个巨大的长字符串,中间有换行符。

谢谢!

python pandas scikit-learn tf-idf
5个回答
21
投票

我猜这是因为你只有一根绳子。尝试将其拆分为字符串列表,例如:

In [51]: smallcorp
Out[51]: 'Ah! Now I have done Philosophy,\nI have finished Law and Medicine,\nAnd sadly even Theology:\nTaken fierce pains, from end to end.\nNow here I am, a fool for sure!\nNo wiser than I was before:'

In [52]: tf = TfidfVectorizer()

In [53]: tf.fit_transform(smallcorp.split('\n'))
Out[53]: 
<6x28 sparse matrix of type '<type 'numpy.float64'>'
    with 31 stored elements in Compressed Sparse Row format>

4
投票

在0.12版本中,我们将最小文档频率设置为2,这意味着只有出现至少两次的单词才会被考虑。为了使您的示例正常工作,您需要设置

min_df=1
。从 0.13 开始,这是默认设置。 所以我猜你用的是 0.12,对吗?


0
投票

如果您坚持只有一个字符串,您也可以将单个字符串作为元组。而不是:

smallcorp = "your text"

你宁愿把它放在一个元组中。

In [22]: smallcorp = ("your text",)
In [23]: tf.fit_transform(smallcorp)
Out[23]: 
<1x2 sparse matrix of type '<type 'numpy.float64'>'
    with 2 stored elements in Compressed Sparse Row format>

0
投票

我也遇到了同样的问题。 将 int(nums) 列表转换为 str(nums) 列表没有帮助。 但我转换为:

['d'+str(nums) for nums in set] #where d is some letter which mention, we work with strings

这有帮助。


0
投票

可能发生这种情况的一种情况是当您的数据(pandas 列、列表或字符串)没有有用的文本时。这些值可能为空,也可能只包含数据中的特殊字符。

在这种情况下,请用占位符文本替换该空值。像这样的东西:

df['column_1'] = 'Some text'

或使用以下方法替换 na 值:

df['column_1'] = df['column_1'].fillna('Some text')
© www.soinside.com 2019 - 2024. All rights reserved.