为什么R中的ksvm中的概率和响应不一致? [英] Why are probabilities and response in ksvm in R not consistent?

查看:222
本文介绍了为什么R中的ksvm中的概率和响应不一致?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用R中的kernlab软件包中的ksvm,通过predict.ksvm中的type="probabilities"选项来预测概率.但是,我发现有时使用predict(model,observation,type="r")不会产生predict(model,observation,type="p")给定的最高概率的类.

I am using ksvm from the kernlab package in R to predict probabilities, using the type="probabilities" option in predict.ksvm. However, I find that sometimes using predict(model,observation,type="r") yields not the class with the highest probability given by 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

这是正确的行为还是错误?如果这是正确的行为,我该如何从概率中估计最可能的类别?

Is this correct behavior, or a bug? If it is correct behavior, how can I estimate the most likely class from the probabilities?

尝试使用可重现的示例:

Attempt at reproducible example:

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

推荐答案

如果您看决策矩阵和投票,它们似乎与回答更加一致:

If you look at the decicision matrix and votes, they seem to be more in line with the responses:

> predict(out, newdata = testdat, type = "response")
[1] 0 0 1 1
Levels: 0 1
> predict(out, newdata = testdat, type = "decision")
            [,1]
[1,] -0.07077917
[2,] -0.01762016
[3,]  0.02210974
[4,]  0.04762563
> predict(out, newdata = testdat, type = "votes")
     [,1] [,2] [,3] [,4]
[1,]    1    1    0    0
[2,]    0    0    1    1
> predict(out, newdata = testdat, type = "prob")
             0         1
[1,] 0.7198132 0.2801868
[2,] 0.6987129 0.3012871
[3,] 0.6823679 0.3176321
[4,] 0.6716249 0.3283751

kernlab帮助页面(?predict.ksvm)链接到论文

The kernlab help pages (?predict.ksvm) link to paper Probability estimates for Multi-class Classification by Pairwise Coupling by T.F. Wu, C.J. Lin, and R.C. Weng.

在7.3节中,决策和概率可能会有所不同:

In section 7.3 it is said that the decisions and probabilities can differ:

...我们解释了为什么通过基于概率的结果和 基于决策值的方法可能如此不同.对于某些问题 δDV选择的参数与 其他五个规则.在波形中,某些参数全部 基于概率的方法可提供更高的交叉验证准确性 比δDV.例如,我们观察验证的决策值 两类数据的集合分别在[0.73,0.97]和[0.93,1.02]中; 因此,验证集中的所有数据都归为一类 而且错误率很高.相反,基于概率的方法 通过S型函数拟合决策值,这样可以更好 通过将决策值削减到0.95附近来分离这两个类别. 这项观察揭示了两者之间的区别 基于概率和基于决策值的方法...

...We explain why the results by probability-based and decision-value-based methods can be so distinct. For some problems, the parameters selected by δDV are quite different from those by the other five rules. In waveform, at some parameters all probability-based methods gives much higher cross validation accuracy than δDV . We observe, for example, the decision values of validation sets are in [0.73, 0.97] and [0.93, 1.02] for data in two classes; hence, all data in the validation sets are classified as in one class and the error is high. On the contrary, the probability-based methods fit the decision values by a sigmoid function, which can better separate the two classes by cutting at a decision value around 0.95. This observation shed some light on the difference between probability-based and decision-value based methods...

我对这些方法不太了解,但您可能理解,似乎有很多不同的概率预测方法和其他一些方法,并且type=response对应的方法与方法不同.一种用于预测概率.

I'm not familiar enough with these methods to understand the issue, but maybe you do, It looks like that there is distinct methods for predicting with probabilities and some other method, and the type=response corresponds to different method than the one which is used for prediction probabilities.

这篇关于为什么R中的ksvm中的概率和响应不一致?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆