gpt4 book ai didi

r - naiveBayes 在使用非零拉普拉斯参数时给出意外结果(包 e1071)

转载 作者:行者123 更新时间:2023-11-30 08:52:24 24 4
gpt4 key购买 nike

我正在尝试使用 e1071 中的 naiveBayes() 函数包裹。当我添加非零laplace参数时,我得到的概率估计没有改变,我不明白为什么。

示例:

library(e1071)

# Generate data
train.x <- data.frame(x1=c(1,1,0,0), x2=c(1,0,1,0))
train.y <- factor(c("cat", "cat", "dog", "dog"))
test.x <- data.frame(x1=c(1), x2=c(1))

# without laplace smoothing
classifier <- naiveBayes(x=train.x, y=train.y, laplace=0)
predict(classifier, test.x, type="raw") # returns (1, 0.00002507)

# with laplace smoothing
classifier <- naiveBayes(x=train.x, y=train.y, laplace=1)
predict(classifier, test.x, type="raw") # returns (1, 0.00002507)

我预计在这种情况下概率会发生变化,因为“狗”类的所有训练实例的 x1 均为 0。要检查这一点,请使用 Python 进行同样的操作

Python 示例:

import numpy as np
from sklearn.naive_bayes import BernoulliNB

train_x = pd.DataFrame({'x1':[1,1,0,0], 'x2':[1,0,1,0]})
train_y = np.array(["cat", "cat", "dog", "dog"])
test_x = pd.DataFrame({'x1':[1,], 'x2':[1,]})

# alpha (i.e. laplace = 0)
classifier = BernoulliNB(alpha=.00000001)
classifier.fit(X=train_x, y=train_y)
classifier.predict_proba(X=test_x) # returns (1, 0)

# alpha (i.e. laplace = 1)
classifier = BernoulliNB(alpha=1)
classifier.fit(X=train_x, y=train_y)
classifier.predict_proba(X=test_x) # returns (.75, .25)

为什么我使用 e1071 会得到这个意想不到的结果?

最佳答案

拉普拉斯估计仅对分类特征有效,对数字特征无效。在源码中可以找到:

## estimation-function
est <- function(var)
if (is.numeric(var)) {
cbind(tapply(var, y, mean, na.rm = TRUE),
tapply(var, y, sd, na.rm = TRUE))
} else {
tab <- table(y, var)
(tab + laplace) / (rowSums(tab) + laplace * nlevels(var))
}

对于数值,使用高斯估计。因此,您可以将数据转换为因子,然后就可以开始了。

train.x <- data.frame(x1=c("1","1","0","0"), x2=c("1","0","1","0"))
train.y <- factor(c("cat", "cat", "dog", "dog"))
test.x <- data.frame(x1=c("1"), x2=c("1"))

# without laplace smoothing
classifier <- naiveBayes(x=train.x, y=train.y, laplace=0)
predict(classifier, test.x, type="raw") # returns (100% for dog)

# with laplace smoothing
classifier <- naiveBayes(x=train.x, y=train.y, laplace=1)
predict(classifier, test.x, type="raw") # returns (75% for dog)

关于r - naiveBayes 在使用非零拉普拉斯参数时给出意外结果(包 e1071),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36875905/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com