【问题标题】:tfidfvectorizer prints results based on all wordstfidfvectorizer 根据所有单词打印结果
【发布时间】:2017-08-25 11:03:58
【问题描述】:

虽然有六个不同的词。结果中只打印了 5 个单词。如何根据所有单词(6列向量)得到结果?

from sklearn.feature_extraction.text import TfidfVectorizer
sent=["This is a sample", "This is another example"]
tf = TfidfVectorizer(analyzer='word', ngram_range=(1,1), min_df = 0)
tfidf_matrix =  tf.fit_transform(sent)
print tfidf_matrix.toarray()

[[ 0. 0. 0.50154891 0.70490949 0.50154891] [ 0.57615236 0.57615236 0.40993715 0.0.40993715]]

还有如何打印列详细信息(特征(单词))和行(文档)?

【问题讨论】:

    标签: python scikit-learn nlp tf-idf


    【解决方案1】:

    您正在使用默认的 token_pattern,它只选择 2 个或更多字符的标记。

    token_pattern:

    “token”,仅在 Analyzer == 'word' 时使用。默认正则表达式选择 2 个或更多字母数字字符的标记(标点符号完全 忽略并始终视为标记分隔符)

    如果你定义一个新的token_pattern,你会得到'a'字符,例如:

    from sklearn.feature_extraction.text import TfidfVectorizer
    sent=["This is a sample", "This is another example"]
    tf = TfidfVectorizer(token_pattern=u'(?u)\\b\\w+\\b')
    tfidf_matrix =  tf.fit_transform(sent)
    print tfidf_matrix.toarray()
    tf.vocabulary_
    

    [[ 0.57615236 0. 0. 0.40993715 0.57615236 0.40993715] [0.0.57615236 0.57615236 0.40993715 0.0.40993715]]

    tf.vocabulary_
    

    {u'a': 0, u'sample': 4, u'another': 1, u'this': 5, u'is': 3, u'example': 2}

    【讨论】:

      猜你喜欢
      • 2022-11-25
      • 2021-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多