SVM From Scratch

Tanvi Penumudy
Jan 17 · 2 min read

Support Vector Machine or SVM is one of the most popular Supervised Learning algorithms, which is used for Classification as well as Regression problems. However, primarily, it is used for Classification problems in Machine Learning.

The goal of the SVM algorithm is to create the best line or decision boundary that can segregate n-dimensional space into classes so that we can easily put the new data point in the correct category in the future. This best decision boundary is called a hyperplane.

Source: Javatpoint

Image Source: R-bloggers

For the conceptual overview of SVM, refer — A Beginner’s Introduction to SVM

We shall now go through the code walkthrough for the implementation of the SVM algorithm from scratch:

import numpy as np 

class SVM:

def __init__(self, learning_rate=0.001, lambda_param=0.01, n_iters=1000):
self.lr = learning_rate
self.lambda_param = lambda_param
self.n_iters = n_iters
self.w = None
self.b = None


def fit(self, X, y):
n_samples, n_features = X.shape

y_ = np.where(y <= 0, -1, 1)

self.w = np.zeros(n_features)
self.b = 0

for _ in range(self.n_iters):
for idx, x_i in enumerate(X):
condition = y_[idx] * (np.dot(x_i, self.w) - self.b) >= 1
if condition:
self.w -= self.lr * (2 * self.lambda_param * self.w)
else:
self.w -= self.lr * (2 * self.lambda_param * self.w - np.dot(x_i, y_[idx]))
self.b -= self.lr * y_[idx]


def predict(self, X):
approx = np.dot(X, self.w) - self.b
return np.sign(approx)
from sklearn import datasetsdef accuracy(y_true, y_pred):
accuracy = np.sum(y_true == y_pred)/len(y_true)
return accuracy
X, y = datasets.make_blobs(n_samples=50, n_features=2, centers=2, cluster_std=1.05, random_state=40)
y = np.where(y == 0, -1, 1)

clf = SVM()
clf.fit(X, y)
print(clf.w, clf.b)
y_pred = clf.predict(X)
acc = accuracy(y, y_pred)
print("Training Accuracy: ",acc)
Out:
Training Accuracy: 1.0

Hope you enjoyed and made the most out of this article! Stay tuned for my upcoming blogs! Make sure to CLAP and FOLLOW if you find my content helpful/informative!

For complete code implementation:

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store