Python 使用scikit learn预测有趣的文章
我正试图建立一个算法,能够根据我之前喜欢的文章预测我是否会喜欢一篇文章 例如:Python 使用scikit learn预测有趣的文章,python,machine-learning,scikit-learn,cosine-similarity,Python,Machine Learning,Scikit Learn,Cosine Similarity,我正试图建立一个算法,能够根据我之前喜欢的文章预测我是否会喜欢一篇文章 例如: 我读了50篇文章,我喜欢10篇。我告诉我的节目我喜欢它们 然后会有20篇新文章。我的程序必须根据我以前喜欢的10篇文章,为每一篇新文章提供一个“喜欢的百分比” 我在这里找到了线索: 然后,将数据集中的第一个文档与数据集中的其他文档进行比较: >>> from sklearn.metrics.pairwise import linear_kernel >>> cosine_si
- 我读了50篇文章,我喜欢10篇。我告诉我的节目我喜欢它们
- 然后会有20篇新文章。我的程序必须根据我以前喜欢的10篇文章,为每一篇新文章提供一个“喜欢的百分比”
>>> from sklearn.metrics.pairwise import linear_kernel
>>> cosine_similarities = linear_kernel(tfidf[0:1], tfidf).flatten()
>>> cosine_similarities
array([ 1. , 0.04405952, 0.11016969, ..., 0.04433602,
0.04457106, 0.03293218])
对于我的例子,我想我将要做的是连接我的10篇文章的文本,运行TFIDFvectorier,然后将新的大向量与每一篇新文章进行比较
但我想知道如何进行比较:
- 大向量(10篇文章)与小向量或
- 小的和大的相比
p(word1|like) -- among all the articles I like, the probability of word1 appears
p(word2|like) -- among all the articles I like, the probability of word2 appears
...
p(wordn|like) -- among all the articles I like, the probability of wordn appears
p(word1|unlike) -- among all the articles I do not like, the prob of word1 appears
...
p(like) -- the portion of articles I like (should be 0.2 in your example)
p(unlike) -- the portion of articles I do not like. (0.8)
然后给出第51个新示例,您应该在其中找到所有看到的单词,例如,它只包含word2和word5naive bayes的优点之一是它只关心词汇表中的单词。即使大向量中90%以上的单词都不会出现在新向量中,这也不是问题,因为所有不相关的特征都会相互抵消,而不会影响结果。
问题是
只要比率大于1,您就可以预测文章为“like”。此外,如果您想提高识别“喜欢”文章的精度,可以通过将阈值比率值从1.0增加到更大的值来实现精确召回平衡。另一方面,如果你想增加召回率,你可以降低阈值等
有关文本域中朴素贝叶斯分类的进一步阅读,请参阅
此算法可以很容易地修改以进行在线学习,即,一旦用户“喜欢”或“不喜欢”一个新示例,就更新学习的模型。因为上面统计表中的每一项基本上都是标准化计数。只要保留每个计数(每个单词)和保存的总计数,就可以基于每个实例更新模型
为了将单词的tf-idf-weight用于朴素贝叶斯,我们将权重视为单词的计数。即,如果没有tf idf,则每个文档中的每个单词都计为1;使用tf-idf,文档中的单词将被计算为其tf-idf权重。然后用同样的公式得到朴素贝叶斯的概率。这个想法可以在这本书中找到。我认为应该接受tf idf权重作为输入数据
有关多项式nb,请参见注释:
多项式朴素贝叶斯分类器适合于分类
具有离散特征(例如,文本分类的字数)。
多项式分布通常需要整数特征计数。
然而,在实践中,tf idf等分数计数也可能起作用。
谢谢你,这是个很好的回答。现在我只需要和python建立一种联系。当你谈论一个词的概率时,它是“真实”概率,还是我可以使用tf idf?如果是tf-idf,很容易得到p(wordn | like)的概率。没有tf-idf,每个文档中的每个单词都计为1;使用tf-idf,文档中的单词将被计算为其tf-idf权重。然后用同样的公式得到朴素贝叶斯的概率。
p(word1|like) -- among all the articles I like, the probability of word1 appears
p(word2|like) -- among all the articles I like, the probability of word2 appears
...
p(wordn|like) -- among all the articles I like, the probability of wordn appears
p(word1|unlike) -- among all the articles I do not like, the prob of word1 appears
...
p(like) -- the portion of articles I like (should be 0.2 in your example)
p(unlike) -- the portion of articles I do not like. (0.8)
prob(like|51th article) p(like) x p(word2|like) x p(word5|like)
---------------------------- = -----------------------------------------
prob(unlike|51th article) p(unlike)xp(word2|unlike)xp(word5|unlike)