{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2dd1fa3f",
   "metadata": {},
   "outputs": [],
   "source": [
    "from easynn.feedforward import Layer, ReLU, Regularization, Linear, FFNN, MSELoss, Softmax, CrossEntropyLoss\n",
    "from easynn.schedulers import AdamScheduler\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from sklearn.datasets import make_regression, make_classification\n",
    "from sklearn.preprocessing import StandardScaler, OneHotEncoder\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.metrics import mean_squared_error, accuracy_score\n",
    "import logging\n",
    "\n",
    "logging.basicConfig(level=logging.WARNING)  # Set default logging level to WARNING"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "73649f28",
   "metadata": {},
   "source": [
    "# Regression Example"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "79c55021",
   "metadata": {},
   "outputs": [],
   "source": [
    "X, y = make_regression(n_samples=1000, n_features=5, n_informative=3, noise=0.1)\n",
    "X = StandardScaler().fit_transform(X)\n",
    "y = y.reshape(-1, 1)\n",
    "y = StandardScaler().fit_transform(y)\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "8a88c060",
   "metadata": {},
   "outputs": [],
   "source": [
    "layers = [Layer(5, 5, ReLU()), Layer(5, 25, ReLU()),  Layer(25, 1, Linear())]\n",
    "network = FFNN(layers, AdamScheduler(learning_rate=0.001, epochs=1000), MSELoss())\n",
    "network.fit(X_train, y_train)\n",
    "predictions = network.predict(X_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "515e1e02",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Text(0.5, 1.0, 'Training Loss Over Time')"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 30988 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.plot(network.scheduler.loss_history - min(network.scheduler.loss_history) + 1e-8)\n",
    "plt.yscale(\"log\")\n",
    "plt.xlabel(\"Epoch\")\n",
    "plt.ylabel(\"Loss\")\n",
    "plt.title(\"Training Loss Over Time\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "1dd8fefc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Text(0.5, 1.0, 'True vs Predicted Values')"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 34416 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.scatter(y_test, predictions)\n",
    "plt.plot([y_test.min(), y_test.max()], [y_test.min(), y_test.max()], 'k--', lw=2)\n",
    "plt.xlabel(\"True Values\")\n",
    "plt.ylabel(\"Predictions\")\n",
    "plt.title(\"True vs Predicted Values\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "d3052118",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.0006306650385315765"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mean_squared_error(y_test, predictions)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ea3bd93f",
   "metadata": {},
   "source": [
    "# Classifier Example"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "fc9641f9",
   "metadata": {},
   "outputs": [],
   "source": [
    "X, y = make_classification(n_samples=1000, n_features=5, n_informative=3, n_classes=2)\n",
    "X = StandardScaler().fit_transform(X)\n",
    "y = y.reshape(-1, 1)\n",
    "y = OneHotEncoder(sparse_output=False).fit_transform(y)\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "00f52ecd",
   "metadata": {},
   "outputs": [],
   "source": [
    "layers = [Layer(5, 10, ReLU()), Layer(10, 5, ReLU()),  Layer(5, 2, Softmax())]\n",
    "network = FFNN(layers, AdamScheduler(learning_rate=0.001, epochs=1000), CrossEntropyLoss())\n",
    "network.fit(X_train, y_train)\n",
    "predictions = network.predict(X_test)\n",
    "single_class_predictions = OneHotEncoder(sparse_output=False).fit_transform(np.argmax(predictions, axis=1).reshape(-1, 1))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "a3e32a73",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.944"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "accuracy_score(y_test, single_class_predictions)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "74212e01",
   "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
}
