{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "122c67bd",
   "metadata": {},
   "outputs": [],
   "source": [
    "from easynn.feedforward import FFNN, Layer, Regularization, LeakyReLU, Softmax, CrossEntropyLoss\n",
    "from easynn.schedulers import AdamScheduler\n",
    "\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "from sklearn.preprocessing import OneHotEncoder\n",
    "from sklearn.model_selection import train_test_split"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "b9c5b725",
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.read_csv(\"breast_cancer_regression_results.csv\")\n",
    "X = df.iloc[:, :-1].to_numpy()\n",
    "y = df.iloc[:, -1].to_numpy().reshape(-1, 1)\n",
    "encoder = OneHotEncoder(sparse_output=False)\n",
    "y_encoded = encoder.fit_transform(y)\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y_encoded, test_size=0.2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "35e06abf",
   "metadata": {},
   "outputs": [],
   "source": [
    "logistic_model = FFNN([\n",
    "    Layer(30, 2, activation_function=Softmax()),\n",
    "], loss_fn=CrossEntropyLoss(), scheduler=AdamScheduler(learning_rate=5e-4, epochs=25000)\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "8a55a0c3",
   "metadata": {},
   "outputs": [],
   "source": [
    "logistic_model.fit(X_train, y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "cd5a03ac",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<matplotlib.lines.Line2D at 0x7f26628d6850>]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 48144 characters]",
      "text/plain": [
       "<Figure size 1350x900 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import plotting\n",
    "\n",
    "\n",
    "plt.plot(logistic_model.scheduler.loss_history)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "08c0b434",
   "metadata": {},
   "outputs": [],
   "source": [
    "y_pred = logistic_model.predict(X_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "3492bd31",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.metrics import accuracy_score, roc_auc_score, confusion_matrix\n",
    "\n",
    "acc = accuracy_score(y_test.argmax(axis=1), y_pred.argmax(axis=1))\n",
    "auc = roc_auc_score(y_test, y_pred)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "298b055a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(0.9473684210526315, 0.9910485933503836)"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "acc, auc"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "35cc8cc0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 45544 characters]",
      "text/plain": [
       "<Figure size 1350x900 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import seaborn as sns\n",
    "\n",
    "cm = confusion_matrix(y_test.argmax(axis=1), y_pred.argmax(axis=1))\n",
    "\n",
    "sns.heatmap(cm, annot=True, fmt='d')\n",
    "plt.xlabel(\"Predicted Label\")\n",
    "plt.ylabel(\"True Label\")\n",
    "plt.tight_layout()\n",
    "plt.savefig(\"logistic_classification_confusion_matrix.pdf\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "47e2c31e",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "project2",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
