Exp 5
Exp 5
Experiment No. 5
Title: To implement SVM
Lab Objective: To implement an appropriate machine learning model for the given
application. LO1
Support Vectors are simply the coordinates of individual observation. The SVM
classifier is a frontier that best segregates the two classes (hyper-plane/ line).
You can look at support vector machines and a few examples of their working
here.
Let’s understand:
● Identify the right hyper-plane (Scenario-1): Here, we have three hyper-planes (A,
B, and C). Now, identify the right hyper-plane to classify stars and circles.
snapshot:
Above, you can see that the margin for hyper-plane C is high as compared to both A
and B. Hence, we name the right hyper-plane as C. Another lightning reason for
selecting the hyper-plane with higher margin is robustness. If we select a hyper-
plane having low margin then there is high chance of miss-classification.
class as an outlier.
In the SVM classifier, it is easy to have a linear hyper-plane between these two
classes. But, another burning question which arises is, should we need to add this
feature manually to have a hyper-plane. No, the SVM algorithm has a technique
called the kernel trick. The SVM kernel is a function that takes low dimensional
input space and transforms it to a higher dimensional space i.e. it converts not
separable problem to separable problem. It is mostly useful in non-linear separation
problem. Simply put, it does some extremely complex data transformations, then
finds out the process to separate the data based on the labels or outputs you’ve
defined.
When we look at the hyper-plane in original input space it looks like a circle:
Program Code:
SVM Code: Have a linear SVM kernel
import numpy as np
import matplotlib.pyplot as plt
from sklearn import svm, datasets
# import some data to play with
iris = datasets.load_iris()
X = iris.data[:, :2] # we only take the first two features. We could
# avoid this ugly slicing by using a two-dim dataset
y = iris.target
# we create an instance of SVM and fit out data. We do not scale our
# data since we want to plot the support vectors
C = 1.0 # SVM regularization parameter
svc = svm.SVC(kernel='linear', C=1,gamma=’auto’).fit(X, y)
# create a mesh to plot in
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
h = (x_max / x_min)/100
xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
np.arange(y_min, y_max, h))
plt.subplot(1, 1, 1)
Z = svc.predict(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)
plt.contourf(xx, yy, Z, cmap=plt.cm.Paired, alpha=0.8)
plt.scatter(X[:, 0], X[:, 1], c=y, cmap=plt.cm.Paired)
plt.xlabel('Sepal length')
plt.ylabel('Sepal width')
plt.xlim(xx.min(), xx.max())
plt.title('SVC with linear kernel')
plt.show()
Change the kernel type to rbf in below line and look at the impact.
I would suggest you go for linear SVM kernel if you have a large number of features
(>1000) because it is more likely that the data is linearly separable in high
dimensional space. Also, you can use RBF but do not forget to cross-validate for its
parameters to avoid over-fitting.
gamma: Kernel coefficient for ‘rbf’, ‘poly’ and ‘sigmoid’. Higher the value of
gamma, will try to exact fit the as per training data set i.e. generalization error and
cause over-fitting problem.
C: Penalty parameter C of the error term. It also controls the trade-off between
smooth decision boundaries and classifying the training points correctly.
IMPLEMANTATION:
PROGRAM OUTPUT:
svc = svm.SVC(kernel='rbf', C=1,gamma=10).fit(X, y)
Conclusion: SVM implemented with experimental model on given data set of iris file for
prediction of sepals length and width.