0% found this document useful (0 votes)
9 views4 pages

Week 6

Uploaded by

abhirampabolu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views4 pages

Week 6

Uploaded by

abhirampabolu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

10/7/24, 11:53 PM Untitled5.

ipynb - Colab

import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from sklearn.metrics import precision_score, recall_score, f1_score, confusion_matrix, accuracy_score
from sklearn.tree import DecisionTreeClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.svm import SVC
from sklearn.linear_model import Perceptron

def sigmoid(x):
return 1 / (1 + np.exp(-x))

def sigmoid_derivative(x):
return x * (1 - x)

def relu(x):
return np.maximum(0, x)

def relu_derivative(x):
return np.where(x > 0, 1, 0)

df = pd.read_csv('students_result.csv')
df = df.sample(frac=1)
df['Result'] = df['Result'].apply(lambda x: 1 if x == 'Pass' else 0)

X = df.drop(['Student_ID', 'Result'], axis=1).values


y = df['Result'].values

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4, random_state=42)

scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)

input_size = X_train.shape[1]
hidden_layer_size = input_size + 2
output_size = 1

weights_input_hidden = np.random.randn(input_size, hidden_layer_size) * np.sqrt(1. / input_size)


weights_hidden_output = np.random.randn(hidden_layer_size, output_size) * np.sqrt(1. / hidden_layer_size)
bias_hidden = np.random.randn(1, hidden_layer_size)
bias_output = np.random.randn(1, output_size)

learning_rate = 0.01
epochs = 1000

def forward_propagation(X):
hidden_layer_input = np.dot(X, weights_input_hidden) + bias_hidden
hidden_layer_activation = relu(hidden_layer_input)
output layer input = np dot(hidden layer activation weights hidden output) + bias output
https://colab.research.google.com/drive/1MR4fMU8-CDeQtzEnAlO1fNjnjGZ7rUOr#scrollTo=5Ovd6SNxO8Gb&printMode=true 1/4
10/7/24, 11:53 PM Untitled5.ipynb - Colab
output_layer_input np.dot(hidden_layer_activation, weights_hidden_output) + bias_output
output_layer_activation = sigmoid(output_layer_input)
return hidden_layer_activation, output_layer_activation

def backward_propagation(X, y, hidden_layer_activation, output_layer_activation):


global weights_input_hidden, weights_hidden_output, bias_hidden, bias_output

error = y - output_layer_activation
d_output = error * sigmoid_derivative(output_layer_activation)

error_hidden_layer = d_output.dot(weights_hidden_output.T)
d_hidden_layer = error_hidden_layer * relu_derivative(hidden_layer_activation)

weights_hidden_output += hidden_layer_activation.T.dot(d_output) * learning_rate


bias_output += np.sum(d_output, axis=0, keepdims=True) * learning_rate
weights_input_hidden += X.T.dot(d_hidden_layer) * learning_rate
bias_hidden += np.sum(d_hidden_layer, axis=0, keepdims=True) * learning_rate

for epoch in range(epochs):


hidden_layer_activation, output_layer_activation = forward_propagation(X_train)
backward_propagation(X_train, y_train.reshape(-1, 1), hidden_layer_activation, output_layer_activation)
if epoch % 100 == 0:
loss = np.mean(np.square(y_train.reshape(-1, 1) - output_layer_activation))

_, y_pred_custom = forward_propagation(X_test)
y_pred_custom = np.round(y_pred_custom)

accuracy_custom = np.mean(y_pred_custom == y_test.reshape(-1, 1))


precision_custom = precision_score(y_test, y_pred_custom)
recall_custom = recall_score(y_test, y_pred_custom)
f1_custom = f1_score(y_test, y_pred_custom)
cm_custom = confusion_matrix(y_test, y_pred_custom)

print(f"BPNN Accuracy: {accuracy_custom * 100:.2f}%")


print(f"BPNN Precision: {precision_custom:.2f}")
print(f"BPNN Recall: {recall_custom:.2f}")
print(f"BPNN F1-score: {f1_custom:.2f}")
print("BPNN Confusion Matrix:")
print(cm_custom)
print("\n" + "-"*50 + "\n")

models = {
"Decision Tree": DecisionTreeClassifier(),
"KNN": KNeighborsClassifier(),
"SVM": SVC(),
"Perceptron": Perceptron()
}

for model_name, model in models.items():


print(f"Model: {model_name}")

model.fit(X_train, y_train)

y_pred = model.predict(X_test)

accuracy = accuracy_score(y_test, y_pred)


precision = precision_score(y_test, y_pred)
ll ll ( t t d)
https://colab.research.google.com/drive/1MR4fMU8-CDeQtzEnAlO1fNjnjGZ7rUOr#scrollTo=5Ovd6SNxO8Gb&printMode=true 2/4
10/7/24, 11:53 PM Untitled5.ipynb - Colab
recall = recall_score(y_test, y_pred)
f1 = f1_score(y_test, y_pred)

print(f"Accuracy: {accuracy * 100:.2f}%")


print(f"Precision: {precision:.2f}")
print(f"Recall: {recall:.2f}")
print(f"F1-score: {f1:.2f}")

cm = confusion_matrix(y_test, y_pred)
print("Confusion Matrix:")
print(cm)
print("\n" + "-"*50 + "\n")

BPNN Accuracy: 96.25%


BPNN Precision: 0.98
BPNN Recall: 0.95
BPNN F1-score: 0.96
BPNN Confusion Matrix:
[[188 5]
[ 10 197]]

--------------------------------------------------

Model: Decision Tree


Accuracy: 100.00%
Precision: 1.00
Recall: 1.00
F1-score: 1.00
Confusion Matrix:
[[193 0]
[ 0 207]]

--------------------------------------------------

Model: KNN
Accuracy: 95.25%
Precision: 0.92
Recall: 1.00
F1-score: 0.96
Confusion Matrix:
[[174 19]
[ 0 207]]

--------------------------------------------------

Model: SVM
Accuracy: 97.00%
Precision: 0.96
Recall: 0.99
F1-score: 0.97
Confusion Matrix:
[[184 9]
[ 3 204]]

--------------------------------------------------

Model: Perceptron
Accuracy: 86.25%
Precision: 0.87
Recall: 0.86

https://colab.research.google.com/drive/1MR4fMU8-CDeQtzEnAlO1fNjnjGZ7rUOr#scrollTo=5Ovd6SNxO8Gb&printMode=true 3/4
10/7/24, 11:53 PM Untitled5.ipynb - Colab
F1-score: 0.87
Confusion Matrix:
[[166 27]
[ 28 179]]

--------------------------------------------------

https://colab.research.google.com/drive/1MR4fMU8-CDeQtzEnAlO1fNjnjGZ7rUOr#scrollTo=5Ovd6SNxO8Gb&printMode=true 4/4

You might also like