Perceptron
Procedure
Step 1: Import Required Libraries
Import necessary Python libraries, including pandas for data handling, NumPy for numerical computations, and Matplotlib for visualization of the dataset and decision boundaries.
Step 2: Create a Dataset
Generate the OR truth table with four data points:
If doing for XOR, generate the XOR truth table with four data points:
Store this data in a pandas DataFrame for easy manipulation and visualization.
Step 3: Visualize the Dataset
Create a scatter plot showing the four data points with:
- Blue dots representing Class 0
- Red dots representing Class 1
This visualization shows a pattern that is linearly separable for OR. For XOR, the diagonal pattern makes it non-linearly separable.
Step 4: Initialize Perceptron Parameters
Set random seed for reproducibility and initialise:
- : Random weights uniformly distributed between and
- : Random bias uniformly distributed between and
- (
learning_rate): Set to (or in alternate version)
Print initial parameters to track how they change during training.
Step 5: Define Decision Boundary Plotting Function
Create a function plot_decision_boundary(w1, w2, b) that:
- Calculates the decision boundary line using equation:
- Rearranges to:
- Plots this line in green on the scatter plot
- Handles the case when (vertical line)
Step 6: Visualize Initial Random Decision Boundary
Plot the dataset along with the initial random decision boundary to show the starting position before any training occurs. This demonstrates how randomly initialized weights create an arbitrary separation.
Step 7: Define Perceptron Training Function
Implement perceptron_train() function that:
For each epoch:
- Initialise the correct prediction counter.
- For each data point:
- Calculate linear output:
- Apply step activation: if else
- Count if prediction matches actual label.
- Calculate error:
- Update weights:
- Update weights:
- Update bias:
- Calculate accuracy:
- Print epoch results showing accuracy and updated parameters.
- Visualise the decision boundary after each epoch.
Return final trained weights and bias.
Step 8: Run Perceptron Training
Execute the training function for 10 epochs on the dataset.
Step 9: Visualise Training Progress
After each epoch, generate and display a plot showing:
- The current decision boundary (green line)
- Data points (blue and red dots)
- Epoch number in the title
- Current accuracy percentage
For OR, the boundary converges to a correct solution. For XOR, the perceptron fails to converge, demonstrating the limitation of single-layer perceptron on non-linearly separable data.
Step 10: Make Final Predictions
Define predict() function and use it to make predictions on all four points using the final trained weights. Display results showing:
- Input coordinates
- Predicted class
- Actual class
- Whether the prediction is correct or incorrect
Calculate and display final accuracy on the complete dataset.
For the OR gate, the perceptron achieves accuracy. For the XOR gate, accuracy remains below , confirming that a single-layer perceptron cannot solve non-linearly separable problems. This motivates the use of a Multi-Layer Perceptron (MLP) with hidden layers and backpropagation to learn non-linear decision boundaries.