Astar
Astar
Program 7
Date: 17th September 2024
AIM:
Write a program to implement Perceptron neural network for Classification/ Regression.
DESCRIPTION:
The Perceptron is one of the simplest types of artificial neural networks. It is
primarily used for binary classification, where it learns to classify data into two
distinct classes. The perceptron algorithm works by adjusting weights during the
training process to minimize the error between predicted and actual outputs.
In this program, we will implement a basic Perceptron network for both
classification and regression tasks. The Perceptron consists of the following
key components:
1. Inputs: The features of the dataset.
2. Weights: Parameters that the model learns during the training phase.
3. Bias: A constant term added to the weighted sum of inputs to allow the
model to shift the decision boundary.
4. Activation Function: For classification, a step function is typically used
to decide the output (0 or 1). For regression, a linear activation function
can be used to predict continuous values.
5. Learning Rule: The model updates the weights based on the error
(difference between predicted and actual values) during each iteration.
ALGORITHM:
1. Initialization
1. Initialize the weights (W) and bias (b) to small random values or zero.
2. Set the learning rate (η) and the number of epochs.
2. Training
For each epoch, repeat the following steps:
- For each input sample X[i], calculate the weighted sum of inputs:
output = X[i] · W + b
- Apply activation function:
- Classification: Step function
- Regression: Linear activation
- Calculate the error between the predicted output and the actual target (y[i]):
error = y[i] - predicted output
- Update the weights and bias based on the error:
W = W + η * error * X[i]
b = b + η * error
3. Testing
After training, test the model on unseen data by calculating the output
predictions, then evaluate the performance by comparing the predictions to the
actual values.
- For classification, measure accuracy.
- For regression, calculate Mean Squared Error (MSE).
SOURCE CODE:
import numpy as np
class Perceptron:
def __init__(self, input_size, learning_rate=0.01, epochs=1000):
self.weights = np.zeros(input_size) # Initialize weights to zero
self.bias = 0 # Initialize bias term to zero
self.learning_rate = learning_rate
self.epochs = epochs
# Accuracy score
accuracy = perceptron.score(X, y)
print(f"Accuracy: {accuracy * 100}%")
OUTPUT: