From ba452284496012605402a30b56d26de7b7f0bb0c Mon Sep 17 00:00:00 2001 From: Ayush Tiwari <36932180+GameLogist@users.noreply.github.com> Date: Fri, 29 Oct 2021 02:36:40 +0530 Subject: [PATCH] Added SVM for Non Linear Data --- Classification/NonLiniearSVM.py | 46 +++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 Classification/NonLiniearSVM.py diff --git a/Classification/NonLiniearSVM.py b/Classification/NonLiniearSVM.py new file mode 100644 index 0000000..88d9cbf --- /dev/null +++ b/Classification/NonLiniearSVM.py @@ -0,0 +1,46 @@ +# importing libraries +import numpy as np +import matplotlib.pyplot as plt +from sklearn.datasets import make_circles +from mpl_toolkits.mplot3d import Axes3D + +# generating data +X, Y = make_circles(n_samples = 500, noise = 0.02) + +# visualizing data +plt.scatter(X[:, 0], X[:, 1], c = Y, marker = '.') +plt.show() + + +# adding a new dimension to X +X1 = X[:, 0].reshape((-1, 1)) +X2 = X[:, 1].reshape((-1, 1)) +X3 = (X1**2 + X2**2) +X = np.hstack((X, X3)) + +# visualizing data in higher dimension +fig = plt.figure() +axes = fig.add_subplot(111, projection = '3d') +axes.scatter(X1, X2, X1**2 + X2**2, c = Y, depthshade = True) +plt.show() + +# create support vector classifier using a linear kernel +from sklearn import svm + +svc = svm.SVC(kernel = 'linear') +svc.fit(X, Y) +w = svc.coef_ +b = svc.intercept_ + +# plotting the separating hyperplane +x1 = X[:, 0].reshape((-1, 1)) +x2 = X[:, 1].reshape((-1, 1)) +x1, x2 = np.meshgrid(x1, x2) +x3 = -(w[0][0]*x1 + w[0][1]*x2 + b) / w[0][2] + +fig = plt.figure() +axes2 = fig.add_subplot(111, projection = '3d') +axes2.scatter(X1, X2, X1**2 + X2**2, c = Y, depthshade = True) +axes1 = fig.gca(projection = '3d') +axes1.plot_surface(x1, x2, x3, alpha = 0.01) +plt.show()