R语言实现SVM算法——分类与回归

打印 上一主题 下一主题

主题 1680|帖子 1680|积分 5040

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. ### 11.6        基于支持向量机进行类别预测 ###
  2. # 构建数据子集
  3. X <- iris[iris$Species!= 'virginica',2:3] # 自变量:Sepal.Width, Petal.Length
  4. y <-  iris[iris$Species != 'virginica','Species'] # 因变量
  5. plot(X,col = y,pch = as.numeric(y)+15,cex = 1.5) # 绘制散点图
  6. # 构建支持向量机分类器
  7. library(e1071)
  8. svm.model <- svm(x = X,y = y,kernel = 'linear',degree = 1,scale = FALSE)
  9. summary(svm.model)
  10. svm.model$index # 查看支持向量的序号
  11. svm.model$nSV   # 查看各类的支持向量个数
  12. svm.model$SV    # 查看支持向量的自变量值
  13. # 绘制SVM分类器的判别边界实线、支持向量及最大间隔分类
  14. plot_svc_decision_boundary <- function(svm.model,X) {
  15.   w = t(svm.model$coefs) %*% svm.model$SV
  16.   b = -svm.model$rho
  17.   margin = 1/w[2]
  18.   abline(a = -b/w[1,2],b=-w[1,1]/w[1,2],col = "red",lwd=2)
  19.   points(X[svm.model$index,],col="blue",cex=2.5,lwd = 2)
  20.   abline(a = -b/w[1,2]+margin,b=-w[1,1]/w[1,2],col = "grey",lwd=2,lty=2)
  21.   abline(a = -b/w[1,2]-margin,b=-w[1,1]/w[1,2],col = "grey",lwd=2,lty=2)
  22. }
  23. # 增加分割线的散点图
  24. plot(X,col = y,pch = as.numeric(y)+15,cex = 1.5) # 绘制散点图
  25. plot_svc_decision_boundary(svm.model,X) # 增加决策边界和标注支持向量
  26. # SVM对特征缩放敏感
  27. Xs <- data.frame(x1 = c(1,5,3,5),
  28.                  x2 = c(50,20,80,60))
  29. ys <- factor(c(0,0,1,1))
  30. svm_clf <- svm(x = Xs,y = ys,cost=100,
  31.                kernel = "linear",scale = FALSE)
  32. Xs_scale <- apply(Xs,2,scale) # 标准化处理
  33. svm_clf1 <- svm(x = Xs_scale,y = ys,cost=100,
  34.                 kernel = "linear",scale = FALSE)
  35. par(mfrow=c(1,2))
  36. plot(Xs,col=ys,pch=as.numeric(ys)+15,cex=1.5,main='Unscaled')
  37. plot_svc_decision_boundary(svm_clf,Xs)
  38. plot(Xs_scale,col = ys,pch=as.numeric(ys)+15,cex=1.5,main="scaled")
  39. plot_svc_decision_boundary(svm_clf1,Xs_scale)
  40. par(mfrow=c(1,1))
  41. # 将参数scale设置为TRUE
  42. svm_clf2 <- svm(x = Xs,y = ys,cost=100,
  43.                 kernel = "linear",scale = TRUE)
  44. # 可以查看标准化的中心和标准差
  45. svm_clf2$x.scale
  46. # 查看手工标准化的均值和标准差
  47. apply(Xs,2,function(x) {c('center' = mean(x,na.rm=TRUE),'scale' = sd(x,na.rm=TRUE))})
  48. # 软间隔分类
  49. X = iris[iris$Species!= 'virginica',1:2] # "Sepal.Length" "Sepal.Width"
  50. y = iris[iris$Species != 'virginica','Species']
  51. svm_smallC <- svm(x = X,y = y,cost = 1,
  52.                   kernel = "linear",scale = FALSE)
  53. svm_largeC <- svm(x = X,y = y,cost = 100,
  54.                   kernel = "linear",scale = FALSE)
  55. par(mfrow=c(1,2))
  56. plot(X,col=y,pch=as.numeric(y)+15,main='small cost')
  57. plot_svc_decision_boundary(svm_smallC,X)
  58. plot(X,col=y,pch=as.numeric(y)+15,main='large cost')
  59. plot_svc_decision_boundary(svm_largeC,X)
  60. par(mfrow=c(1,1))
  61. # 非线性支持向量机分类
  62. # 导入数据集
  63. moons <- read.csv('moons.csv')
  64. # 查看数据结构
  65. str(moons)
  66. # 编写绘制决策边界函数
  67. visualize_classifier <- function(model,X,y,xlim,ylim,title = NA){
  68.   x1s <- seq(xlim[1],xlim[2],length.out=200)
  69.   x2s <- seq(ylim[1],ylim[2],length.out=200)
  70.   Z <- expand.grid(x1s,x2s)
  71.   colnames(Z) <- colnames(X)
  72.   y_pred <- predict(model,Z,type = 'class')
  73.   y_pred <- matrix(y_pred,length(x1s))
  74.   
  75.   filled.contour(x1s,x2s,y_pred,
  76.                  nlevels = 2,
  77.                  col = RColorBrewer::brewer.pal(length(unique(y)),'Pastel1'),
  78.                  key.axes = FALSE,
  79.                  plot.axes = {axis(1);axis(2);
  80.                    points(X[,1],X[,2],pch=as.numeric(y)+16,col=as.numeric(y)+2,cex=1.5)
  81.                  },
  82.                  xlab = colnames(X)[1],ylab = colnames(X)[2]
  83.   )
  84.   title(main = title)
  85. }
  86. xlim <- c(-1.5,2.5)
  87. ylim <- c(-1,1.5)
  88. # 构建线性支持向量机分类
  89. svm_linear <- svm(x = moons[,1:2],y = factor(moons[,3]),
  90.                 kernel = 'linear',degree = 1,cost = 10)
  91. # 绘制决策边界
  92. visualize_classifier(svm_linear,moons[,1:2],moons[,3],
  93.                      xlim,ylim,title = '线性支持向量机分类')
  94. # 构建非线支持向量机分类
  95. svm_poly <- svm(x = moons[,1:2],y = factor(moons[,3]),
  96.                 kernel = 'polynomial',degree = 3,cost = 5)
  97. # 绘制决策边界
  98. visualize_classifier(svm_poly,moons[,1:2],moons[,3],
  99.                      xlim,ylim,title = '非线性支持向量机分类')
  100. # 多项式核
  101. svm_poly1 <- svm(x = moons[,1:2],y = factor(moons[,3]),
  102.                 kernel = 'polynomial',degree = 3,cost = 5,coef0 = 1)
  103. visualize_classifier(svm_poly1,moons[,1:2],moons[,3],
  104.                      xlim,ylim,'多项式核')
  105. # 增加相似性特征
  106. svm_rbf <- svm(x = moons[,1:2],y = factor(moons[,3]),
  107.                kernel='radial',gamma = 0.1, cost = 0.01)
  108. svm_rbf1 <- svm(x = moons[,1:2],y = factor(moons[,3]),
  109.                kernel='radial',gamma = 0.1, cost = 1000)
  110. svm_rbf2 <- svm(x = moons[,1:2],y = factor(moons[,3]),
  111.                 kernel='radial',gamma = 5, cost =1000)
  112. visualize_classifier(svm_rbf,moons[,1:2],moons[,3],
  113.                      xlim,ylim,'gamma = 0.1, cost = 0.01')
  114. visualize_classifier(svm_rbf1,moons[,1:2],moons[,3],
  115.                      xlim,ylim,'gamma = 0.1, cost = 1000')
  116. visualize_classifier(svm_rbf2,moons[,1:2],moons[,3],
  117.                      xlim,ylim,'gamma = 5, cost = 1000')
  118. # 调整支持向量机
  119. # 使用tune.svm函数调整支持向量机
  120. moons$y <- as.factor(moons$y)
  121. tuned <- tune.svm(y ~ .,data = moons,
  122.                   gamma = 10^(-5:-1),cost = 10^(1:3))
  123. summary(tuned) # 得到模型相关信息
  124. # 利用最佳参数设置支持向量机
  125. model.tuned <- svm(y ~ .,data = moons,
  126.                    gamma = tuned$best.parameters$gamma,
  127.                    cost = tuned$best.parameters$cost)
  128. # 对训练集进行类别预测
  129. pred <- predict(model.tuned,newdata = moons[,1:2])
  130. #生成混淆矩阵,观察预测精度
  131. table('actual' = moons$y,
  132.       'prediction'= pred)
复制代码


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

万有斥力

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表