Guest User

Untitled

a guest
Dec 12th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.35 KB | None | 0 0
  1. install.packages('ggplot2', dep = TRUE)
  2. install.packages('e1071', dep = TRUE)
  3.  
  4. #Load data
  5. data("iris")
  6. str(iris)
  7.  
  8. #Plot sample data
  9. library(ggplot2)
  10. qplot(Petal.Length, Petal.Width, data=iris, color=Species)
  11.  
  12. #Use SVM
  13. library(e1071)
  14. mymodel <- svm(Species~., data=iris)
  15. summary(mymodel)
  16. plot(mymodel, data = iris, Petal.Width~Petal.Length, slice = list(Sepal.Width = 3, Sepal.Length=4))
  17.  
  18. #SVM Model evaluation
  19. pred <- predict(mymodel, iris)
  20. tab <- table(Predicted = pred, Actual = iris$Species)
  21. tab
  22.  
  23. #Calculate missclassification rate
  24. mcr <- 1-sum(diag(tab))/sum(tab)
  25. mcr
  26.  
  27. #Change kernel to linear kernel
  28. mymodel <- svm(Species~., data=iris, kernel = "linear")
  29. summary(mymodel)
  30. plot(mymodel, data = iris, Petal.Width~Petal.Length, slice = list(Sepal.Width = 3, Sepal.Length=4))
  31.  
  32. #SVM Model evaluation with linear kernel
  33. pred <- predict(mymodel, iris)
  34. tab <- table(Predicted = pred, Actual = iris$Species)
  35. tab
  36.  
  37. #Calculate missclassification rate with linear kernel
  38. mcr <- 1-sum(diag(tab))/sum(tab)
  39. mcr
  40.  
  41. #Change kernel to polynomial kernel
  42. mymodel <- svm(Species~., data=iris, kernel = "polynomial")
  43. summary(mymodel)
  44. plot(mymodel, data = iris, Petal.Width~Petal.Length, slice = list(Sepal.Width = 3, Sepal.Length=4))
  45.  
  46. #SVM Model evaluation with polynomial kernel
  47. pred <- predict(mymodel, iris)
  48. tab <- table(Predicted = pred, Actual = iris$Species)
  49. tab
  50.  
  51. #Calculate missclassification rate with polynomial kernel
  52. mcr <- 1-sum(diag(tab))/sum(tab)
  53. mcr
  54.  
  55. #Change kernel to sigmoid kernel
  56. mymodel <- svm(Species~., data=iris, kernel = "sigmoid")
  57. summary(mymodel)
  58. plot(mymodel, data = iris, Petal.Width~Petal.Length, slice = list(Sepal.Width = 3, Sepal.Length=4))
  59.  
  60. #SVM Model evaluation with sigmoid kernel
  61. pred <- predict(mymodel, iris)
  62. tab <- table(Predicted = pred, Actual = iris$Species)
  63. tab
  64.  
  65. #Calculate missclassification rate with sigmoid kernel
  66. mcr <- 1-sum(diag(tab))/sum(tab)
  67. mcr
  68.  
  69. #Tune svm model
  70. set.seed(123)
  71. tmodel <- tune(svm, Species~., data = iris, ranges = list(epsilon = seq(0,1,0.1), cost = 2^(2:9)))
  72. summary(tmodel)
  73.  
  74. plot(tmodel)
  75.  
  76. #Use best svm model
  77. mymodel <- tmodel$best.model
  78. summary(mymodel)
  79. plot(mymodel, data = iris, Petal.Width~Petal.Length, slice = list(Sepal.Width = 3, Sepal.Length=4))
  80.  
  81. #Best SVM Model evaluation
  82. pred <- predict(mymodel, iris)
  83. tab <- table(Predicted = pred, Actual = iris$Species)
  84. tab
  85.  
  86. #Calculate missclassification rate with best model
  87. mcr <- 1-sum(diag(tab))/sum(tab)
  88. mcr
Add Comment
Please, Sign In to add comment