R语言 SVM(线性可分、线性不可分、多分类) |
您所在的位置:网站首页 › r中svm函数 › R语言 SVM(线性可分、线性不可分、多分类) |
关注微信公共号:小程在线
R版本:3.6.1 setwd('G:\\R语言\\大三下半年\\数据挖掘:R语言实战\\') > library("e1071", lib.loc="H:/Program Files/R/R-3.6.1/library") Warning message: 程辑包‘e1071’是用R版本3.6.2 来建造的 #############模拟线性可分下的SVM > set.seed(12345) > x y x[y==1,] data_train x y x[y==1,] data_test plot(data_train[,2:1],col=as.integer(as.vector(data_train[,3]))+2,pch=8,cex=0.7,main="训练样本集-1和+1类散点图") Call: svm(formula = Fy ~ ., data = data_train, type = "C-classification", kernel = "linear", cost = 10, scale = FALSE) Parameters: SVM-Type: C-classification SVM-Kernel: linear cost: 10 Number of Support Vectors: 16 ( 8 8 ) Number of Classes: 2 Levels: -1 1 > SvmFit$index [1] 1 6 7 10 11 16 17 20 22 24 28 31 33 35 36 37 > plot(x=SvmFit,data=data_train,formula=Fx1~Fx2,svSymbol="#",dataSymbol="*",grid=100)
Call: svm(formula = Fy ~ ., data = data_train, type = "C-classification", kernel = "linear", cost = 0.1, scale = FALSE) Parameters: SVM-Type: C-classification SVM-Kernel: linear cost: 0.1 Number of Support Vectors: 25 ( 12 13 ) Number of Classes: 2 Levels: -1 1
##############10折交叉验证选取损失惩罚参数C > set.seed(12345) > tObj summary(tObj) Parameter tuning of ‘svm’: - sampling method: 10-fold cross validation - best parameters: cost 5 - best performance: 0.175 - Detailed performance results: cost error dispersion 1 1e-03 0.675 0.3129164 2 1e-02 0.375 0.3584302 3 1e-01 0.225 0.2486072 4 1e+00 0.200 0.2297341 5 5e+00 0.175 0.2371708 6 1e+01 0.175 0.2371708 7 1e+02 0.175 0.2371708 8 1e+03 0.175 0.2371708 > BestSvm summary(BestSvm) Call: best.svm(x = Fy ~ ., data = data_train, cost = c(0.001, 0.01, 0.1, 1, 5, 10, 100, 1000), type = "C-classification", kernel = "linear", scale = FALSE) Parameters: SVM-Type: C-classification SVM-Kernel: linear cost: 5 Number of Support Vectors: 16 ( 8 8 ) Number of Classes: 2 Levels: -1 1 > yPred (ConfM (Err ##############模拟线性不可分下的SVM > set.seed(12345) > x x[1:100,] x[101:150,] y data flag data_train data_test plot(data_train[,2:1],col=as.integer(as.vector(data_train[,3])),pch=8,cex=0.7,main="训练样本集散点图")
Call: best.svm(x = Fy ~ ., data = data_train, gamma = c(0.5, 1, 2, 3, 4), cost = c(0.001, 0.01, 0.1, 1, 5, 10, 100, 1000), type = "C-classification", kernel = "radial", scale = FALSE) Parameters: SVM-Type: C-classification SVM-Kernel: radial cost: 1 Number of Support Vectors: 40 ( 23 17 ) Number of Classes: 2 Levels: 1 2 > plot(x=BestSvm,data=data_train,formula=Fx1~Fx2,svSymbol="#",dataSymbol="*",grid=100)
> set.seed(12345) > tObj BestSvm summary(BestSvm) Call: best.svm(x = Fy ~ ., data = data, gamma = c(0.5, 1, 2, 3, 4), cost = c(0.001, 0.01, 0.1, 1, 5, 10, 100, 1000), type = "C-classification", kernel = "radial", scale = FALSE) Parameters: SVM-Type: C-classification SVM-Kernel: radial cost: 5 Number of Support Vectors: 133 ( 70 31 32 ) Number of Classes: 3 Levels: 0 1 2 > plot(x=BestSvm,data=data,formula=Fx1~Fx2,svSymbol="#",dataSymbol="*",grid=100) > SvmFit head(SvmFit$decision.values) 1/2 1/0 2/0 1 1.033036 1.2345269 -0.61225558 2 1.600637 1.2219439 0.76098974 3 1.068253 1.0112116 0.59276079 4 1.047869 0.9999145 0.05666298 5 2.146043 1.4892178 1.23321397 6 1.031256 1.2279855 -1.10302134 > yPred (ConfM (Err ################天猫数据SVM > Tmall_train Tmall_train$BuyOrNot set.seed(12345) > tObj plot(tObj,xlab=expression(gamma),ylab="损失惩罚参数C", + main="不同参数组合下的预测错误率",nlevels=10,color.palette=terrain.colors)
Call: best.svm(x = BuyOrNot ~ ., data = Tmall_train, gamma = 10^(-6:-3), cost = 10^(-3:2), type = "C-classification", kernel = "radial") Parameters: SVM-Type: C-classification SVM-Kernel: radial cost: 100 Number of Support Vectors: 79 ( 40 39 ) Number of Classes: 2 Levels: 0 1 > Tmall_test Tmall_test$BuyOrNot yPred (ConfM (Err |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |