# 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*

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:*

importnumpyas npclass 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)fromsklearnimportdatasetsdef 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

CLAPandFOLLOWif you find my content helpful/informative!

*For complete code implementation:*