# Support Vector Machine Algorithm

* Difficulty Level :Medium
* Last Updated : 24 Nov, Support Vector Machine(SVM) is a supervised machine learning algorithm used for both classification and regression. Though we say regression problems as well its best suited for classification. The objective of SVM algorithm is to find a hyperplane in an N-dimensional space that distinctly classifies the data points. The dimension of the hyperplane depends upon the number of features. If the number of input features is two, then the hyperplane is just a line. If the number of input features is three, then the hyperplane becomes a 2-D plane. It becomes difficult to imagine when the number of features exceeds three.

Let’s consider two independent variables x1, x2 and one dependent variable which is either a blue circle or a red circle.

Linearly Separable Data points

From the figure above its very clear that there are multiple lines (our hyperplane here is a line because we are considering only two input features x1, x2) that segregates our data points or does a classification between red and blue circles. So how do we choose the best line or in general the best hyperplane that segregates our data points.

Selecting the best hyper-plane:

One reasonable choice as the best hyperplane is the one that represents the largest separation or margin between the two classes.

So we choose the hyperplane whose distance from it to the nearest data point on each side is maximized. If such a hyperplane exists it is known as the maximum-margin hyperplane/hard margin. So from the above figure, we choose L2.

Let’s consider a scenario like shown below

Here we have one blue ball in the boundary of the red ball. So how does SVM classify the data? It’s simple! The blue ball in the boundary of red ones is an outlier of blue balls. The SVM algorithm has the characteristics to ignore the outlier and finds the best hyperplane that maximizes the margin. SVM is robust to outliers.

So in this type of data points what SVM does is, it finds maximum margin as done with previous data sets along with that it adds a penalty each time a point crosses the margin. So the margins in these type of cases are called soft margin. When there is a soft margin to the data set, the SVM tries to minimize (1/margin+∧(∑penalty)). Hinge loss is a commonly used penalty. If no violations no hinge loss.If violations hinge loss proportional to the distance of violation.

Till now, we were talking about linearly separable data(the group of blue balls and red balls are separable by a straight line/linear line). What to do if data are not linearly separable?

Say, our data is like shown in the figure above. SVM solves this by creating a new variable using a kernel. We call a point xi on the line and we create a new variable yi as a function of distance from origin o.so if we plot this we get something like as shown below

In this case, the new variable y is created as a function of distance from the origin. A non-linear function that creates a new variable is referred to as kernel.

SVM Kernel:

The SVM kernel is a function that takes low dimensional input space and transforms it into higher-dimensional space, ie it converts non separable problem to separable problem. It is mostly useful in non-linear separation problems. Simply put the kernel, it does some extremely complex data transformations then finds out the process to separate the data based on the labels or outputs defined.

* Effective in high dimensional cases
* Its memory efficient as it uses a subset of training points in the decision function called support vectors
* Different kernel functions can be specified for the decision functions and its possible to specify custom kernels

SVM implementation in python:

Objective: Predict if cancer is Benign or malignant.

Using historical data about patients diagnosed with cancer, enable the doctors to differentiate malignant cases and benign given the independent attributes.

Dataset:/ml/datasets/Breast+Cancer+Wisconsin+(Original)

Python
import matplotlib.pyplot as plt

dataset = pd.DataFrame(data)

Output:

Index([‘ID’, ‘ClumpThickness’, ‘Cell Size’, ‘Cell Shape’, ‘Marginal Adhesion’,
‘Single Epithelial Cell Size’, ‘Bare Nuclei’, ‘Normal Nucleoli’, ‘Bland Chromatin’,
‘Mitoses’, ‘Class’], dtype=’object’)

Output:

RangeIndex: 699 entries, 0 to 698
Data columns (total 11 columns):
ID non-null int64
ClumpThickness non-null int64
Cell Size non-null int64
Cell Shape non-null int64
Single Epithelial Cell Size non-null int64
Bare Nuclei non-null object
Normal Nucleoli non-null int64
Bland Chromatin non-null int64
Mitoses non-null int64
Class non-null int64
dtypes: int64(10), object(1)
memory usage: 60.1+ KB

Python
dataset.describe().transpose()

Output:

countmeanstdmin25%50%75%maxID6991.071704e+ . . . . . .0clump Thickness6994.417740e+002. .02.04.06.010.0Cell Size699.04.417740e+002. .01.01.05.010.0Cell Shape699.03.134478e+003. .01.01.05.010.0Marginal Adhension699.02.806867e+002. .01.01.04.010.0Single Epithelial cell size699.03.216023e+002. .02.02.04.010.0Normal Nucleoli699.03.437768e+002. .02.03.05.010.0Bland chromatin699.02.866953e+002. .01.01.04.010.0Mitoses699.01.589413e+003. .01.01.01.010.0class699.02.689557e+001. .02.02.04.04.0Python
dataset = dataset.replace(‘?’, np.nan)

dataset = dataset.apply(lambda x: x.fillna(x.median()),axis=0)

dataset[‘Bare Nuclei’] = dataset[‘Bare Nuclei’].astype(‘float64′)

Output:

ID ClumpThickness Cell Size Cell Shape Marginal Adhesion Single Epithelial Cell Size Bare Nuclei Normal Nucleoli Bland Chromatin Mitoses Class dtype: int64

Python
from sklearn.model_selection import train_test_split

from sklearn.metrics import accuracy_score, confusion_matrix

features = dataset.drop([“ID”,”Class”], axis=1)

X_train, X_test, y_train, y_test = train_test_split(features,target, test_size = 0.2, random_state = 10)

svc_model = SVC(C= .1, kernel=’linear’, gamma= 1)

svc_model.fit(X_train, y_train)

prediction = svc_model .predict(X_test)

print(svc_model.score(X_train, y_train))

print(svc_model.score(X_test, y_test))

Output:

0. . Python
print(“Confusion Matrix:\n”,confusion_matrix(prediction,y_test))

Output:

Confusion Matrix:
[[95 2]
[ 3 40]]

Python
svc_model = SVC(kernel=’rbf’)

svc_model.fit(X_train, y_train)

Output:

SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,
decision_function_shape=’ovr’, degree=3, gamma=’auto_deprecated’,
kernel=’rbf’, max_iter=-1, probability=False, random_state=None,
shrinking=True, tol=0.001, verbose=False)

Python
print(svc_model.score(X_train, y_train))

print(svc_model.score(X_test, y_test))

Output:

0. . Python
svc_model = SVC(kernel=’poly’)

svc_model.fit(X_train, y_train)

prediction = svc_model.predict(X_test)

print(svc_model.score(X_train, y_train))

print(svc_model.score(X_test, y_test))

Output:

1.0
0. Python
svc_model = SVC(kernel=’sigmoid’)

svc_model.fit(X_train, y_train)

prediction = svc_model.predict(X_test)

print(svc_model.score(X_train, y_train))

print(svc_model.score(X_test, y_test))

Output:

0. .