Linear Models — From Normal Equations to Logistic Regression
A from-scratch deep dive into linear models, building each method from its mathematical foundation before confirming the result against scikit-learn.
Overview
Rather than just calling .fit(), this notebook implements the underlying math directly (normal equation, gradient descent) to build intuition for what scikit-learn's estimators are actually doing internally.
Covered
- Linear regression via the normal equation — solving
θ = (XᵀX)⁻¹Xᵀydirectly, then verifying the result matches scikit-learn'sLinearRegression. - Batch gradient descent — implemented manually, with cost-function convergence visualized over iterations.
- Stochastic gradient descent — compared against batch GD for convergence speed and stability, then via scikit-learn's
SGDRegressor. - Polynomial regression — fitting non-linear (quadratic) data by engineering polynomial features.
- Bias-variance tradeoff — learning curves showing underfitting vs overfitting as model complexity changes.
- Regularization — Ridge, Lasso, and ElasticNet compared, with a visualization of how each shrinks coefficients differently as the penalty strength increases.
- Logistic regression — binary classification via the sigmoid function, with a visualized decision boundary.
- Softmax regression — multiclass classification on the Iris dataset, with visualized decision regions across all three species.
Tech stack
Python, NumPy, scikit-learn, matplotlib
Notebooks
linear_models_notebook.ipynb— original notebook, my own worklinear_models_notebook_polished.ipynb— same analysis with AI-assisted documentation and formatting added for readability