Files
FYSSTK-Project2/notebooks/logisitic-regression.ipynb
T

96 KiB

In [1]:
from easynn.feedforward import FFNN, Layer, Regularization, LeakyReLU, Softmax, CrossEntropyLoss
from easynn.schedulers import AdamScheduler

import pandas as pd
import numpy as np
from sklearn.preprocessing import OneHotEncoder
from sklearn.model_selection import train_test_split
In [2]:
df = pd.read_csv("breast_cancer_regression_results.csv")
X = df.iloc[:, :-1].to_numpy()
y = df.iloc[:, -1].to_numpy().reshape(-1, 1)
encoder = OneHotEncoder(sparse_output=False)
y_encoded = encoder.fit_transform(y)
X_train, X_test, y_train, y_test = train_test_split(X, y_encoded, test_size=0.2)
In [3]:
logistic_model = FFNN([
    Layer(30, 2, activation_function=Softmax()),
], loss_fn=CrossEntropyLoss(), scheduler=AdamScheduler(learning_rate=5e-4, epochs=25000)
)
In [4]:
logistic_model.fit(X_train, y_train)
In [5]:
import matplotlib.pyplot as plt
import plotting


plt.plot(logistic_model.scheduler.loss_history)
Out [5]:
[<matplotlib.lines.Line2D at 0x7f26628d6850>]
In [6]:
y_pred = logistic_model.predict(X_test)
In [7]:
from sklearn.metrics import accuracy_score, roc_auc_score, confusion_matrix

acc = accuracy_score(y_test.argmax(axis=1), y_pred.argmax(axis=1))
auc = roc_auc_score(y_test, y_pred)
In [8]:
acc, auc
Out [8]:
(0.9473684210526315, 0.9910485933503836)
In [9]:
import seaborn as sns

cm = confusion_matrix(y_test.argmax(axis=1), y_pred.argmax(axis=1))

sns.heatmap(cm, annot=True, fmt='d')
plt.xlabel("Predicted Label")
plt.ylabel("True Label")
plt.tight_layout()
plt.savefig("logistic_classification_confusion_matrix.pdf")
plt.show()
In [ ]: