Site icon DataFlair

e1071 Package – Perfect Guide on SVM Training & Testing Models in R

e1071-Package-in-R

FREE Online Courses: Knowledge Awaits – Click for Free Access!

We are going to discuss about the e1071 package in R. We will understand the SVM training and testing models in R and look at the main functions of e1071 package i.e. svm(), predict(), plot(), tune() to execute SVM in R.

Let’s start the tutorial.

R – SVM Training and Testing Models

There are several packages to execute SVM in R. The first and most intuitive package is the e1071 package.

The e1071 Package:

This package was the first implementation of SVM in R.

With the svm() function, we achieve a rigid interface in the libsvm by using visualization and parameter tuning methods.

Refer some of the features of libsvm library given below:

First, you need to set the path to include the directory where the e1071 package is. Then you have to install and include it.

You can use ‘?svm’ to see the help information of the interface.

Technology is evolving rapidly!
Stay updated with DataFlair on WhatsApp!!

Install e1071 package and load it using the following commands:

install.packages (‘e1071’, dependencies = TRUE)
library(e1071)

The R implementation depends on the S3 class mechanism. It provides a training function with standard and formula interfaces, and a predict() method. Also provides a plot() method for visualizing data, support vectors, and decision boundaries. We can perform hyperparameter tuning by using the tune() framework. It performs a grid search over specified parameter ranges.

Learn to create S3 and S4 class in R from the tutorial on Object Oriented Programming in R

Functions in e1071 Package

The main functions in the e1071 package are:

1. The svm() Function

The svm() function trains an SVM. It can perform general regression and classification, as well as density-estimation. It provides a formula interface.

The below data describes some import parameters of the svm() function:

1.1 Data – Specifies an optional data frame that contains the variables present in a model. When you use this parameter, then you don’t need to use the x and y parameters. Take the variables by default from the environment through which ‘svm’ is called from.

Master the Working of Multiple Linear Regression in R

1.2 Type – We can use SVM as a classification machine, regression machine, or for novelty detection. It is dependent on y, that is, whether it is a factor not, the type takes C-classification or eps-regression as its default setting. It may be overwritten by setting an explicit value. Valid options are:

1.3 parameter – It is required for the kernel of type polynomial (default: 3).

2. The plot() Function

Use the plot() function to view the built model with a scatter plot of the input. It optionally draws a filled contour plot of the class regions. plot() function is used to represent data, support vectors and models in a visual form. Let’s learn how to use this function:

plot.svm(x, data, formula, fill = TRUE, grid = 50, slice = list(),symbolPalette = palette(), svSymbol = "x", dataSymbol = "o", ...)

Here,

SVM allows simple graphical visualization of classification models.

Creating SVM Model in R

In order to create our SVM model, we will use the e1071 library and the iris dataset in R.

#Author DataFlair
library("e1071")
data("iris")
head(iris)

Output:

In the next step, we will partition our dataset into x and y variables. The x variables consist of all the independent variables such as Sepal.length, Sepal.Width etc. and y variable comprises of the Species, that is, setosa, versicolor and virginica.

x <- iris[,-5]
y <- iris[5]
model_svm <- svm(Species ~ ., data=iris)
summary(svm_model)

Output:

In the final step, we will perform predictions based on our input variable x. Then, we will create a confusion matrix to evaluate the result of the SVM prediction and the class data.

pred <- predict(model_svm,x)

confusionMatrix(pred,y$Species)

Output:

Summary

In this tutorial of e1071 packages in R, we discussed the training and testing models in R. We also saw the main functions of e1071 packages in R that are SVM, Plot, Predict, Tune.

Up next in our R DataFlair Tutorial Series – Bayesian Network in R

If you have any query or suggestion related to the tutorial, feel free to share with us. We will definitely solve them.

Exit mobile version