So mistake was mine, I have followed an online tutorial for installing sklearn which get me version 0.10. As per the error I think stop_words is not supported in sklearn version 0.10. After updating it to version 0.14.1, it is working fine!!
__init__() got an unexpected keyword argument 'stop_words'
-
25-06-2023 - |
Question
I was trying to calculate tf-idf using scikit-learn version 0.14.1. and here is my code:
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()
I got this error:
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'
I don't understand what is the problem with 'stop_words', need help ?
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow