# 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 ...
model_1

## Output

Call:
model_2

## Output

Call:
svm(formula = df1\$y1 ~ ., data = df1)
Parameters:
SVM-Type: C-classification
Number of Support Vectors: 20