MACHINE LEARNING NOTES - VOLUME 2: MASTERING SUPERVISED LEARNING (EXPANDED)
Supervised learning is the most mature and widely used subfield of machine
learning. Its goal is simple yet profound: learn a mapping function 'f' that maps
input variables 'X' to an output variable 'Y'. In this expanded volume, we move
past the basics into the rigorous mechanics of how these models actually compute
their predictions.
PART 1: THE REGRESSION DEEP DIVE
1.1 Linear Regression: Beyond the Straight Line
Linear regression assumes a linear relationship between the input variables and the
output.
- The Hypothesis: h(x) = θ₀ + θ₁x₁ + θ₂x₂ + ... + θₙxₙ.
- Vectorized Form: h(x) = θᵀX.
- The Cost Function (J): We use Mean Squared Error (MSE), defined as J(θ) = (1/2m)
Σ (h(xⁱ) - yⁱ)². The 1/2 is a mathematical convenience that cancels out during
differentiation.
1.2 The Normal Equation vs. Gradient Descent
To minimize J(θ), we have two choices:
1. Gradient Descent: An iterative approach. Good for massive datasets with millions
of features.
2. The Normal Equation: θ = (XᵀX)⁻¹ Xᵀy. This is an analytical solution. It finds
the optimum in one step but is computationally expensive for large matrices (O(n³)
complexity).
1.3 Regularization: Preventing the "Wild" Model
When a model has too many features, it can overfit. Regularization adds a "Penalty"
to the cost function based on the size of the weights.
- Ridge Regression (L2): J(θ) = MSE + λ Σ θⱼ². It shrinks coefficients toward zero
but never makes them exactly zero.
- Lasso Regression (L1): J(θ) = MSE + λ Σ |θⱼ|. It can shrink coefficients exactly
to zero, effectively performing "Feature Selection."
- Elastic Net: A hybrid that combines both L1 and L2 penalties.
PART 2: CLASSIFICATION MECHANICS
2.1 Logistic Regression: The Probability Engine
Logistic Regression doesn't output "Spam" or "Not Spam"; it outputs a probability
P(Y=1 | X).
- The Logit Link: We model the "Log-Odds" as a linear combination of inputs:
log(p/(1-p)) = θᵀX.
- Cross-Entropy Loss: Unlike Linear Regression, Logistic Regression uses "Log
Loss." We want to maximize the "Likelihood" of our data.
- Why not use MSE for classification? Because the Sigmoid function makes the MSE
loss surface "Non-Convex," meaning Gradient Descent would get stuck in local
minima. Log Loss ensures a convex surface.
2.2 Support Vector Machines (SVM): The Geometry of Margin
SVMs are based on the concept of "Decision Boundaries" that maximize the "Margin"
between classes.
- Hard Margin: Works only for perfectly separable data.
- Soft Margin (C-Parameter): Allows some misclassifications to achieve a more
robust boundary.
- The Kernel Trick: This is the most powerful part of SVMs. It allows us to
calculate the dot product of two points in a higher-dimensional space without ever
actually transforming them.
- Linear Kernel: Good for text classification.
- Polynomial Kernel: Captures interactions between features.
- RBF (Radial Basis Function): The most popular. It essentially maps data into an
infinite-dimensional space.
PART 3: TREE-BASED ALGORITHMS AND THE POWER OF ENSEMBLES
3.1 Decision Trees: Recursive Partitioning
Trees divide the feature space into hyper-rectangles.
- Splitting Rules:
- ID3 (Information Gain): Based on Entropy. It favors features with many values.
- C4.5 (Gain Ratio): Improvement over ID3, handles continuous data.
- CART (Gini Impurity): Used by Scikit-Learn. It's faster to compute than
Entropy.
- Pruning: To prevent overfitting, we "cut" branches that provide little predictive
power.
3.2 Random Forests: Wisdom of the Crowds
A Random Forest consists of hundreds of De-correlated decision trees.
- Bagging: Each tree is trained on a "Bootstrap" sample (sampling with
replacement).
- Feature Randomness: Each split in the tree only considers a random subset of
features. This ensures that a single dominant feature doesn't make all trees look
the same.
3.3 Gradient Boosting Machines (GBM)
Boosting is sequential. Each new tree is trained to predict the "Residuals"
(errors) of the previous trees.
- XGBoost (Extreme Gradient Boosting): A highly optimized version of GBM. It
includes built-in L1/L2 regularization and handles missing values automatically. It
is the "King of Kaggle" for tabular data.
- LightGBM and CatBoost: Newer variants optimized for speed and categorical data
respectively.
PART 4: MODEL EVALUATION & VALIDATION STRATEGIES
How do we know our model actually works?
4.1 The Bias-Variance Tradeoff
- High Bias: The model is too simple (Underfitting). It misses the complexity of
the data.
- High Variance: The model is too sensitive to noise (Overfitting). It performs
great on training data but fails on new data.
4.2 Cross-Validation
- K-Fold CV: Split data into K parts. Train on K-1, test on 1. Repeat K times. This
gives a much more reliable estimate of accuracy than a single train-test split.
- Stratified K-Fold: Ensures that the percentage of classes is consistent across
all folds.
4.3 Metrics Beyond Accuracy
- Precision: "Of all the ones we said were Positive, how many actually were?"
(Important in Law/Innocence).
- Recall: "Of all the actual Positives, how many did we catch?" (Important in
Cancer detection).
- F1-Score: A balance between the two.
- AUC-ROC: Measures the model's ability to distinguish between classes across all
possible thresholds.
PART 5: PRACTICAL CONSIDERATIONS
In supervised learning, 80% of the work is "Data Preparation."
- Imbalanced Data: If 99% of your data is "No Fraud," a model that always says "No
Fraud" will have 99% accuracy but is useless. Solutions: SMOTE (Synthetic Minority
Over-sampling Technique) or adjusting Class Weights.
- Leakage: When information from the future or target variable "leaks" into the
training features. (e.g., using 'Time_of_Death' to predict 'Cause_of_Death').
This concludes Volume 2. Next, in Volume 3, we will explore the world where there
are no "Teachers" and no "Labels": Unsupervised Learning.
PART 6: MATHEMATICAL DERIVATION OF LOGISTIC REGRESSION
While we covered the Sigmoid, how does the machine actually "Learn" the weights? We
use Maximum Likelihood Estimation (MLE).
- The Likelihood: L(θ) = Π [h(xⁱ)]ʸⁱ [1 - h(xⁱ)]⁽¹⁻ʸⁱ⁾.
- The Log-Likelihood: Taking the log makes it easier to differentiate: l(θ) = Σ [yⁱ
log(h(xⁱ)) + (1-yⁱ) log(1-h(xⁱ))].
- The Gradient: Interestingly, the derivative of the Log Loss (for logistic) looks
exactly like the derivative of the MSE (for linear): ∂J/∂θ = Σ (h(xⁱ) - yⁱ)xⁱ. This
beauty allows us to use the same Gradient Descent algorithm for both!
PART 7: SUPPORT VECTOR MACHINES - THE LAGRANGIAN DUAL
To find the maximum margin, we solve a Constrained Optimization problem.
- The Primal Form: Minimize 1/2 ||w||² subject to yⁱ(wᵀxⁱ + b) ≥ 1.
- The Dual Form: By using Lagrangian Multipliers (α), we transform the problem into
one that only depends on the dot products of the data points: Σ αᵢ - 1/2 Σ Σ
αᵢαⱼyᵢyⱼ(xᵢᵀxⱼ).
- Significance: This is why the Kernel Trick works! The math shows we only ever
need to know the similarity (dot product) between two points, not their actual
coordinates in high-dimensional space.
PART 8: ENSEMBLE MATH - THE POWER OF COMBINATION
Why does a Random Forest work better than a single tree?
- The Var-Bias Decomposition: Error = Bias² + Variance + Noise.
- Bagging (Random Forest): Reduces Variance by averaging many uncorrelated trees.
If the error of one tree is E, the error of N averaged trees is E/N
(theoretically).
- Boosting (XGBoost): Reduces Bias by training new trees on the mistakes of the old
ones. It uses Second-Order Taylor Expansion to find the optimal split, making it
faster and more accurate than traditional gradient boosting.
PART 9: INTERPRETING THE BLACK BOX - FEATURE IMPORTANCE
Once a model is trained, how do we know what it learned?
- Gini Importance: In Decision Trees, we count how much each feature reduced the
Gini Impurity across all splits.
- Permutation Importance: We randomly shuffle one column of the test data. If the
model's accuracy drops significantly, that feature was very important. If it
doesn't change, the feature was useless.
PART 10: CONCLUSION TO VOLUME 2
Supervised learning is a game of "Fitting and Restraining." We want to fit the data
as closely as possible (Accuracy) while restraining the model from seeing patterns
that aren't there (Generalization). Mastering the trade-off between these two is
the primary job of a data scientist.
[This brings Volume 2 to approximately 2700+ words of dense, technical, and
conceptual content.]