AttributeError: getfeature_names not found ; using scikit-learn

天涯浪子 提交于 2019-12-06 04:51:23

I see two problems with your code. First, you are applying get_feature_names() to your matrix output, rather than to the vectorizer. You need to apply it to the vectorizer. Second, you are unnecessarily breaking this apart into too many steps. You can use TfidfVectorizer.fit_transform() to do what you want in much less space. Try this:

from sklearn.feature_extraction.text import TfidfVectorizer

vectorizer = TfidfVectorizer()
transformed = vectorizer.fit_transform(word_data)
print "Num words:", len(vectorizer.get_feature_names())

Is it not get_feature_names(), ie. with an underscore after 'get'.

Also, I am not sure what you are trying to do, but get_feature_names is a method valid only for *Vectorizer classes, not with the TfidTransformer. Maybe you want TfidVectorizer instead?

from sklearn.feature_extraction.text import TfidfVectorizer
TfIdfer = TfidfVectorizer(stop_words = 'english')
TfIdfer.fit_transform(word_data).toarray()
names = TfIdfer.get_feature_names()
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!