【问题标题】:how to use tf-idf with Naive Bayes?如何将 tf-idf 与朴素贝叶斯一起使用?
【发布时间】:2016-09-21 04:44:15
【问题描述】:

根据我在此处发布的有关查询的搜索,我有许多链接提出了解决方案,但没有具体说明如何完成。例如,我探索了以下链接:

Link 1

Link 2

Link 3

Link 4

等等。

因此,我在这里提出我对如何使用带有 tf-idf 的朴素贝叶斯公式的理解,具体如下:

朴素贝叶斯公式:

P(word|class)=(word_count_in_class + 1)/(total_words_in_class+total_unique_words_in_all_classes(basically vocabulary of words in the entire training set))

tf-idf 权重可以在上面的公式中使用为:

word_count_in_class : sum of(tf-idf_weights of the word for all the documents belonging to that class) //basically replacing the counts with the tfidf weights of the same word calculated for every document within that class.

total_words_in_class : sum of (tf-idf weights of all the words belonging to that class) 

total_unique_words_in_all_classes : as is.

这个问题已经在堆栈溢出上多次发布,但到目前为止还没有任何实质性的答案。我想知道我思考问题的方式是否正确,即我上面展示的实现。我需要知道这一点,因为我自己实现了朴素贝叶斯,而不需要任何 Python 库的帮助,该库带有朴素贝叶斯和 tf-idf 的内置函数。我真正想要的是提高使用朴素贝叶斯训练分类器的模型的准确性(目前为 30%)。因此,如果有更好的方法来实现良好的准确性,欢迎提出建议。

请给我建议。我是这个领域的新手。

【问题讨论】:

    标签: python-2.7 tf-idf naivebayes


    【解决方案1】:

    如果您真的向我们提供了您想要使用的确切功能和类别,或者至少给出一个示例,那就更好了。由于这些都没有具体给出,我只是假设以下是你的问题:

    1. 您有许多文档,每个文档都有多个单词。
    2. 您希望将文档分类。
    3. 您的特征向量由所有文档中所有可能的单词组成,并且具有每个文档中的计数值。

    您的解决方案

    你给的tf idf如下:

    word_count_in_class : sum of(tf-idf_weights of the word for all the documents belonging to that class) //basically replacing the counts with the tfidf weights of the same word calculated for every document within that class.
    
    total_words_in_class : sum of (tf-idf weights of all the words belonging to that class)
    

    您的方法听起来很合理。所有概率之和总和为 1,与 tf-idf 函数无关,并且特征将反映 tf-idf 值。我会说这看起来像是将 tf-idf 合并到 NB 中的可靠方法。

    另一种可能的解决方案

    我花了一段时间才解决这个问题。造成这种情况的主要原因是不得不担心维持概率标准化。使用高斯朴素贝叶斯将有助于完全忽略这个问题。

    如果你想使用这个方法:

    1. 计算每个类的 tf-idf 值的平均值和变化。
    2. 使用由上述均值和变化生成的高斯分布计算先验。
    3. 照常进行(乘以先前)并预测值。

    硬编码这不应该太难,因为 numpy 固有地具有高斯函数。对于这类问题,我只是更喜欢这种类型的通用解决方案。

    其他增加方法

    除了上述之外,您还可以使用以下技术来提高准确性:

    1. 预处理:

      1. 特征缩减(通常是 NMF、PCA 或 LDA)
      2. 附加功能
    2. 算法:

      朴素贝叶斯速度很快,但本质上比其他算法的性能更差。进行特征缩减可能会更好,然后切换到判别模型,例如 SVM 或 Logistic Regression

    3. 杂项

      Bootstrapping、boosting 等。但请注意不要过度拟合...

    希望这会有所帮助。有什么不清楚的地方可以留言

    【讨论】:

      【解决方案2】:

      P(单词|类)=(word_count_in_class+1)/(total_words_in_class+total_unique_words_in_all_classes (基本上是整个训练集中单词的词汇))

      这个总和如何等于 1?如果使用上述条件概率,我假设 SUM 是

      P(word1|class)+P(word2|class)+...+P(wordn|class) = (total_words_in_class + total_unique_words_in_class)/(total_words_in_class+total_unique_words_in_all_classes)

      为了纠正这个问题,我认为 P(word|class) 应该像

      (word_count_in_class + 1)/(total_words_in_class+total_unique_words_in_classes(vocabulary of words in class))
      

      如果我错了,请纠正我。

      【讨论】:

        【解决方案3】:

        我认为有两种方法可以做到:

        1. 将 tf-idf 向下舍入为整数,然后对条件概率使用多项分布。请参阅本文https://www.cs.waikato.ac.nz/ml/publications/2004/kibriya_et_al_cr.pdf。
        2. 使用 Dirichlet 分布,它是条件概率的多项分布的连续版本。

        我不确定高斯混合是否会更好。

        【讨论】:

          猜你喜欢
          • 2021-07-14
          • 2020-06-26
          • 2012-02-21
          • 2011-12-28
          • 2017-04-12
          • 2013-12-02
          • 2013-09-09
          • 2015-01-03
          • 2017-02-09
          相关资源
          最近更新 更多