我有一些数据,其中Y变量是一个因子——好或坏。我正在使用'caret'包的'train'方法构建支持向量机。使用'train'函数,我能够确定各种调整参数的值,并得到最终的支持向量机。对于测试数据,我可以预测'class'。但是当我尝试预测测试数据的概率时,我会得到以下错误(例如,我的模型告诉我测试数据中的第一个数据点有y='good',但我想知道获得'good'的概率是多少...通常情况下,在支持向量机中,模型将计算预测的概率...如果Y变量有两个结果,则模型将预测每个结果的概率。具有最大概率的结果被认为是最终解决方案)。
以下是示例代码:
根据以下输出,有9个支持向量。如何识别这12个训练数据点中的哪些是这9个?
**Warning message:
In probFunction(method, modelFit, ppUnk) :
kernlab class probability calculations failed; returning NAs**
以下是示例代码:
library(caret)
trainset <- data.frame(
class=factor(c("Good", "Bad", "Good", "Good", "Bad", "Good", "Good", "Good", "Good", "Bad", "Bad", "Bad")),
age=c(67, 22, 49, 45, 53, 35, 53, 35, 61, 28, 25, 24))
testset <- data.frame(
class=factor(c("Good", "Bad", "Good" )),
age=c(64, 23, 50))
library(kernlab)
set.seed(231)
### finding optimal value of a tuning parameter
sigDist <- sigest(class ~ ., data = trainset, frac = 1)
### creating a grid of two tuning parameters, .sigma comes from the earlier line. we are trying to find best value of .C
svmTuneGrid <- data.frame(.sigma = sigDist[1], .C = 2^(-2:7))
set.seed(1056)
svmFit <- train(class ~ .,
data = trainset,
method = "svmRadial",
preProc = c("center", "scale"),
tuneGrid = svmTuneGrid,
trControl = trainControl(method = "repeatedcv", repeats = 5))
### svmFit finds the optimal values of tuning parameters and builds the model using the best parameters
### to predict class of test data
predictedClasses <- predict(svmFit, testset )
str(predictedClasses)
### predict probablities but i get an error
predictedProbs <- predict(svmFit, newdata = testset , type = "prob")
head(predictedProbs)
根据以下输出,有9个支持向量。如何识别这12个训练数据点中的哪些是这9个?
svmFit$finalModel
支持向量机对象属于"ksvm"类
支持向量类型:C-svc(分类) 参数:成本C = 1
高斯径向基核函数。 超参数:sigma = 0.72640759446315
支持向量数:9
目标函数值:-5.6994 训练误差:0.083333
dput(head( your_data, 30))
),并在适当时更频繁地使用大写键,而在不适当时则少用。 - IRTFM