我是scikit-learn的新手。我需要计算大型语料库的 tf-idf 向量。但在开始之前,我尝试编写一些小文档,每个文档不超过 5-6 个单词。我编写的具体代码如下:
from sklearn.feature_extraction.text
import TfidfVectorizer
vectorizer = TfidfVectorizer(min_df=1)
vectors = vectorizer.fit_transform(docList)
它在我的笔记本电脑上运行良好,但是当我在服务器上运行时生成以下错误:
Traceback (most recent call last):
File "temp1.py", line 49, in <module>
tfidf_vectorizer.fit_transform(docList)
File "/usr/lib64/python2.6/site-packages/sklearn/feature_extraction/text.py", line 1285, in fit_transform
X = super(TfidfVectorizer, self).fit_transform(raw_documents)
File "/usr/lib64/python2.6/site-packages/sklearn/feature_extraction/text.py", line 825, in fit_transform
max_features)
File "/usr/lib64/python2.6/site-packages/sklearn/feature_extraction/text.py", line 697, in _limit_features
dfs = _document_frequency(X)
File "/usr/lib64/python2.6/site-packages/sklearn/feature_extraction/text.py", line 491, in _document_frequency
return bincount(X.indices, minlength=X.shape[1])
File "/usr/lib64/python2.6/site-packages/sklearn/utils/fixes.py", line 345, in bincount
return np.bincount(x, weights, minlength)
TypeError: function takes at most 2 arguments (3 given)
这是安装的 sklearn 版本有问题吗?我的笔记本电脑上安装了 0.17.1,服务器安装了 sklearn 0.16.1。由于我的语料库非常大,我必须在服务器上运行它,否则我自然会遇到内存问题。
对此问题的任何见解将不胜感激。谢谢:)
我认为这与此处报告的numpy安装错误有关。
您可以运行测试并检查(需要鼻子):
pip install nose
nosetests -v numpy
nosetests -v sklearn
如果是这种情况,您需要删除numpy(也许还有scikit-learn)并使用推荐的说明重新安装它们:http://scikit-learn.org/stable/install.html
使用 conda 也是一个不错的选择:https://www.continuum.io/downloads