在 R 中绘制由 caret 包训练的 SVM 线性模型
Posted
技术标签:
【中文标题】在 R 中绘制由 caret 包训练的 SVM 线性模型【英文标题】:Plot SVM linear model trained by caret package in R 【发布时间】:2016-03-09 19:29:54 【问题描述】:目的
我试图通过plot
可视化 SVMLinear 分类模型。我正在使用kernlab
包中提供的示例代码和数据,注意到caret
实际上是通过ksvm
函数训练svm(此处指src 代码(https://github.com/topepo/caret/blob/master/models/files/svmLinear.R))
问题
当我绘制插入符号模型对象的最终模型时,它没有产生数字。而我 尝试了三种方法后没有找到出路。
代码
require(caret)
require(kernlab)
# ===== sample codes from ksvm
x <- rbind(matrix(rnorm(120),ncol = 2), matrix(rnorm(120, mean = 3), ncol = 2))
y <- matrix(c(rep(1, 60), rep(-1, 60)))
# ===== train linear svm model via three wrappers
#===
#dedicated to caret because if I am not preparing data in this way,
#caret reports errors and stop training with following messages:
#Error in train.default(x, y, weights = w, ...) :
#At least one of the class levels is not a valid R variable name; This will cause errors when class
#probabilities are generated because the variables names will be converted to X.1, X1 . Please use
#factor levels that can be used as valid R variable names (see ?make.names for help).
dat0 <- data.frame(x = x, y= factor(y, levels = c(-1, 1), labels = c('c0', 'c1')))
svp0 <- ksvm(y~., dat0, type = 'C-svc', kernel = 'vanilladot')
dat <- data.frame(x = x, y = as.factor(y))
svp <- ksvm(y~., dat, type = 'C-svc', kernel = 'vanilladot')
svp1 <- ksvm(x, y, kernel = 'vanilladot', type = 'C-svc')
kernlab::plot(svp0, data = dat0) # works
kernlab::plot(svp, data = dat) # works
kernlab::plot(svp1, data = x) # works
ctr <- trainControl(method='cv',
number=5,
classProbs=TRUE,
summaryFunction=twoClassSummary
)
svp.c <- train(y ~., dat0, method = "svmLinear",
trControl = ctr,
preProcess = c('center', 'scale'),
metric = "ROC")
kernlab::plot(svp.c$finalModel, data = dat0) #Not working
# Error in seq.default(min(sub[, 2]), max(sub[, 2]), length = grid) :
# 'from' cannot be NA, NaN or infinite
我认为svp.c$finalModel
(由插入符号训练)的结构与其他svp
(由原始ksvm
训练)相同。但是为什么plot
不适用于前者呢?
总之,我想知道是否有人设法通过caret
包训练了plot
SVM。
谢谢。
Edit2:附件是我的session.Info()
。希望它有助于确定我错过了什么。
R 版本 3.2.2 (2015-08-14) 平台:x86_64-apple-darwin13.4.0 (64 位)运行于:OS X 10.11.1 (El Capitan)
语言环境:1 en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
附加的基础包:
1 stats graphics grDevices utils datasets methods base
其他附加包:
1kernlab_0.9-22 caret_6.0-62 ggplot2_1.0.1 lattice_0.20-33
通过命名空间加载(未附加):1Rcpp_0.12.2 magrittr_1.5 splines_3.2.2 MASS_7.3-44 munsell_0.4.2
[6] 颜色空间_1.2-6 foreach_1.4.3 minqa_1.2.4 stringr_1.0.0 car_2.1-0
[11] plyr_1.8.3 工具_3.2.2 并行_3.2.2 nnet_7.3-11 pbkrtest_0.4-2
[16] grid_3.2.2 gtable_0.1.2 nlme_3.1-122 mgcv_1.8-7 quantreg_5.19 [21] MatrixModels_0.4-1 iterators_1.0.8 lme4_1.1-9 digest_0.6.8 Matrix_1.2-2
[26] nloptr_1.0.4 reshape2_1.4.1 codetools_0.2-14 stringi_1.0-1 编译器_3.2.0
[31] pROC_1.8 scales_0.3.0.9000 stats4_3.2.2 SparseM_1.7 proto_0.3-10
【问题讨论】:
【参考方案1】:当我使用kernlab::plot(svp.c$finalModel)
时,我得到以下输出:
附录:sessionInfo()
的结果
R version 3.2.2 (2015-08-14)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X 10.11.1 (El Capitan)
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] kernlab_0.9-20 caret_6.0-47 ggplot2_1.0.1 lattice_0.20-33
loaded via a namespace (and not attached):
[1] Rcpp_0.11.6 magrittr_1.5 splines_3.2.2 MASS_7.3-43
[5] munsell_0.4.2 colorspace_1.2-6 foreach_1.4.2 minqa_1.2.4
[9] car_2.0-25 stringr_1.0.0 plyr_1.8.3 tools_3.2.2
[13] parallel_3.2.2 pbkrtest_0.4-2 nnet_7.3-10 grid_3.2.2
[17] gtable_0.1.2 nlme_3.1-121 mgcv_1.8-7 quantreg_5.11
[21] iterators_1.0.7 gtools_3.5.0 lme4_1.1-8 digest_0.6.8
[25] Matrix_1.2-2 nloptr_1.0.4 reshape2_1.4.1 codetools_0.2-14
[29] stringi_0.5-5 compiler_3.2.2 BradleyTerry2_1.0-6 pROC_1.8
[33] scales_0.2.5 SparseM_1.6 brglm_0.5-9 proto_0.3-10
【讨论】:
谢谢。您介意附上您的 session.info 吗?以上是关于在 R 中绘制由 caret 包训练的 SVM 线性模型的主要内容,如果未能解决你的问题,请参考以下文章
R语言使用caret包构建线性核的支持向量机SVM(Support Vector Machines with Linear Kernel )构建回归模型通过method参数指定算法名称
R语言使用caret包构建GBM模型:在模型最优参数已知的情况下,拟合整个训练集,而无需进行任何重采样或参数调优
R语言使用caret包构建岭回归模型(Ridge Regression )构建回归模型通过method参数指定算法名称通过trainControl函数控制训练过程