Reputation: 153
Why do I keep on getting this error? I try other codes too, but once it uses the get_feature_names_out
function it will pop out this error.
Below is my code:
from sklearn.datasets._twenty_newsgroups import fetch_20newsgroups
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.naive_bayes import MultinomialNB # fast to train and achieves a decent F-score
from sklearn import metrics
import numpy as np
def show_top10(classifier, vectorizer, categories):
feature_names = vectorizer.get_feature_names_out()
for i, category in enumerate(categories):
top10 = np.argsort(classifier.coef_[i])[-10:]
print("%s: %s" % (category, " ".join(feature_names[top10])))
newsgroups_train = fetch_20newsgroups(subset='train')
print(list(newsgroups_train.target_names))
cats = ['alt.atheism', 'sci.space', 'rec.sport.baseball', 'rec.sport.hockey']
newsgroups_train = fetch_20newsgroups(subset='train', categories=cats)
print(list(newsgroups_train.target_names))
print(newsgroups_train.filenames.shape)
vectorizer = TfidfVectorizer()
vectors = vectorizer.fit_transform(newsgroups_train.data)
print(vectors.shape)
Upvotes: 15
Views: 59062
Reputation: 676
When sklearn.__version__ <= 0.24.x
use following method
get_feature_names()
When sklearn.__version__ >= 1.0.x
use following method
get_feature_names_out()
Ref:
Upvotes: 40
Reputation: 10545
This is probably because you are using an older scikit-learn version than the one this code was written for.
get_feature_names_out
is a method of the class sklearn.feature_extraction.text.TfidfVectorizer
since scikit-learn 1.0. Previously, there was a similar method called get_feature_names
.
So you should update your scikit-learn package, or use the old method (not recommended).
Upvotes: 14