__init__() 收到意外的关键字参数 'stop_words

2 投票
1 回答
3468 浏览
提问于 2025-04-18 00:52

我在用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)'出了什么问题,能帮帮我吗?

1 个回答

3

所以这个错误是我自己的问题,我跟着一个网上的教程安装了sklearn,结果安装到了0.10版本。根据错误信息来看,我觉得在0.10版本中不支持stop_words这个功能。后来我把它更新到了0.14.1版本,现在一切都正常了!!

撰写回答