Skip to main content
 首页 » 编程设计

python之__init__() 得到了一个意外的关键字参数 'stop_words'

2024年11月01日9mengfanrong

我尝试使用 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' 

我不明白“停用词”有什么问题,需要帮助吗?

请您参考如下方法:

所以错误是我的,我已经按照在线教程安装了 sklearn,这让我得到了 0.10 版。根据错误,我认为 sklearn 版本 0.10 不支持 stop_words。 将其更新到0.14.1 版 后,它工作正常!!