You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
35 lines
1.0 KiB
35 lines
1.0 KiB
# CODE SOURCE IS DIRECTLY FROM DOCUMENTATION
|
|
# https://scikit-learn.org/stable/auto_examples/svm/plot_separating_hyperplane.html
|
|
import numpy as np
|
|
import seaborn as sns
|
|
import matplotlib.pyplot as plt
|
|
|
|
|
|
def plot_svm_boundary(model,X,y):
|
|
|
|
X = X.values
|
|
y = y.values
|
|
|
|
# Scatter Plot
|
|
plt.scatter(X[:, 0], X[:, 1], c=y, s=30,cmap='seismic')
|
|
|
|
|
|
# plot the decision function
|
|
ax = plt.gca()
|
|
xlim = ax.get_xlim()
|
|
ylim = ax.get_ylim()
|
|
|
|
# create grid to evaluate model
|
|
xx = np.linspace(xlim[0], xlim[1], 30)
|
|
yy = np.linspace(ylim[0], ylim[1], 30)
|
|
YY, XX = np.meshgrid(yy, xx)
|
|
xy = np.vstack([XX.ravel(), YY.ravel()]).T
|
|
Z = model.decision_function(xy).reshape(XX.shape)
|
|
|
|
# plot decision boundary and margins
|
|
ax.contour(XX, YY, Z, colors='k', levels=[-1, 0, 1], alpha=0.5,
|
|
linestyles=['--', '-', '--'])
|
|
# plot support vectors
|
|
ax.scatter(model.support_vectors_[:, 0], model.support_vectors_[:, 1], s=100,
|
|
linewidth=1, facecolors='none', edgecolors='k')
|
|
plt.show() |