已解决(sklearn运行报错)AttributeError: ‘TfidfVectorizer‘ object has no attribute ‘get_feature_names_out‘
报错代码
粉丝群里面的一个小伙伴想用sklearn中的TfidfVectorizer库用来计算TF-IDF值,但是发生了报错(当时他心里瞬间凉了一大截,跑来找我求助,然后顺利帮助他解决了,顺便记录一下希望可以帮助到更多遇到这个bug不会解决的小伙伴),报错代码如下::
from sklearn.feature_extraction.text import TfidfVectorizer
corpus = [
'This is the first document.',
'This document is the second document.',
'And this is the third one.',
'Is this the first document?',
]
vectorizer = TfidfVectorizer()
X = vectorizer.fit_transform(corpus)
print(vectorizer.get_feature_names_out())
print(X.shape)
报错信息如下:
AttributeError: 'TfidfVectorizer' object has no attribute 'get_feature_names_out'
报错翻译
报错信息翻译:
属性错误 :“TfidfVectorizer”对象没有属性“get_feature_names_out”
报错原因
报错原因:
粉丝电脑上的sklearn版本太老了,但是他在网上的复制的语法却是最新的,所以语法并不支持使用。小伙伴按下面的两种方法任选其一即可!!!
解决方法
解决方法1(修改为老版本的语法):
print(vectorizer.get_feature_names())
解决方法2(pip升级sklearn的版本):
pip install --upgrade sklearn
任选其一解决方法运行代码成功:
帮忙解决
本文已收录于:《告别Bug》专栏
本专栏用于记录学习和工作中遇到的各种疑难Bug问题,以及粉丝群里小伙伴提出的各种问题,文章形式:报错代码 + 报错翻译 + 报错原因 + 解决方法,包括程序安装、运行程序过程中等等问题,订阅专栏+关注博主后如遇到其他问题可私聊帮忙解决!!!