Last active
March 30, 2025 19:51
-
-
Save matheus-santos/019d8f63362e5506899f72a649bcb9b8 to your computer and use it in GitHub Desktop.
plot_decision_boundary.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def plot_decision_boundary(perceptron, X, y, title="Perceptron Decision Boundary"): | |
""" | |
Plots the decision boundary of a trained perceptron. | |
Args: | |
perceptron (Perceptron): The trained perceptron model. | |
X (numpy.ndarray): The input data points (N x 2). | |
y (numpy.ndarray): The corresponding labels (N,). | |
title (str, optional): The title of the plot. Defaults to "Perceptron Decision Boundary". | |
""" | |
plt.figure() | |
# Plot data points | |
plt.scatter(X[y == 1, 0], X[y == 1, 1], c='blue', marker='o', label='Label 1') | |
plt.scatter(X[y == -1, 0], X[y == -1, 1], c='red', marker='^', label='Label -1') | |
# Calculate decision boundary | |
b, w1, w2 = perceptron.w_ # Extract weights and bias | |
# Create a line that represents the decision boundary | |
x1_min, x1_max = X[:, 0].min() - 1, X[:, 0].max() + 1 | |
x2_min, x2_max = X[:, 1].min() - 1, X[:, 1].max() + 1 | |
x1_line = np.linspace(x1_min, x1_max, 100) | |
x2_line = -(w1 * x1_line + b) / w2 # Equation of the line: w1*x1 + w2*x2 + b = 0 | |
# Plot the decision boundary | |
plt.plot(x1_line, x2_line, 'r:', label='Decision Boundary') | |
plt.xlabel('x1') | |
plt.ylabel('x2') | |
plt.title(title) | |
plt.legend() | |
plt.grid(True) | |
plt.show() | |
# Example | |
epochs = 100 | |
lr = 0.1 | |
# Loading datasets | |
X_train, y_train = load_dataset("train_dataset1") | |
X_test, y_test = load_dataset("test_dataset1") | |
# Training Perceptron | |
perceptron = Perceptron(features_count=X_train[0].shape[0], learning_rate=lr, epochs=epochs) | |
perceptron.fit(X_train, y_train) | |
plot_decision_boundary(perceptron, X_train, y_train, "Dataset 1 - Training Scatter Plot") |
Author
matheus-santos
commented
Mar 30, 2025

Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment