python 属性错误:"TfidfVectorizer"对象没有属性"get_feature_names_out"

mqkwyuun  于 2023-01-19  发布在  Python
关注(0)|答案(2)|浏览(1841)

为什么我总是得到这个错误?我也尝试了其他代码,但是一旦它使用get_feature_names_out函数,它就会弹出这个错误。
下面是我的代码:

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)
epggiuax

epggiuax1#

这可能是因为您使用的scikit-learn版本比编写此代码时使用的版本旧。
get_feature_names_out是scikit-learn 1.0版本中sklearn.feature_extraction.text.TfidfVectorizer类的一个方法,以前有一个类似的方法叫做get_feature_names
所以你应该更新你的scikit-learn软件包,或者使用旧的方法(不推荐)。

ctehm74n

ctehm74n2#

sklearn.__version__ <= 0.24.x时,使用以下方法

get_feature_names()

sklearn.__version__ >= 1.0.x时,使用以下方法

get_feature_names_out()

参考:

  1. https://github.com/scikit-learn/scikit-learn/blob/0.24.X/sklearn/feature_extraction/text.py

相关问题