【发布时间】:2013-09-25 14:14:50
【问题描述】:
我正在尝试使用 scikit-learn 进行一些文本分析。但是,当我尝试调用 CountVectorizer 时,会引发错误。示例代码和引发的错误如下:
>>> from sklearn.feature_extraction.text import CountVectorizer
>>> corpus = [ 'This is the first document.', 'This is the second second document.', 'And the third one.', 'Is this the first document?', ]
>>> vectorizer = CountVectorizer(min_df=1)
>>> X = vectorizer.fit_transform(corpus)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Python/2.6/site-packages/sklearn/feature_extraction/text.py", line 789, in fit_transform
vocabulary, X = self._count_vocab(raw_documents, self.fixed_vocabulary)
File "/Library/Python/2.6/site-packages/sklearn/feature_extraction/text.py", line 716, in _count_vocab
vocabulary = defaultdict(None)
TypeError: first argument must be callable
这是我安装的错误还是什么?其他示例运行良好。
【问题讨论】:
-
我无法在 Python 2.7 上使用最新的 scikit-learn 重现这一点,而且我的 Python 2.6.6 对
defaultdict(None)也没有问题。你的确切 Python 版本是什么? -
Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49) [GCC 4.2.1 (Apple Inc. build 5646)] 在 darwin 上
-
python -c 'from collections import defaultdict as d; d(None)是否给出错误信息? -
我应该将 Python 升级到更新的版本吗?已经安装的模块会发生什么?
-
刚刚编译 Python 2.7.5 并成功运行
defaultdict。CountVectorizer现在应该可以工作了。谢谢
标签: python scikit-learn