0% found this document useful (0 votes)
53 views

Neural Network Assignment

This document contains solutions to 5 questions on neural networks and data preprocessing techniques. Question 1 creates a neuron class and predicts output using a threshold activation function. Question 2 trains an AND gate using perceptron learning algorithm. Question 3 trains a perceptron on a sample dataset and predicts classes for new inputs. Question 4 implements min-max scalar normalization. Question 5 implements standard scalar normalization.

Uploaded by

Prbn Poudel
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views

Neural Network Assignment

This document contains solutions to 5 questions on neural networks and data preprocessing techniques. Question 1 creates a neuron class and predicts output using a threshold activation function. Question 2 trains an AND gate using perceptron learning algorithm. Question 3 trains a perceptron on a sample dataset and predicts classes for new inputs. Question 4 implements min-max scalar normalization. Question 5 implements standard scalar normalization.

Uploaded by

Prbn Poudel
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Central Department of CSIT

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

def predict(self, inputs):


# Calculate the weighted sum of inputs and add bias
weighted_sum = 0
for i in range(len(inputs)):
weighted_sum += inputs[i] * self.weights[i]
weighted_sum += self.bias

# Apply threshold activation function


if weighted_sum >= 0:
return 1
else:
return 0

weights = [0.5, -0.3, 0.2]


bias = -0.1
neuron = Neuron(weights, 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

# Define the training dataset for the AND gate


X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
y = np.array([0, 0, 0, 1])

# Define the activation function


def activation(z):
if z >= 0:
return 1
else:
return 0

# Define the Perceptron Learning Algorithm


def perceptron_learning(X, y, eta, epochs):
n, m = X.shape
w = np.zeros(m)
b = 0
for epoch in range(epochs):
for i in range(n):
z = np.dot(w, X[i]) + b
a = activation(z)
error = y[i] - a
w += eta * error * X[i]
b += eta * error
return w, b

# Train the AND gate using the Perceptron Learning Algorithm


w, b = perceptron_learning(X, y, eta=0.1, epochs=10)

# Test the trained model on the input [1, 1]


z = np.dot(w, [1, 1]) + b
a = activation(z)
print(a) # Output: 1

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

def predict(self, x):


# Add bias term to input
x = np.append(x, 1)
# Compute dot product of weights and input
z = np.dot(self.weights, x)
# Apply step function to dot product
if z > 0:
return 1
else:
return 0

def train(self, X, y):


for epoch in range(self.num_epochs):
for i in range(X.shape[0]):
# Add bias term to input
x = np.append(X[i], 1)
# Compute dot product of weights and input
z = np.dot(self.weights, x)
# Apply step function to dot product
y_pred = 1 if z > 0 else 0
# Update weights based on prediction error
error = y[i] - y_pred
self.weights += self.learning_rate * error * x
# Given training set
X_train = np.array([[5.9, 75], [5.8, 86], [5.2, 50], [5.4, 55], [6.1,
85], [5.5, 62]])
y_train = np.array([1, 1, 0, 0, 1, 0])

# Train perceptron on training set


perceptron = Perceptron(num_features=2)
perceptron.train(X_train, y_train)

# Inputs to predict class for


x_input1 = np.array([6, 82])
x_input2 = np.array([5.3, 52])

# Predict class for inputs


y_pred1 = perceptron.predict(x_input1)
y_pred2 = perceptron.predict(x_input2)

print("Input 1: ", x_input1)


print("Predicted class for input 1: ", y_pred1)

print("Input 2: ", x_input2)


print("Predicted class for input 2: ", y_pred2)

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)).

Q5. Write a python program to implement Standard Scalar.


import numpy as np

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).

You might also like