Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- install.packages('ggplot2', dep = TRUE)
- install.packages('e1071', dep = TRUE)
- #Load data
- data("iris")
- str(iris)
- #Plot sample data
- library(ggplot2)
- qplot(Petal.Length, Petal.Width, data=iris, color=Species)
- #Use SVM
- library(e1071)
- mymodel <- svm(Species~., data=iris)
- summary(mymodel)
- plot(mymodel, data = iris, Petal.Width~Petal.Length, slice = list(Sepal.Width = 3, Sepal.Length=4))
- #SVM Model evaluation
- pred <- predict(mymodel, iris)
- tab <- table(Predicted = pred, Actual = iris$Species)
- tab
- #Calculate missclassification rate
- mcr <- 1-sum(diag(tab))/sum(tab)
- mcr
- #Change kernel to linear kernel
- mymodel <- svm(Species~., data=iris, kernel = "linear")
- summary(mymodel)
- plot(mymodel, data = iris, Petal.Width~Petal.Length, slice = list(Sepal.Width = 3, Sepal.Length=4))
- #SVM Model evaluation with linear kernel
- pred <- predict(mymodel, iris)
- tab <- table(Predicted = pred, Actual = iris$Species)
- tab
- #Calculate missclassification rate with linear kernel
- mcr <- 1-sum(diag(tab))/sum(tab)
- mcr
- #Change kernel to polynomial kernel
- mymodel <- svm(Species~., data=iris, kernel = "polynomial")
- summary(mymodel)
- plot(mymodel, data = iris, Petal.Width~Petal.Length, slice = list(Sepal.Width = 3, Sepal.Length=4))
- #SVM Model evaluation with polynomial kernel
- pred <- predict(mymodel, iris)
- tab <- table(Predicted = pred, Actual = iris$Species)
- tab
- #Calculate missclassification rate with polynomial kernel
- mcr <- 1-sum(diag(tab))/sum(tab)
- mcr
- #Change kernel to sigmoid kernel
- mymodel <- svm(Species~., data=iris, kernel = "sigmoid")
- summary(mymodel)
- plot(mymodel, data = iris, Petal.Width~Petal.Length, slice = list(Sepal.Width = 3, Sepal.Length=4))
- #SVM Model evaluation with sigmoid kernel
- pred <- predict(mymodel, iris)
- tab <- table(Predicted = pred, Actual = iris$Species)
- tab
- #Calculate missclassification rate with sigmoid kernel
- mcr <- 1-sum(diag(tab))/sum(tab)
- mcr
- #Tune svm model
- set.seed(123)
- tmodel <- tune(svm, Species~., data = iris, ranges = list(epsilon = seq(0,1,0.1), cost = 2^(2:9)))
- summary(tmodel)
- plot(tmodel)
- #Use best svm model
- mymodel <- tmodel$best.model
- summary(mymodel)
- plot(mymodel, data = iris, Petal.Width~Petal.Length, slice = list(Sepal.Width = 3, Sepal.Length=4))
- #Best SVM Model evaluation
- pred <- predict(mymodel, iris)
- tab <- table(Predicted = pred, Actual = iris$Species)
- tab
- #Calculate missclassification rate with best model
- mcr <- 1-sum(diag(tab))/sum(tab)
- mcr
Add Comment
Please, Sign In to add comment