In [ ]:
import numpy as np
import matplotlib.pyplot as plt
from sklearn import svm, datasets
from itertools import product
In [ ]:
# Create simple data points
X, y = datasets.make_circles(n_samples = 150, factor = 0.5, noise = 0.05)
In [ ]:
# Visualize

plt.figure(figsize = (5, 3))
plt.scatter(X[:, 0], X[:, 1], c=y, cmap = 'coolwarm')
plt.show()
No description has been provided for this image
In [ ]:
# Develop SVM

clf_linear = svm.SVC(kernel = 'linear').fit(X, y)
clf_rbf = svm.SVC(kernel = 'rbf', gamma = 1).fit(X, y)
In [ ]:
# Visualize the Linear One

w = clf_linear.coef_[0]
b = clf_linear.intercept_[0]

x_vals = np.linspace(-1.5, 1.5, 100)
y_vals = -(w[0]/w[1])*x_vals - b/w[1]
In [ ]:
plt.figure(figsize = (5, 3))
plt.scatter(X[:, 0], X[:, 1], c=y, cmap = 'coolwarm')
plt.plot(x_vals, y_vals, c = 'green')
plt.show()
No description has been provided for this image
In [ ]:
points = np.array(list(product(np.linspace(-1.5, 1.5, 100), np.linspace(-1.5, 1.5, 100))))
Z = clf_rbf.decision_function(points)
In [ ]:
fig = plt.figure(figsize = (6, 6))
ax = fig.add_subplot(projection = '3d')
ax.scatter(X[:, 0], X[:, 1], y, c=y, cmap = 'coolwarm')
surf = ax.plot_trisurf(points[:, 0], points[:, 1], Z, cmap = 'viridis', alpha = 0.3)
plt.show()
No description has been provided for this image