我使用R 中的kernlabksvm
包来预测概率,使用. 但是,我发现有时使用不会产生由 给出的最高概率的类。type="probabilities"
predict.ksvm
predict(model,observation,type="r")
predict(model,observation,type="p")
例子:
> predict(model,observation,type="r")
[1] A
Levels: A B
> predict(model,observation,type="p")
A B
[1,] 0.21 0.79
这是正确的行为还是错误?如果这是正确的行为,我如何从概率中估计最可能的类别?
尝试可重现的示例:
library(kernlab)
set.seed(1000)
# Generate fake data
n <- 1000
x <- rnorm(n)
p <- 1 / (1 + exp(-10*x))
y <- factor(rbinom(n, 1, p))
dat <- data.frame(x, y)
tmp <- split(dat, dat$y)
# Create unequal sizes in the groups (helps illustrate the problem)
newdat <- rbind(tmp[[1]][1:100,], tmp[[2]][1:10,])
# Fit the model using radial kernal (default)
out <- ksvm(y ~ x, data = newdat, prob.model = T)
# Create some testing points near the boundary
testdat <- data.frame(x = seq(.09, .12, .01))
# Get predictions using both methods
responsepreds <- predict(out, newdata = testdat, type = "r")
probpreds <- predict(out, testdat, type = "p")
results <- data.frame(x = testdat,
response = responsepreds,
P.x.0 = probpreds[,1],
P.x.1 = probpreds[,2])
结果输出:
> results
x response P.x.0 P.x.1
1 0.09 0 0.7199018 0.2800982
2 0.10 0 0.6988079 0.3011921
3 0.11 1 0.6824685 0.3175315
4 0.12 1 0.6717304 0.3282696