# SVM From Scratch

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 accuracyX, 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:

## The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +724K followers.

Written by

## Tanvi Penumudy

CS Undergrad at Bennett University ## The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +724K followers.

## More From Medium

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