【问题标题】:Why are probabilities and response in ksvm in R not consistent?为什么 R 中 ksvm 的概率和响应不一致?
【发布时间】:2013-03-08 08:04:06
【问题描述】:

我正在使用 R 中的 kernlab 包中的 ksvm 来预测概率,使用 predict.ksvm 中的 type="probabilities" 选项。但是,我发现有时使用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

【问题讨论】:

  • 如果您给我们一些样本数据和样本拟合模型,这将对我们有很大帮助。这将使解释这种行为变得更容易。然而,简而言之,这可能是正确的行为。
  • 除非您提供生成此行为的可重现代码,否则可能没人会同意这是一个错误。否则,我们中间的贝叶斯主义者只会说他们的“你犯了错误”的先验可能是正确的。
  • 我的问题主要是这是否应该是可能的,但我知道一个可重现的例子会有所帮助。我将尝试用这种行为训练一个样本模型,但不幸的是,这发生在一个对 SO 来说太大的模型上。我会告诉你的。
  • @joran 这种行为是可重现的。我将添加一些代码以实现重现性。 roelandvanbeek - 如果我添加的代码不能说明你在说什么,请随意删除它。
  • 用于拟合模型的数据中不等的样本量似乎加剧了这种行为。

标签: r kernlab


【解决方案1】:

如果您查看决策矩阵和投票,它们似乎更符合响应:

> 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) 链接到论文 Probability estimates for Multi-class Classification by Pairwise Coupling by T.F. Wu, C.J. Lin, and R.C. Weng.

在第 7.3 节中说决策和概率可以不同:

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

我对这些方法不够熟悉,无法理解这个问题,但也许你知道,看起来有不同的方法来预测概率和其他一些方法,type=response 对应的方法与一种用于预测概率。

【讨论】:

  • 不错的侦探工作!该链接将我发送到一个包含大量 pdf 的目录。您能否更具体地说明您找到的实际论文是哪一篇?
  • 糟糕,复制粘贴时链接被切断,我现在将其更正为直接指向实际纸张。
猜你喜欢
  • 2016-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-27
  • 2017-01-04
  • 1970-01-01
  • 2013-03-27
  • 2019-03-05
相关资源
最近更新 更多