我试图使用scikit-learn版本0.14.1来计算tf-idf。这是我的代码:
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import TfidfTransformer
from nltk.corpus import stopwords
import numpy as np
import numpy.linalg as LA
train_set = ["The sky is blue.", "The sun is bright."] #Documents
test_set = ["The sun in the sky is bright sun."] #Query
stopWords = stopwords.words('english')
vectorizer = CountVectorizer(stop_words = stopWords)
#print vectorizer
transformer = TfidfTransformer()
#print transformer
trainVectorizerArray = vectorizer.fit_transform(train_set).toarray()
testVectorizerArray = vectorizer.transform(test_set).toarray()
print 'Fit Vectorizer to train set', trainVectorizerArray
print 'Transform Vectorizer to test set', testVectorizerArray
transformer.fit(trainVectorizerArray)
print
print transformer.transform(trainVectorizerArray).toarray()
transformer.fit(testVectorizerArray)
print
tfidf = transformer.transform(testVectorizerArray)
print tfidf.todense()
我收到了这个错误:
Traceback (most recent call last):
File "tfidf.py", line 12, in <module>
vectorizer = CountVectorizer(stop_words = stopWords)
TypeError: __init__() got an unexpected keyword argument 'stop_words'
我不明白'stop_words'有什么问题,需要帮助吗?
答案 0 :(得分:2)
错误是我的,我已经按照安装 sklearn 的在线教程获得了0.10版本。根据错误,我认为sklearn版本0.10不支持stop_words。 将其更新为版本0.14.1 后,它工作正常!!