【问题标题】:Scikit - TF-IDF empty vocabularyScikit - TF-IDF 空词汇
【发布时间】:2018-08-05 15:32:50
【问题描述】:

我必须计算两个或多个文本的距离/相似度。有些文本真的很小或不能形成正确的英文单词等,“A1024515”。这意味着它应该接受列表中的每个单词。

作为一个测试用例,我使用了以下列表作为语料库。

words= ['A', 'A', 'A']

vect = TfidfVectorizer(min_df =0)
dtm = vect.fit_transform(words)
df_tf_idf = pd.DataFrame(dtm.toarray(), columns=vect.get_feature_names())

但是,我收到以下错误

ValueError: empty vocabulary; perhaps the documents only contain stop words

如何确保列表被接受为可能的词,并确保不从语料库中删除停用词?

【问题讨论】:

  • 我不认为这是重复的,因为他们不处理停用词的删除。

标签: python scikit-learn nlp tf-idf tfidfvectorizer


【解决方案1】:

问题不在于停用词,默认情况下没有停用词。问题是您的测试用例中的句子太短(1 个字符)。

By default tfidfVectorizer uses r'(?u)\b\w\w+\b' to tokenize 给定的句子语料库到单词列表中。这不适用于单个字符串。

sklearn.feature_extraction.text.TfidfVectorizer(... token_pattern=’(?u)\b\w\w+\b’, ...)

您可以使用自己的正则表达式,将分词器作为构造函数参数(在这种情况下,给定的分词器会覆盖正则表达式)。或者使用更长、更真实的测试用例。

【讨论】:

    【解决方案2】:

    参考问题的答案:“CountVectorizer raise error on short words”:

    words= ['A', 'A', 'A']
    
    vect = TfidfVectorizer(token_pattern='(?u)\\b\\w+\\b')
    dtm = vect.fit_transform(words)
    
    vect.get_feature_names()
    

    给出输出:

    ['a']
    

    【讨论】:

      猜你喜欢
      • 2013-05-16
      • 2021-01-24
      • 2018-01-30
      • 1970-01-01
      • 2018-03-21
      • 2018-05-27
      • 2012-08-05
      • 2020-01-18
      • 2023-02-07
      相关资源
      最近更新 更多