gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 03:47:55 26 4
gpt4 key购买 nike

我尝试使用 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 版 后,它工作正常!!

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com