9/23/24, 3:50 PM POC - Colab
import numpy as np
import pandas as pd
data = pd.read_csv('/content/Results.csv')
X = data[['Math', 'Science', 'English', 'History', 'Art']].values
y = data['Result'].apply(lambda x: 1 if x == 'Pass' else 0).values
X = X / 100.0
train_size = int(0.8 * len(X))
X_train, X_test = X[:train_size], X[train_size:]
y_train, y_test = y[:train_size], y[train_size:]
class MLP:
def __init__(self, input_size, hidden_size, output_size, learning_rate=0.01, n_iters=1000):
self.input_size = input_size
self.hidden_size = hidden_size
self.output_size = output_size
self.learning_rate = learning_rate
self.n_iters = n_iters
self.W1 = np.random.randn(self.input_size, self.hidden_size)
self.b1 = np.zeros((1, self.hidden_size))
self.W2 = np.random.randn(self.hidden_size, self.output_size)
self.b2 = np.zeros((1, self.output_size))
def sigmoid(self, z):
return 1 / (1 + np.exp(-z))
def sigmoid_derivative(self, z):
return z * (1 - z)
def forward(self, X):
self.z1 = np.dot(X, self.W1) + self.b1
self.a1 = self.sigmoid(self.z1)
self.z2 = np.dot(self.a1, self.W2) + self.b2
self.a2 = self.sigmoid(self.z2)
return self.a2
def backward(self, X, y, output):
m = X.shape[0]
d_z2 = output - y
d_W2 = (1/m) * np.dot(self.a1.T, d_z2)
d_b2 = (1/m) * np.sum(d_z2, axis=0)
d_a1 = np.dot(d_z2, self.W2.T)
d_z1 = d_a1 * self.sigmoid_derivative(self.a1)
d_W1 = (1/m) * np.dot(X.T, d_z1)
d_b1 = (1/m) * np.sum(d_z1, axis=0)
self.W1 -= self.learning_rate * d_W1
self.b1 -= self.learning_rate * d_b1
self.W2 -= self.learning_rate * d_W2
self.b2 -= self.learning_rate * d_b2
def train(self, X, y):
for _ in range(self.n_iters):
t t lf f d(X)
https://colab.research.google.com/drive/1R5dRCm7e2acYHFr5M5qPQ7iVuOvTSitS#scrollTo=wyRvnm4zSYxw&printMode=true 1/5
9/23/24, 3:50 PM POC - Colab
output = self.forward(X)
self.backward(X, y, output)
def predict(self, X):
output = self.forward(X)
return np.where(output >= 0.5, 1, 0)
y_train = y_train.reshape(-1, 1)
y_test = y_test.reshape(-1, 1)
mlp = MLP(input_size=5, hidden_size=10, output_size=1, learning_rate=0.01, n_iters=100000)
mlp.train(X_train, y_train)
y_pred = mlp.predict(X_test)
accuracy = np.mean(y_pred == y_test) * 100
print(f"MLP Accuracy: {accuracy:.2f}%")
MLP Accuracy: 88.00%
import numpy as np
import pandas as pd
data = pd.read_csv('/content/Results.csv')
X = data[['Math', 'Science', 'English', 'History', 'Art']].values
y = data['Result'].apply(lambda x: 1 if x == 'Pass' else 0).values
X = X / 100.0
train_size = int(0.8 * len(X))
X_train, X_test = X[:train_size], X[train_size:]
y_train, y_test = y[:train_size], y[train_size:]
class SVM:
def __init__(self, lr=0.01, n_iters=1000, lambda_param=0.01):
self.lr = lr
self.n_iters = n_iters
self.lambda_param = lambda_param
self.w = None
self.b = None
def fit(self, X, y):
y_ = np.where(y == 0, -1, 1)
self.w = np.zeros(X.shape[1])
self.b = 0
for _ in range(self.n_iters):
for idx, x_i in enumerate(X):
condition = y_[idx] * (np.dot(x_i, self.w) - self.b) >= 1
if condition:
self.w -= self.lr * (2 * self.lambda_param * self.w)
else:
self.w -= self.lr * (2 * self.lambda_param * self.w - np.dot(x_i, y_[idx]))
self.b -= self.lr * y_[idx]
https://colab.research.google.com/drive/1R5dRCm7e2acYHFr5M5qPQ7iVuOvTSitS#scrollTo=wyRvnm4zSYxw&printMode=true 2/5
9/23/24, 3:50 PM POC - Colab
def predict(self, X):
approx = np.dot(X, self.w) - self.b
return np.where(approx >= 0, 1, 0)
class KNN:
def __init__(self, k=5):
self.k = k
def fit(self, X, y):
self.X_train = X
self.y_train = y
def predict(self, X):
y_pred = [self._predict(x) for x in X]
return np.array(y_pred)
def _predict(self, x):
distances = [np.sqrt(np.sum((x - x_train)**2)) for x_train in self.X_train]
k_indices = np.argsort(distances)[:self.k]
k_nearest_labels = [self.y_train[i] for i in k_indices]
most_common = max(set(k_nearest_labels), key=k_nearest_labels.count)
return most_common
class DecisionTree:
def __init__(self, max_depth=10):
self.max_depth = max_depth
def fit(self, X, y):
self.tree = self._grow_tree(X, y)
def _grow_tree(self, X, y, depth=0):
if len(set(y)) == 1 or depth == self.max_depth:
return np.argmax(np.bincount(y))
feat_idx, threshold = self._best_split(X, y)
left_idx = X[:, feat_idx] <= threshold
right_idx = X[:, feat_idx] > threshold
left = self._grow_tree(X[left_idx], y[left_idx], depth + 1)
right = self._grow_tree(X[right_idx], y[right_idx], depth + 1)
return (feat_idx, threshold, left, right)
def _best_split(self, X, y):
best_feat, best_thresh, best_gain = None, None, -1
for feat_idx in range(X.shape[1]):
thresholds = np.unique(X[:, feat_idx])
for thresh in thresholds:
gain = self._information_gain(X[:, feat_idx], y, thresh)
if gain > best_gain:
best_gain = gain
best_feat = feat_idx
best_thresh = thresh
return best_feat, best_thresh
def _information_gain(self, X_col, y, split_thresh):
parent_entropy = self._entropy(y)
left_idx = X_col <= split_thresh
right_idx = X_col > split_thresh
https://colab.research.google.com/drive/1R5dRCm7e2acYHFr5M5qPQ7iVuOvTSitS#scrollTo=wyRvnm4zSYxw&printMode=true 3/5
9/23/24, 3:50 PM POC - Colab
n, n_left, n_right = len(y), np.sum(left_idx), np.sum(right_idx)
if n_left == 0 or n_right == 0:
return 0
e_left, e_right = self._entropy(y[left_idx]), self._entropy(y[right_idx])
child_entropy = (n_left/n) * e_left + (n_right/n) * e_right
return parent_entropy - child_entropy
def _entropy(self, y):
proportions = np.bincount(y) / len(y)
return -np.sum([p * np.log2(p) for p in proportions if p > 0])
def predict(self, X):
return np.array([self._traverse_tree(x, self.tree) for x in X])
def _traverse_tree(self, x, tree):
if not isinstance(tree, tuple):
return tree
feat_idx, threshold, left, right = tree
if x[feat_idx] <= threshold:
return self._traverse_tree(x, left)
else:
return self._traverse_tree(x, right)
svm = SVM()
svm.fit(X_train, y_train)
y_pred_svm = svm.predict(X_test)
svm_accuracy = np.mean(y_pred_svm == y_test) * 100
knn = KNN(k=5)
knn.fit(X_train, y_train)
y_pred_knn = knn.predict(X_test)
knn_accuracy = np.mean(y_pred_knn == y_test) * 100
tree = DecisionTree()
tree.fit(X_train, y_train)
y_pred_tree = tree.predict(X_test)
tree_accuracy = np.mean(y_pred_tree == y_test) * 100
print(f"SVM Accuracy: {svm_accuracy:.2f}%")
print(f"KNN Accuracy: {knn_accuracy:.2f}%")
print(f"Decision Tree Accuracy: {tree_accuracy:.2f}%")
SVM Accuracy: 98.00%
KNN Accuracy: 91.50%
Decision Tree Accuracy: 100.00%
https://colab.research.google.com/drive/1R5dRCm7e2acYHFr5M5qPQ7iVuOvTSitS#scrollTo=wyRvnm4zSYxw&printMode=true 4/5
9/23/24, 3:50 PM POC - Colab
https://colab.research.google.com/drive/1R5dRCm7e2acYHFr5M5qPQ7iVuOvTSitS#scrollTo=wyRvnm4zSYxw&printMode=true 5/5