python - __init__() 获得意外的关键字参数 'stop_words'

标签 python scikit-learn tf-idf stop-words

我尝试使用 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”有什么问题,需要帮助吗?

最佳答案

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

关于python - __init__() 获得意外的关键字参数 'stop_words',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22790764/

相关文章:

python - 将二维列表(tfidf 结果的密集输出)附加到 pandas 数据帧行中,每个索引

Python和tfidf算法,让它更快?

python - 使用 ibm-cos-sdk 时出现意外的关键字参数 'ibm_api_key_id'?

python - 用Python中的值更新字典列表中所有匹配的键

python - 多元线性回归和列误差选择

python-2.7 - Scikit-learn KNN 中的归一化

scikit-learn - 导入错误 : cannot import name 'GaussianProcess' from 'sklearn.gaussian_process'

python - 为什么 TF-IDF 计算需要这么长时间?

python - 是否有与 __import__ 等效的 __reload__ 函数?

python - 在 Reddit API 中访问列表结果的下一页