【问题标题】:how to selected vocabulary in scikit CountVectorizer如何在 scikit CountVectorizer 中选择词汇
【发布时间】:2016-07-02 22:52:06
【问题描述】:

我使用 scikit CountVectorizer 将文档集合转换为令牌计数矩阵。我还使用了它的 max_features,它考虑了整个语料库中按词频排序的最高 max_features。

现在我想分析我选择的语料库,特别是我想知道所选词汇表中标记的频率。但我无法找到一种简单的方法来做到这一点。所以请在这方面帮助我。

【问题讨论】:

    标签: python scikit-learn term-document-matrix


    【解决方案1】:

    在@bernard post的帮助下,我完全可以得到结果,如下:

    vec = CountVectorizer()
    doc_term_matrix = vec.fit_transform(['toto titi', 'toto toto', 'titi tata'])
    doc_term_matrix = doc_term_matrix.toarray()
    term_freq_matrix = doc_term_matrix.sum(0)
    min_freq = np.amin(term_freq_matrix)
    indices_name_mapping = vec.get_feature_names()
    feature_names = [indices_name_mapping[i] for i, x in enumerate(term_freq_matrix) if x == min_freq]
    

    【讨论】:

      【解决方案2】:

      当您调用fit_transform() 时,将返回一个稀疏矩阵。

      要显示它,您只需调用 toarray() 方法。

      vec = CountVectorizer()
      spars_mat = vec.fit_transform(['toto titi', 'toto toto', 'titi tata'])
      
      #you can observer the matrix in the interpretor by doing
      spars_mat.toarray()
      

      【讨论】:

        猜你喜欢
        • 2020-01-15
        • 2019-03-14
        • 2014-07-13
        • 2016-10-18
        • 2016-09-25
        • 2015-05-07
        • 2015-12-16
        • 2015-02-13
        • 2018-12-12
        相关资源
        最近更新 更多