Skip to content

Instantly share code, notes, and snippets.

@knaggita
Created February 17, 2021 03:46
Show Gist options
  • Save knaggita/ae0ccea1a2c2e9f3df831bf63c445dd2 to your computer and use it in GitHub Desktop.
Save knaggita/ae0ccea1a2c2e9f3df831bf63c445dd2 to your computer and use it in GitHub Desktop.
Python Script to Draw Hyperplanes and Lines Perpendicular to it
import warnings
warnings.filterwarnings("ignore", category=FutureWarning)
import numpy as np
from matplotlib import pyplot as plt
import seaborn as sns
# Specify the data and labels
data = np.array([[-4, -3], [-1, -7], [3, 2], [-1, 7], [3, -2]])
labels = np.array([1, -1, 1, -1, 1])
# Specify the data annotations
nn = ['$z_{0}$', '$x_{1}$', '$x_{2}$', '$x_{3}$', '$x_{4}$']
# Specify the initial weight vector
weights = [-7,-1]
# Compute the slope for the hyperplanes
slope = - weights[0] / weights[1]
# Define the x hyperplane
x_hyperplane = np.linspace(np.amin(data[:,:1]-8), np.amax(data[:,:1]+12))
# Hyperplane 1
bias = 0
y_intercept = - bias / weights[1]
y_hyperplane = slope * x_hyperplane + y_intercept
# Hyperplane 2
bias2 = -5
y_intercept2 = - bias2 / weights[1]
y_hyperplane2 = slope * x_hyperplane + y_intercept2
# Hyperplane 3
bias3 = 5
y_intercept3 = - bias3 / weights[1]
y_hyperplane3 = slope * x_hyperplane + y_intercept3
# Hyperplane 4
bias4 = -35
y_intercept4 = - bias4 / weights[1]
y_hyperplane4 = slope * x_hyperplane + y_intercept4
# Draw a line perpendicular to the hyperplanes
new_slope = (1/slope)*-1
new_b = weights[1] - (weights[0] * new_slope)
xhyp = np.linspace(np.amin(data[:,:1]-4), np.amax(data[:,:1]-3))
yhyp = new_slope * xhyp + new_b
xx = np.min(xhyp)
yy = new_slope * xx + new_b
xxx = np.max(xhyp)
yyy = new_slope * xxx + new_b
# Specify the look of the plot
sns.set(style = 'darkgrid')
fig = plt.figure(figsize=(6, 5))
data = np.around(data, decimals=3)
title = " "
# Create labels for positive and negative datapoints
data_label = ['negative' if l == -1 else 'positive' for l in labels]
with sns.color_palette('dark', 10):
sns.scatterplot(x = data[:,0], y = data[:,1], hue = data_label)
# Anotate the datapoints
for i, txt in enumerate(nn):
plt.annotate(txt, (data[:,0][i], data[:,1][i]), xytext=(6, -7),
textcoords="offset points",
ha='left', va='bottom', color='black', weight='bold')
# Draw the line plots with different bias terms
plt.plot(x_hyperplane, y_hyperplane, '--')
plt.plot(x_hyperplane, y_hyperplane2, '-')
plt.plot(x_hyperplane, y_hyperplane3, '--')
plt.plot(x_hyperplane, y_hyperplane4, '-')
# Draw the perpendicular line
plt.plot(xhyp, yhyp, '-')
# Anotate the arrow
plt.annotate('', ha = 'center', va = 'bottom', xytext = (xxx, yyy),
xy = (xx, yy), arrowprops=dict(facecolor='purple', headwidth=10, width=2))
# Specify the plot features
plt.title(title , loc='left')
plt.xlabel("First Feature")
plt.ylabel("Second Feature")
plt.ylim(np.min(data[:,1])-3, np.max(data[:,1])+4)
plt.xlim(-10.0, 10.0)
plt.legend()
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment