SVM7
SVM7
• Linear SVM:
• The working of the SVM algorithm can be
understood by using an example. Suppose we
have a dataset that has two tags (green and
blue), and the dataset has two features x1 and
x2. We want a classifier that can classify the
pair(x1, x2) of coordinates in either green or
blue. Consider the below image:
• So as it is 2-d space so by just using a straight
line, we can easily separate these two classes.
But there can be multiple lines that can
separate these classes. Consider the below
image:
• Hence, the SVM algorithm helps to find the best line or
decision boundary; this best boundary or region is
called as a hyperplane. SVM algorithm finds the closest
point of the lines from both the classes. These points
are called support vectors. The distance between the
vectors and the hyperplane is called as margin. And the
goal of SVM is to maximize this margin.
The hyperplane with maximum margin is called
the optimal hyperplane.
Non-Linear SVM:
•
Till the Data pre-processing step, the code will
remain the same. Below is the code:
• #Data Pre-processing Step
• # importing libraries
• import numpy as nm
• import matplotlib.pyplot as mtp
• import pandas as pd
•
• #importing datasets
• data_set= pd.read_csv('user_data.csv')
•
• #Extracting Independent and dependent Variable
• x= data_set.iloc[:, [2,3]].values
• y= data_set.iloc[:, 4].values
•
• # Splitting the dataset into training and test set.
• from sklearn.model_selection import train_test_split
• x_train, x_test, y_train, y_test= train_test_split(x, y, test_size= 0.25, random_state=0)
• #feature Scaling
• from sklearn.preprocessing import StandardScaler
• st_x= StandardScaler()
• x_train= st_x.fit_transform(x_train)
• x_test= st_x.transform(x_test)
• After executing the above code, we will pre-
process the data. The code will give the
dataset as:
The scaled output for the test set will be:
• Fitting the SVM classifier to the training set:
• Now the training set will be fitted to the SVM
classifier. To create the SVM classifier, we will
import SVC class from Sklearn.svm library. Below is
the code for it:
•
from sklearn.svm import SVC # "Support vector class
ifier"
• classifier = SVC(kernel='linear', random_state=0)
• classifier.fit(x_train, y_train)
• In the above code, we have
used kernel='linear', as here we are creating
SVM for linearly separable data. However, we
can change it for non-linear data. And then we
fitted the classifier to the training
dataset(x_train, y_train)
Output:
• Output:
• By executing the above code, we will get the
output as:
• As we can see, the above output is appearing
similar to the Logistic regression output. In the
output, we got the straight line as hyperplane
because we have used a linear kernel in the
classifier. And we have also discussed above
that for the 2d space, the hyperplane in SVM
is a straight line.
Visualizing the test set result: