Skip to content

Instantly share code, notes, and snippets.

@ryanwwest
Last active October 25, 2022 12:43
Show Gist options
  • Save ryanwwest/e3167bd30839fbcf4c79279c82dd52ae to your computer and use it in GitHub Desktop.
Save ryanwwest/e3167bd30839fbcf4c79279c82dd52ae to your computer and use it in GitHub Desktop.
Perceptron Data Plotting
from toolkit.matrix import Matrix
import numpy as np
import matplotlib.pyplot as plt
import math
# plots the decision line and data points
def plot(self, features, labels):
# Prepare data
xVec0 = []
yVec0 = []
xVec1 = []
yVec1 = []
# split data into outputs of 0 and 1
for (i, row) in enumerate(features.data):
if labels.data[i][0] == 1:
xVec1.append(row[0])
yVec1.append(row[1])
else:
xVec0.append(row[0])
yVec0.append(row[1])
plt.scatter(xVec0, yVec0, color='red')
plt.scatter(xVec1, yVec1, color='blue')
x = np.linspace(-1, 1, 100)
# Plot the decision line
plt.plot(x, (-self.weights[0]/self.weights[1])*x - self.weights[2]/self.weights[1], label='not linear')
# Add a legend - not really necessary
plt.legend()
plt.xlabel('Feature A')
plt.ylabel('Feature B')
plt.title('Linearly Separable Decision Boundary')
# Show the plot
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment