我试图弄清楚如何在拟合多项朴素贝叶斯模型后计算增益统计量 G(w)。新版 Jurafsky 和 Martin,语音和语言处理,第 4 章“朴素贝叶斯和情感分类”的第 17 页描述了这个统计数据。
我可以在这里计算除 P(c_i|\bar{w}) 之外的所有内容。在文本中,他们指出
\bar{w} 表示文档不包含单词 w
如果每个文档中都存在这个词,比如下面的“Chinese”这个词的例子,该怎么办?在朴素贝叶斯的多项式变体中,单词可能性基于(平滑)计数,而不是出现或不出现。那么,具体来说,如何计算 P(c_i|\bar{w}) 呢?
为了说明,我展示了一个来自Manning、Raghavan 和 Schütze (2008) 的工作示例。信息检索导论。剑桥:剑桥大学出版社,第 13 章表 13.1。. 这使用了我的 R 包quanteda中的多项朴素贝叶斯分类器的实现,用于具体说明(但这不是 R 问题!)。
library("quanteda")
## Package version: 1.4.1
## Example from 13.1 of _An Introduction to Information Retrieval_
## https://nlp.stanford.edu/IR-book/pdf/irbookonlinereading.pdf
corp <- corpus(c(d1 = "Chinese Beijing Chinese",
d2 = "Chinese Chinese Shanghai",
d3 = "Chinese Macao",
d4 = "Tokyo Japan Chinese",
d5 = "Chinese Chinese Chinese Tokyo Japan"),
docvars = data.frame(train = factor(c("Y", "Y", "Y", "N", NA),
ordered = TRUE)))
dfmat <- dfm(corp, tolower = FALSE)
dfmat
## Document-feature matrix of: 5 documents, 6 features (60.0% sparse).
## 5 x 6 sparse Matrix of class "dfm"
## features
## docs Chinese Beijing Shanghai Macao Tokyo Japan
## text1 2 1 0 0 0 0
## text2 2 0 1 0 0 0
## text3 1 0 0 1 0 0
## text4 1 0 0 0 1 1
## text5 3 0 0 0 1 1
## replicate IIR p261 prediction for test set (document 5)
tmod <- textmodel_nb(dfmat, y = docvars(dfmat, "train"), prior = "docfreq", smooth = 1)
predict(tmod, newdata = dfmat[5, ], type = "prob")
## N Y
## text5 0.3102414 0.6897586
# word (smoothed) likelihoods
tmod$PwGc
## features
## classes Chinese Beijing Shanghai Macao Tokyo Japan
## N 0.2222222 0.1111111 0.1111111 0.1111111 0.22222222 0.22222222
## Y 0.4285714 0.1428571 0.1428571 0.1428571 0.07142857 0.07142857
# word posteriors by class
tmod$PcGw
## features
## classes Chinese Beijing Shanghai Macao Tokyo Japan
## N 0.1473684 0.2058824 0.2058824 0.2058824 0.5090909 0.5090909
## Y 0.8526316 0.7941176 0.7941176 0.7941176 0.4909091 0.4909091
任何帮助是极大的赞赏。
