Mlee Lab4
Mlee Lab4
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# Load dataset
file_path = '/content/dataLOR.csv'
data = pd.read_csv(file_path)
# Normalize features
X_min, X_max = np.min(X, axis=0), np.max(X, axis=0)
X_normalized = (X - X_min) / (X_max - X_min)
def batch_gradient_descent_l1(X,y,w,alpha,iters,lamb):
cost_history = np.zeros(iters)
for i in range(iters):
hypothesis = sigmoid(np.dot(X,w.T))
w = w - (alpha/len(y)) * (X.T.dot(hypothesis - y) -
(lamb/len(y))*np.sign(w))
cost_history[i] = compute_cost_LOR_L1(y,hypothesis,w,lamb)
return w, cost_history
# Cohen's Kappa
total = TP + TN + FP + FN
po = accuracy
pe = ((TP + FP) * (TP + FN) + (TN + FP) * (TN + FN)) / (total *
total)
kappa = (po - pe) / (1 - pe)
# Training BGD
batch_w, J_his = batch_gradientLOR(X_train, y_train,
np.zeros(X_train.shape[1]), alpha, iters)
plt.plot(range(iters), J_his)
plt.show()
bgd=batch_w
print("WEIGHT VECTOR",bgd)
y_p = sigmoid(np.dot(X_test, bgd))
y_pred=np.where(y_p >= 0.5, 1, 0)
evaluate_classification_metrics(y_test, y_pred)
WEIGHT VECTOR [-7.40788569 2.09931485 6.09559795 -1.29255361
0.09041135 -0.91725038
5.85874336 2.07590874 0.33689408]
Confusion Matrix:
[[140 12]
[35 44]]
Accuracy: 0.7965
Sensitivity (Recall): 0.5570
Specificity: 0.9211
Precision: 0.7857
F1-Score: 0.6519
Cohen's Kappa: 0.5139
plt.plot(range(iters),J_his)
plt.show()
sgd=sgd_w
print("WEIGHT VECTOR",sgd)
y_p = sigmoid(np.dot(X_test, sgd))
y_pred=np.where(y_p >= 0.5, 1, 0)
evaluate_classification_metrics(y_test, y_pred)
WEIGHT VECTOR [-9.18806781 1.43797825 6.88130076 -0.99367934
0.28946922 0.021364
8.73590391 2.51991963 0.02451394]
Confusion Matrix:
[[129 23]
[32 47]]
Accuracy: 0.7619
Sensitivity (Recall): 0.5949
Specificity: 0.8487
Precision: 0.6714
F1-Score: 0.6309
Cohen's Kappa: 0.4561
mbgd_w,J_his = MBGD_gradientLOR(X_train,y_train,w,alpha,
iters,batch_size)
plt.plot(range(iters),J_his)
plt.show()
mbgd=mbgd_w
print("WEIGHT VECTOR",mbgd)
y_p = sigmoid(np.dot(X_test, mbgd))
y_pred=np.where(y_p >= 0.5, 1, 0)
evaluate_classification_metrics(y_test, y_pred)
bgd=batch_w
print("WEIGHT VECTOR",bgd)
y_pred_bgd=X_test.dot(bgd.T)
y_pred=np.where(y_pred_bgd >= 0.5, 1, 0)
evaluate_classification_metrics(y_test, y_pred)
bgd=batch_w
print("WEIGHT VECTOR",bgd)
y_pred_bgd=X_test.dot(bgd.T)
y_pred=np.where(y_pred_bgd >= 0.5, 1, 0)
evaluate_classification_metrics(y_test, y_pred)