【问题标题】:How does TfidfVectorizer take his arguments?TfidfVectorizer 如何接受他的论点?
【发布时间】:2021-08-04 12:17:34
【问题描述】:

我想更好地了解 TfidfVectorizer 的工作原理。我不明白如何使用get_feature_name等后续功能

这是我的问题的可重现示例:

from sklearn.feature_extraction.text import TfidfVectorizer

text = ['It was a queer, sultry summer', 'the summer they electrocuted the Rosenbergs',
    'and I didn’t know what I was doing in New York', 'I m stupid about executions',
    'The idea of being electrocuted makes me sick',
    'and that’s all there was to read about in the papers',
    'goggle eyed headlines staring up at me on every street corner and at the fusty',
    'peanut-selling mouth of every subway', 'It had nothing to do with me',
    'but I couldn’t help wondering what it would be like',
    'being burned alive all along your nerves']


tfidf_vect = TfidfVectorizer(max_df=0.7,
                                 min_df= 0.01,
                                 use_idf=True,
                                 ngram_range=(1,2)) 

tfidf_mat = tfidf_vect.fit_transform(text)
print(tfidf_mat)
features = tfidf_vect.get_feature_names()
print(features)

在这个例子中,我认为我的对象tfidf_vect 定义了我想要应用TfidfVectorizer 的所有参数,然后我将其应用到text,以获得对象tfidf_mat 中的结果。

我不明白为什么,为了提取我的 tfidf 分析的附加信息,我将函数应用于对象 tfidf_vect 而不是 tfidf_mat

命令tfidf_vect.get_feature_names() 如何知道这将应用于text,如果它的定义中没有指定?

【问题讨论】:

    标签: python scikit-learn tf-idf tfidfvectorizer


    【解决方案1】:

    命令tfidf_vect.get_feature_names() 有效,因为tfidf_vect 是类TfidfVectorizer 的一个实例。此类具有某些属性(请参阅documentation)。这些属性在调用类的方法后可以改变,例如方法fit_transform。现在,get_feature_names 可以访问与 fit_transform 方法相同的类实例属性。您可能想了解更多关于classes、方法、属性等的信息。

    所以:tfidf_mat 只是保存了fit_transform() 的返回值(这是一个 (n_samples, n_features) 的稀疏矩阵)。调用fit_transform() 后,tfidf_vect 的属性发生了变化,可以通过该类实例的任何方法访问(get_feature_names() 也是如此)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-06
      • 2021-09-13
      • 2011-03-08
      • 2019-01-31
      • 2021-08-04
      • 2014-09-03
      • 1970-01-01
      • 2019-03-08
      相关资源
      最近更新 更多