How to create a classification model using svm for multiple categories in R?

R ProgrammingServer Side ProgrammingProgramming

SVM is a supervised machine learning algorithm which can be used for both classification or regression challenges but mostly we use it for classification. The classification using svm can be done for two or more categories as well. In R, we can use simply use svm function of e1071 package.

Example

Consider the iris data −

 Live Demo

str(iris)

Output

'data.frame': 150 obs. of 5 variables:
$ Sepal.Length: num 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
$ Sepal.Width : num 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
$ Petal.Length: num 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
$ Petal.Width : num 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
$ Species : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...

Example

 Live Demo

head(iris,20)

Output

  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1    5.1          3.5          1.4          0.2    setosa
2    4.9          3.0          1.4          0.2    setosa
3    4.7          3.2          1.3          0.2    setosa
4    4.6          3.1          1.5          0.2    setosa
5    5.0          3.6          1.4          0.2    setosa
6    5.4          3.9          1.7          0.4    setosa
7    4.6          3.4          1.4          0.3    setosa
8    5.0          3.4          1.5          0.2    setosa
9    4.4          2.9          1.4          0.2    setosa
10   4.9          3.1          1.5          0.1    setosa
11   5.4          3.7          1.5          0.2    setosa
12   4.8          3.4          1.6          0.2    setosa
13   4.8          3.0          1.4          0.1    setosa
14   4.3          3.0          1.1          0.1    setosa
15   5.8          4.0          1.2          0.2    setosa
16   5.7          4.4          1.5          0.4    setosa
17   5.4          3.9          1.3          0.4    setosa
18   5.1          3.5          1.4          0.3    setosa
19   5.7          3.8          1.7          0.3    setosa
20   5.1          3.8          1.5          0.3    setosa

Loading e1071 package and creating svm model to predict Species −

Example

library(e1071)
model_1<-svm(iris$Species~.,iris)
model_1

Output

Call:
svm(formula = iris$Species ~ ., data = iris)
Parameters:
   SVM-Type: C-classification
   SVM-Kernel: radial
      cost: 1
Number of Support Vectors: 51

Example

 Live Demo

Consider the below data frame:
x1<-rnorm(20,1,1.05)
x2<-rnorm(20,1,1.05)
x3<-rnorm(20,1,1.05)
y1<-factor(sample(LETTERS[1:4],20,replace=TRUE))
df1<-data.frame(x1,x2,x3,y1)
df1

Output

      x1          x2       x3        y1
1 -0.16972931 0.7246676 1.45289129    D
2 0.70684500 2.2078975 1.64698238     D
3 0.75542931 1.7193236 1.31461683     A
4 -0.01975337 0.6848992 0.80361117    D
5 0.86139532 1.3101784 0.35196665     C
6 -0.53543129 -0.1596975 1.06723416   B
7 -0.81283371 2.1653334 1.93182228    A
8 -0.31556364 -0.4410462 1.61967614   A
9 1.52678513 1.9356670 0.04359926     D
10 1.24594463 0.6215577 0.71009713    A
11 1.53888275 0.7491438 2.08191985    D
12 1.19568488 0.6597553 2.40080721    C
13 -0.18610407 0.3972270 2.23357076   D
14 0.56453388 0.5964609 0.94534907    D
15 1.98699347 0.8026872 -0.68205488   D
16 2.00788377 0.9093129 3.24888927    B
17 1.69652350 0.5379913 0.67402105    A
18 1.28221388 1.7807587 2.06529243    B
19 0.17814671 -0.4299207 0.47859582   D
20 2.82514461 1.9284933 1.59796618    D

Creating svm model to predict y1 −

Example

model_2<-svm(df1$y1~.,df1)
model_2

Output

Call:
svm(formula = df1$y1 ~ ., data = df1)
Parameters:
   SVM-Type: C-classification
   SVM-Kernel: radial
      cost: 1
Number of Support Vectors: 20
raja
Published on 07-Dec-2020 05:17:29
Advertisements