O'Reilly logo

R Data Analysis Cookbook - Second Edition by Kuntal Ganguly

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How to do it...

To classify using SVM, follow these steps:

  1. Load the e1071 and caret packages:
> library(e1071) 
> library(caret)
  1. Read the data:
> bn <- read.csv("banknote-authentication.csv") 
  1. Convert the outcome variable class to a factor:
> bn$class <- factor(bn$class) 
  1. Partition the data:
> set.seed(1000) 
> t.idx <- createDataPartition(bn$class, p=0.7, list=FALSE) 
  1. Build the model:
> mod <- svm(class ~ ., data = bn[t.idx,]) 
  1. Check model performance on training data by generating an error/classification confusion matrix:
> table(bn[t.idx,"class"], fitted(mod), dnn = c("Actual", "Predicted")) 
      Predicted 
Actual   0   1 
     0 534   0 
     1   0 427 
  1. Check model performance on the validation partition:
> pred <- predict(mod, bn[-t.idx,]) > table(bn[-t.idx, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required