Neural Network Assignment
Neural Network Assignment
TU Kirtipur, Kathmandu
Assignment I
(Neural Network)
Submitted to
Mr. Arjun Saud
CD CSIT, TU
Submitted by
Prabin Poudel
Roll no: 09/080
Q1. Write a python program to create a neuron and predict its output using
the threshold activation function.
class Neuron:
def __init__(self, weights, bias):
self.weights = weights
self.bias = bias
inputs = [1, 0, 1]
output = neuron.predict(inputs)
print(output) # Expected output: 1
Output: 1
Q2. Write a python program to train AND Gate Using Perceptron Learning
Algorithm.
import numpy as np
Output: 1
Q3. Write a python program to train perceptron using given training set
and predict class for the input (6,82) and (5.3,52).
Height(x1) Weight(x2) Class(t)
5.9 75 Male
5.8 86 Male
5.2 50 Female
5.4 55 Female
6.1 85 Male
5.5 62 Female
Let’s assume Male is 1 and female is 0 in class(t).
import numpy as np
class Perceptron:
def __init__(self, num_features, learning_rate=0.01,
num_epochs=100):
self.weights = np.zeros(num_features + 1)
self.learning_rate = learning_rate
self.num_epochs = num_epochs
Input 1: [ 6, 82]
Predicted class for input 1: 1 which is Male
Input 2: [ 5.3, 52]
Predicted class for input 2: 0 which is female
Q4. Write a python program to implement Min-Max Scalar.
import numpy as np
def min_max_scalar(X):
"""
Min-Max Scalar normalization function for a numpy array.
"""
X_norm = (X - X.min(axis=0)) / (X.max(axis=0) - X.min(axis=0))
return X_norm
# Example usage
X = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
X_norm = min_max_scalar(X)
print(X_norm)
We have a 3x3 numpy array X. The min_max_scalar function takes this array as
input and returns the normalized array X_norm, which has been scaled to a range
between 0 and 1. The formula used for Min-Max Scalar normalization is (X -
X.min(axis=0)) / (X.max(axis=0) - X.min(axis=0)).
def standard_scalar(X):
"""
Standard Scalar normalization function for a numpy array.
"""
X_norm = (X - X.mean(axis=0)) / X.std(axis=0)
return X_norm
# Example usage
X = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
X_norm = standard_scalar(X)
print(X_norm)
We have a 3x3 numpy array X. The standard_scalar function takes this array as
input and returns the normalized array X_norm, which has been scaled to have
zero mean and unit variance. The formula used for Standard Scalar normalization
is (X - X.mean(axis=0)) / X.std(axis=0).