{ "cells": [ { "cell_type": "markdown", "id": "a67223e8", "metadata": {}, "source": [ "# Development of a Modular Python Library from Scratch for Automated ROI Segmentation in Thermal Images" ] }, { "cell_type": "markdown", "id": "d207e40a", "metadata": {}, "source": [ "# Module 2: Logistic Regressor From Scratch" ] }, { "cell_type": "markdown", "id": "771dee37", "metadata": {}, "source": [ "Author: Sofia Samaniego Lopez\n", "\n", "Institution: Universidad Autonoma de Baja California (UABC)\n", "\n", "Advisor: Dr. Gerardo Marx Chavez Campos" ] }, { "cell_type": "markdown", "id": "a09130b5", "metadata": {}, "source": [ "This notebook presents **Module 2** of the library's development: the implementation of a **Logistic Regression Classifier from scratch**.\n", "\n", "To ensure a deep understanding of the underlying mechanics, this module avoids high-level machine learning \"black-box\" libraries. Instead, it builds the optimization algorithm using fundamental mathematical operations via **NumPy**. It covers the definition of the Sigmoid activation function, the formulation of the Log-Loss (Cross-Entropy) cost function, and the iterative optimization of weights using Gradient Descent. \n", "\n", "The classic Iris dataset is utilized to evaluate the model's capacity to estimate probabilities and establish a linear decision boundary for binary classification based on morphological features." ] }, { "cell_type": "markdown", "id": "5a856f2a", "metadata": {}, "source": [ "## 1. Environment Setup & Data Loading\n", "Importing core libraries for data manipulation (`pandas`), mathematical operations (`numpy`), and visualization (`matplotlib`). The Iris dataset is loaded to extract the target variables." ] }, { "cell_type": "code", "execution_count": 1, "id": "d8b1c513", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Requirement already satisfied: pandas in c:\\Users\\sofia\\Logistic-Regressor-From_Scratch\\.venv\\Lib\\site-packages (3.0.3)\n", "Requirement already satisfied: numpy>=2.3.3 in c:\\Users\\sofia\\Logistic-Regressor-From_Scratch\\.venv\\Lib\\site-packages (from pandas) (2.5.0)\n", "Requirement already satisfied: python-dateutil>=2.8.2 in c:\\Users\\sofia\\Logistic-Regressor-From_Scratch\\.venv\\Lib\\site-packages (from pandas) (2.9.0.post0)\n", "Requirement already satisfied: tzdata in c:\\Users\\sofia\\Logistic-Regressor-From_Scratch\\.venv\\Lib\\site-packages (from pandas) (2026.2)\n", "Requirement already satisfied: six>=1.5 in c:\\Users\\sofia\\Logistic-Regressor-From_Scratch\\.venv\\Lib\\site-packages (from python-dateutil>=2.8.2->pandas) (1.17.0)\n", "Requirement already satisfied: numpy in c:\\Users\\sofia\\Logistic-Regressor-From_Scratch\\.venv\\Lib\\site-packages (2.5.0)\n", "Requirement already satisfied: matplotlib in c:\\Users\\sofia\\Logistic-Regressor-From_Scratch\\.venv\\Lib\\site-packages (3.11.0)\n", "Requirement already satisfied: contourpy>=1.0.1 in c:\\Users\\sofia\\Logistic-Regressor-From_Scratch\\.venv\\Lib\\site-packages (from matplotlib) (1.3.3)\n", "Requirement already satisfied: cycler>=0.10 in c:\\Users\\sofia\\Logistic-Regressor-From_Scratch\\.venv\\Lib\\site-packages (from matplotlib) (0.12.1)\n", "Requirement already satisfied: fonttools>=4.22.0 in c:\\Users\\sofia\\Logistic-Regressor-From_Scratch\\.venv\\Lib\\site-packages (from matplotlib) (4.63.0)\n", "Requirement already satisfied: kiwisolver>=1.3.1 in c:\\Users\\sofia\\Logistic-Regressor-From_Scratch\\.venv\\Lib\\site-packages (from matplotlib) (1.5.0)\n", "Requirement already satisfied: numpy>=1.25 in c:\\Users\\sofia\\Logistic-Regressor-From_Scratch\\.venv\\Lib\\site-packages (from matplotlib) (2.5.0)\n", "Requirement already satisfied: packaging>=20.0 in c:\\Users\\sofia\\Logistic-Regressor-From_Scratch\\.venv\\Lib\\site-packages (from matplotlib) (26.2)\n", "Requirement already satisfied: pillow>=9 in c:\\Users\\sofia\\Logistic-Regressor-From_Scratch\\.venv\\Lib\\site-packages (from matplotlib) (12.2.0)\n", "Requirement already satisfied: pyparsing>=3 in c:\\Users\\sofia\\Logistic-Regressor-From_Scratch\\.venv\\Lib\\site-packages (from matplotlib) (3.3.2)\n", "Requirement already satisfied: python-dateutil>=2.7 in c:\\Users\\sofia\\Logistic-Regressor-From_Scratch\\.venv\\Lib\\site-packages (from matplotlib) (2.9.0.post0)\n", "Requirement already satisfied: six>=1.5 in c:\\Users\\sofia\\Logistic-Regressor-From_Scratch\\.venv\\Lib\\site-packages (from python-dateutil>=2.7->matplotlib) (1.17.0)\n" ] } ], "source": [ "!pip3 install pandas\n", "!pip3 install numpy\n", "!pip3 install matplotlib\n", "\n", "import pandas as pd\n", "import numpy as np\n", "import matplotlib.pyplot as plt" ] }, { "cell_type": "code", "execution_count": 2, "id": "36ebd91e", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
| \n", " | sl | \n", "sw | \n", "pl | \n", "pw | \n", "target | \n", "tNames | \n", "
|---|---|---|---|---|---|---|
| 0 | \n", "5.1 | \n", "3.5 | \n", "1.4 | \n", "0.2 | \n", "0 | \n", "setosa | \n", "
| 1 | \n", "4.9 | \n", "3.0 | \n", "1.4 | \n", "0.2 | \n", "0 | \n", "setosa | \n", "
| 2 | \n", "4.7 | \n", "3.2 | \n", "1.3 | \n", "0.2 | \n", "0 | \n", "setosa | \n", "
| 3 | \n", "4.6 | \n", "3.1 | \n", "1.5 | \n", "0.2 | \n", "0 | \n", "setosa | \n", "
| 4 | \n", "5.0 | \n", "3.6 | \n", "1.4 | \n", "0.2 | \n", "0 | \n", "setosa | \n", "
| ... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "
| 145 | \n", "6.7 | \n", "3.0 | \n", "5.2 | \n", "2.3 | \n", "2 | \n", "virginica | \n", "
| 146 | \n", "6.3 | \n", "2.5 | \n", "5.0 | \n", "1.9 | \n", "2 | \n", "virginica | \n", "
| 147 | \n", "6.5 | \n", "3.0 | \n", "5.2 | \n", "2.0 | \n", "2 | \n", "virginica | \n", "
| 148 | \n", "6.2 | \n", "3.4 | \n", "5.4 | \n", "2.3 | \n", "2 | \n", "virginica | \n", "
| 149 | \n", "5.9 | \n", "3.0 | \n", "5.1 | \n", "1.8 | \n", "2 | \n", "virginica | \n", "
150 rows × 6 columns
\n", "