📚 Module: Unit 13 • Core Foundations

Supervised & Unsupervised Learning: Algorithmic Foundations

A rigorous architectural and mathematical study of core machine learning algorithms: Ordinary Least Squares (OLS) regression assumptions and polynomial expansions, Logistic Regression log-odds geometry, non-parametric KNN, Naive Bayes conditional probability, Support Vector Machines (SVM) maximum-margin hyperplanes, CART Decision Trees and Random Forest ensembles, K-Means and Hierarchical Agglomerative clustering, Principal Component Analysis (PCA) eigendecomposition, and Market Basket Association Rule mining.

Section 01

Continuous Prediction: Linear & Polynomial Regression

Regression models predict a real-valued continuous target variable y from a set of predictor features X. The foundational benchmark of continuous statistical learning is Ordinary Least Squares (OLS) Linear Regression.

Ordinary Least Squares (OLS) Mechanics

Simple Linear Regression models the relationship between an independent variable X and a dependent variable y as a linear equation:
Predicted_y = Intercept + (Slope * X)

OLS solves for the optimal parameters by minimizing the Residual Sum of Squares (RSS): the sum of the squared vertical distances between the observed ground-truth data points and the fitted regression hyperplane:
RSS = Sum_over_all_samples((Actual_y - Predicted_y) ^ 2)

Ordinary Least Squares Residual Minimization Geometry
Target (y)
   ^                     * (Actual Data Point: y_i)
   |                    /|
   |                   / | <--- Vertical Residual Error: e_i = (Actual_y - Predicted_y)
   |                  /  |
   |                 /   v
   |                /----+ (Fitted Prediction Point on Line: y_hat)
   |               /
   |              /
   |             /   Fitted OLS Hyperplane: y = Beta_0 + Beta_1 * x
   |            /
   |           /
   +----------+----------------------------------------------------> Feature (x)
             Intercept (Beta_0)
        

The 5 Classical Gauss-Markov Assumptions of OLS

According to the Gauss-Markov theorem, OLS estimators are the Best Linear Unbiased Estimators (BLUE) if and only if the following five structural conditions are satisfied:

  1. Linearity in Parameters: The relationship between the predictors and the expected value of the target is linear in the parameters.
  2. Strict Exogeneity: The error terms have a conditional expectation of zero given the predictors: Expected_Value(Error | X) = 0. No omitted variable bias.
  3. Homoscedasticity: The error terms have constant, uniform variance across all values of the predictors. (Heteroscedasticity occurs when residuals fan out or expand).
  4. No Autocorrelation: Error terms are completely uncorrelated with one another across observations.
  5. No Severe Multicollinearity: Predictor features must not be perfect linear combinations of each other. Multicollinearity inflates coefficient variance, diagnosed via the Variance Inflation Factor (VIF > 5.0).

Polynomial Regression

When relationships exhibit curvature, Polynomial Regression models non-linear phenomena by generating higher-order power terms of existing features (e.g., X^2, X^3). While the feature curve is non-linear in geometric space, it remains linear in the parameter space, allowing standard OLS matrix solutions.

Continuous Regression Evaluation Metrics

Section 02

Parametric Classification: Logistic Regression

Applying standard OLS regression to binary classification produces fundamental mathematical flaws: linear lines produce unbounded predictions extending to negative infinity and positive infinity, violating the Kolmogorov probability axioms (where probability must strictly be bounded within [0.0, 1.0]).

The Odds, Log-Odds & Sigmoid Architecture

Logistic Regression resolves this limitation through a two-stage mathematical transformation:

  1. The Odds Ratio: The probability of an event occurring divided by the probability of it not occurring:
    Odds = Probability / (1 - Probability)
  2. The Logit (Log-Odds) Function: Taking the natural logarithm of the odds maps bounded probabilities from (0, 1) to the entire real number line (-inf, +inf):
    Logit(p) = log(p / (1 - p)) = Beta_0 + (Beta_1 * X_1) + ... + (Beta_d * X_d)
  3. The Sigmoid (Logistic) Function: Inverting the logit equation maps any real-valued linear score z back into a bounded probability score between 0.0 and 1.0:
    Probability = 1 / (1 + exp(-z)) where z = Beta * X
The Sigmoid (Logistic) Activation Curve
Probability P(y=1)
  1.0 +-----------------------------------------------------********* (Asymptote at 1.0)
      |                                              *******
      |                                          ****
      |                                       ***
  0.5 +------------------------------------*-------------------------- (Decision Threshold)
      |                                ***
      |                             ***
      |                         ****
  0.0 +*********------------------------------------------------------ (Asymptote at 0.0)
      +--------------+--------------+--------------+--------------+---> Linear Score (z)
     -6             -3              0             +3             +6
        

Unlike OLS, which can be solved analytically via the closed-form Normal Equation, Logistic Regression parameters are solved using Maximum Likelihood Estimation (MLE) via iterative numerical optimization algorithms (such as L-BFGS or Newton-Raphson).

Section 03

Distance, Probabilistic & Maximum-Margin Classifiers

1. K-Nearest Neighbors (KNN)

KNN is a non-parametric, instance-based (lazy learning) algorithm. It performs zero statistical parameter optimization during training; it simply stores the training feature vectors in memory.

During inference on a novel observation, KNN calculates distances to all stored points, identifies the k closest neighbors, and assigns the class label via majority vote. Distance is typically computed using Euclidean distance:
Distance = Square_Root(Sum((x_sample - x_neighbor) ^ 2))

KNN & The Curse of Dimensionality

Because KNN relies entirely on geometric distance, as the number of features d grows, the volume of the feature space expands exponentially. In high dimensions, all points become nearly equidistant from one another, causing distance metrics to lose discriminative power.
Mandatory Requirement: Features must always be standardized (e.g., via StandardScaler) prior to running KNN.

2. Naive Bayes Classification

Naive Bayes is a probabilistic classifier founded upon Bayes' Theorem of conditional probability:
Posterior_Probability P(Class | Features) = [ P(Features | Class) * Prior_Probability P(Class) ] / Marginal_Probability P(Features)

The algorithm is termed "Naive" because it makes the strong independence assumption that every feature is strictly conditionally independent of all other features given the class label. Despite this unrealistic assumption, Naive Bayes performs exceptionally well in high-dimensional text classification (e.g., spam filtering, sentiment analysis).

3. Support Vector Machines (SVM)

Support Vector Machines identify the unique separating hyperplane that achieves the Maximum Geometric Margin: the maximum perpendicular distance between the decision boundary and the closest training points of any class (known as the Support Vectors).

Support Vector Machine Maximum-Margin Hyperplane Geometry
Feature 2
   ^
   |        Class A (Circles)            Optimal Separating Hyperplane
   |             O      O                (w * x + b = 0)
   |                O     O                      |
   |                   (O) <=== Support Vector   |
   |   - - - - - - - - - - - - - - - - - - - - - v - - - - - - - - - - - - Margin Boundary (+1)
   |                     |                       /
   |                     |  MARGIN WIDTH (2/||w||)
   |                     |                       /
   |   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Margin Boundary (-1)
   |                   [X] <=== Support Vector /
   |               X      X                   /
   |            X      X       Class B (Crosses)
   +-----------------------------------------------------------------> Feature 1
        

The Kernel Trick in Support Vector Machines

When data is non-linearly separable in its native feature space, the Kernel Trick implicitly projects observations into an infinite- or higher-dimensional Hilbert space where linear separation becomes possible, without ever explicitly computing the coordinates of points in that high-dimensional space:

  • Linear Kernel: Standard dot-product separation for high-dimensional text data.
  • Radial Basis Function (RBF / Gaussian Kernel): Measures similarity using exponential squared Euclidean distance, creating flexible, non-linear circular and curvilinear decision boundaries.
Section 04

Tree-Based Algorithms: Decision Trees & Random Forests

Tree-based algorithms partition the feature space into a hierarchy of non-overlapping rectangular decision regions using greedy recursive binary splitting.

Split Criteria: Gini Impurity vs. Shannon Entropy

At each node, the Classification and Regression Tree (CART) algorithm evaluates all features and split thresholds, selecting the split that maximizes the reduction in node impurity:

Random Forest Ensembles

Individual Decision Trees suffer from extreme high variance (overfitting); small perturbations in the training data produce completely different tree architectures. Random Forests resolve this via Bagging (Bootstrap Aggregating) and Feature Subspace Sampling.

Random Forest Ensemble Architecture & Majority Voting
TOTAL TRAINING DATASET (n Samples, d Features)
       |
       +---> [ Bootstrap Sample 1 ] ===> [ Decision Tree 1 (Trained on sqrt(d) Features) ] ===> Prediction: Class A
       |
       +---> [ Bootstrap Sample 2 ] ===> [ Decision Tree 2 (Trained on sqrt(d) Features) ] ===> Prediction: Class A
       |
       +---> [ Bootstrap Sample B ] ===> [ Decision Tree B (Trained on sqrt(d) Features) ] ===> Prediction: Class B
                                                          |
                                                          v
                                     [ ENSEMBLE MAJORITY VOTING / AVERAGE ]
                                                          |
                                                          v
                                             FINAL PREDICTION: CLASS A
        

Why Random Forests Work: De-correlating Individual Trees

If one predictor is exceptionally dominant, standard bagging trees will all select that same predictor at the root node, resulting in highly correlated individual trees. Averaging correlated trees yields minimal variance reduction.

Random Forests force each split to choose from a random subset of features (typically Square_Root(Total_Features)). This intentional constraint de-correlates the individual trees; when hundreds of de-correlated trees vote together, the ensemble variance plummets without increasing bias.

Section 05

Classification Diagnostics: Confusion Matrix & ROC-AUC

Evaluating binary and multiclass classifiers requires deep structural performance diagnostics beyond simple accuracy percentages.

Evaluation Metric Mathematical Formulation (Natural English) Operational Focus
Accuracy (True_Positives + True_Negatives) / Total_Samples Overall correctness; completely deceptive on class-imbalanced datasets.
Precision True_Positives / (True_Positives + False_Positives) Minimizing False Alarms (Type I error). Critical in spam filtering and marketing spend.
Recall (Sensitivity) True_Positives / (True_Positives + False_Negatives) Minimizing Missed Detections (Type II error). Critical in medical diagnosis and fraud.
F1-Score (2 * Precision * Recall) / (Precision + Recall) Harmonic mean; penalizes extreme imbalance between Precision and Recall.
ROC-AUC Area under True Positive Rate vs. False Positive Rate curve Measures class ranking separation quality across all possible decision thresholds.
Python Multi-Model Training & Diagnostic Evaluation
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import classification_report, roc_auc_score, confusion_matrix

# 1. Instantiate and train Random Forest Classifier
rf_model = RandomForestClassifier(
    n_estimators=100, 
    max_depth=8, 
    max_features="sqrt", 
    random_state=42
)
rf_model.fit(X_train, y_train)

# 2. Generate predictions and class probabilities
y_pred = rf_model.predict(X_test)
y_probs = rf_model.predict_proba(X_test)[:, 1]

# 3. Diagnostic reports
print("Confusion Matrix:\n", confusion_matrix(y_test, y_pred))
print("\nDiagnostic Metrics:\n", classification_report(y_test, y_pred))
print(f"Area Under ROC Curve (AUC): {roc_auc_score(y_test, y_probs):.4f}")
Section 06

Unsupervised Clustering: K-Means & Hierarchical Models

Unsupervised Clustering partitions unlabeled observations into distinct, internally homogeneous groups without any ground-truth training guidance.

1. K-Means Clustering & Lloyd's Algorithm

K-Means partitions n observations into k pre-specified clusters by minimizing the Within-Cluster Sum of Squares (Inertia / WCSS):
Inertia = Sum_over_clusters(Sum_over_points((Point - Centroid) ^ 2))

The standard algorithm executes via two alternating iterative steps:

  1. Assignment Step: Every observation is assigned to its nearest cluster centroid based on Euclidean distance, creating Voronoi tessellation cells.
  2. Update Step: The cluster centroids are recalculated as the geometric mean of all data points assigned to that cluster.
K-Means Iterative Centroid Convergence & Partitioning
INITIAL RANDOM CENTROIDS:                ASSIGNMENT & UPDATE:                    FINAL CONVERGENCE:
+-------------------------------+       +-------------------------------+       +-------------------------------+
|   * *           x [Centroid 2]|       |   * *         |     o o o     |       | [ Cluster 1 ] | [ Cluster 2 ] |
|  * * *                        |  ==>  |  * * *        |    o o o o    |  ==>  |   * * * *     |    o o o o    |
|                               |       |        [Boundary]             |       |               |               |
|         x [Centroid 1]        |       |---------------+---------------|       |---------------+---------------|
|   o o o                       |       |               |   + + + +     |       |               | [ Cluster 3 ] |
|  o o o o                      |       |               |    + + +      |       |               |    + + + +    |
+-------------------------------+       +-------------------------------+       +-------------------------------+
        

Determining Optimal K: Elbow Method & Silhouette Analysis

2. Hierarchical Agglomerative Clustering

Hierarchical clustering constructs a nested tree of partitions without pre-specifying k. Agglomerative Clustering operates bottom-up: every point begins in its own isolated cluster, and the two closest clusters are iteratively merged according to a defined Linkage Criterion:

The complete clustering hierarchy is visualized via a Dendrogram; horizontal cuts across the dendrogram tree at varying vertical distance thresholds determine the final discrete cluster count.

Section 07

Dimensionality Reduction: Principal Component Analysis (PCA)

High-dimensional datasets contain substantial feature redundancy, severe multicollinearity, and sparse geometry (the Curse of Dimensionality). Principal Component Analysis (PCA) is an unsupervised linear transformation that projects data from a high-dimensional space into an orthogonal lower-dimensional subspace while preserving maximal data variance.

The Mathematical Mechanics of PCA

  1. Mean Centering: Center all feature columns to have an arithmetic mean of zero: X_centered = X - Mean(X).
  2. Covariance Matrix Computation: Compute the (d-by-d) Covariance Matrix quantifying all pairwise feature covariances.
  3. Eigendecomposition: Decompose the Covariance Matrix into its constituent Eigenvectors (directions of maximal variance) and Eigenvalues (magnitudes of variance along those directions).
  4. Principal Component Selection: Sort eigenvectors in descending order of their corresponding eigenvalues. The first Principal Component (PC1) accounts for the largest possible variance; subsequent components are mathematically orthogonal (uncorrelated) to preceding ones.
  5. Projection: Multiply the centered data matrix by the top k eigenvectors to obtain the reduced (n-by-k) coordinates.
Python Principal Component Analysis Dimensionality Reduction
from sklearn.decomposition import PCA
from sklearn.preprocessing import StandardScaler

# 1. Standardize features (PCA requires zero mean and unit variance)
X_scaled = StandardScaler().fit_transform(X)

# 2. Fit PCA retaining 95% of total cumulative variance
pca = PCA(n_components=0.95)
X_projected = pca.fit_transform(X_scaled)

print(f"Original Dimension Count: {X.shape[1]}")
print(f"Reduced Dimension Count:  {X_projected.shape[1]}")
print(f"Explained Variance Ratio per Component: {pca.explained_variance_ratio_.round(3)}")
Section 08

Association Rule Learning & Market Basket Analysis

Association Rule Learning identifies non-obvious, high-probability co-occurrence patterns in transaction databases. In retail enterprise analytics, this is known as Market Basket Analysis.

The Three Core Association Metrics

An association rule takes the form {Antecedent A} => {Consequent B} (e.g., {Diapers} => {Beer}). The strength and validity of a rule are evaluated across three primary metrics:

The Apriori Principle

Calculating all possible item combinations across a grocery inventory of 50,000 SKUs creates an intractable combinatorial explosion of 2^50000 subsets.

The Apriori Principle enforces an anti-monotonicity rule: All non-empty subsets of a frequent itemset must also be frequent. Conversely, if an itemset {Bread, Butter} is infrequent (below the minimum support threshold), any larger superset containing it (e.g., {Bread, Butter, Jam}) is guaranteed to be infrequent and is immediately pruned without calculation.

Unit Summary & Key Architectural Takeaways

  • OLS Regression: Minimizes squared residuals; optimal (BLUE) when Gauss-Markov assumptions (Linearity, Exogeneity, Homoscedasticity, Independence, No Multicollinearity) are satisfied.
  • Logistic Regression: Solves binary classification by applying the logit transform and mapping linear scores into probabilities using the non-linear Sigmoid function.
  • Instance vs. Margin: KNN classifies via majority vote of local geometric neighbors (requires scaling); SVM identifies hyperplanes maximizing the geometric margin to support vectors.
  • Tree Ensembles: Decision Trees split via Gini Impurity or Entropy; Random Forests de-correlate trees by combining Bootstrap Aggregating with random feature subspace sampling.
  • Evaluation Discipline: Use Precision and Recall to balance asymmetric costs; evaluate threshold-independent separation using ROC-AUC.
  • Clustering Mechanics: K-Means iteratively minimizes within-cluster inertia (choose K via Elbow/Silhouette); Hierarchical clustering generates nested dendrograms via agglomerative linkage.
  • PCA Subspace Projection: Orthogonal linear transformation projecting centered data onto eigenvectors of the covariance matrix to compress dimensions while preserving maximal variance.
  • Market Basket Rules: Identify co-occurrence patterns using Support (frequency), Confidence (directional certainty), and Lift (strength of association above random independence).