← Writing & notes

FROM THE NOTEBOOK

Machine Learning Algorithms 1: Supervised Learning

Supervised learning is a paradigm in machine learning where the model learns from data that has been manually labeled with the correct output. The primary goal is to learn a mapping function that can accurately predict the output for new, unseen data.

This process involves a training phase where the model is fed both input features and their corresponding correct labels. The model adjusts its internal parameters to minimize the difference between its predictions and the actual labels.

Supervised learning tasks are typically categorized into two main types:

Common Algorithms

This document covers the theory, mathematics, and practical considerations for the following foundational supervised learning algorithms:

Note: Powerful techniques like Random Forest, Gradient Boosting Machines, and Extra Trees are also supervised learning algorithms, but they are covered in detail in the Ensemble Methods document due to their nature of combining multiple models.


Linear Regression

Linear regression is a fundamental statistical method that models the relationship between a dependent variable and one or more independent variables using a linear approach. It forms the foundation for many machine learning algorithms and statistical techniques.

Mathematical Framework

Simple & Multiple Linear Regression

The relationship is modeled as: y = β₀ + β₁x₁ + ... + βₚxₚ + ε

In matrix notation: Y = Xβ + ε

Cost Function (Mean Squared Error)

The objective is to minimize the sum of squared residuals (the difference between actual and predicted values): J(β) = (1/2n) Σ(yᵢ - ŷᵢ)²

Solution Methods

  1. Normal Equation (Closed-Form Solution): A direct analytical solution for the optimal parameters. It is efficient for smaller datasets. β̂ = (XᵀX)⁻¹XᵀY
  2. Gradient Descent: An iterative optimization algorithm used when the normal equation is computationally expensive (i.e., for large datasets). The parameters are updated in the opposite direction of the cost function's gradient. β⁽ᵗ⁺¹⁾ = β⁽ᵗ⁾ - α∇β J(β⁽ᵗ⁾)

Assumptions

  1. Linearity: The relationship between features and the target is linear.
  2. Independence: Observations are independent of each other.
  3. Homoscedasticity: The variance of the error terms is constant across all levels of the independent variables.
  4. Normality: The error terms are normally distributed.
  5. No Multicollinearity: The independent variables are not highly correlated with each other.

Advantages

Limitations

Use Cases


Logistic Regression

Logistic regression is a statistical method used for binary and multiclass classification problems. It models the probability that an instance belongs to a particular class using the logistic (sigmoid) function.

Mathematical Framework

Sigmoid Function

The core of the model is the sigmoid function, which maps any real-valued number into a value between 0 and 1. σ(z) = 1 / (1 + e⁻ᶻ)

Probability Model

The probability of an instance belonging to class 1 is modeled as: P(Y = 1|x) = σ(βᵀx)

The log-odds (logit) of this probability is a linear function of the features, which is why it is considered a linear model for classification. log(P/(1-P)) = βᵀx

Cost Function (Log-Loss)

Parameters are estimated by maximizing the likelihood of the observed data. This is equivalent to minimizing the negative log-likelihood, often called the Log-Loss or Cross-Entropy Loss: J(β) = -Σ [yᵢ log(pᵢ) + (1-yᵢ) log(1-pᵢ)]

This cost function is convex, ensuring that optimization algorithms like gradient descent can find the global minimum.

Advantages

Limitations

Use Cases


Regularized Regression

Regularized regression methods are extensions of linear regression that add a penalty term to the cost function to prevent overfitting, handle multicollinearity, and perform feature selection, especially when the number of features is large.

Mathematical Framework

The general objective is to minimize: Cost = ||y - Xβ||₂² + λR(β)

Ridge Regression (L2 Regularization)

Lasso Regression (L1 Regularization)

Elastic Net

When to Use Each Method


Support Vector Machines (SVM)

Support Vector Machines are powerful supervised learning algorithms used for classification and regression tasks. The core idea is to find an optimal hyperplane that separates data points of different classes with the maximum possible margin.

Mathematical Framework

Margin Maximization

For a linearly separable dataset, SVM finds the hyperplane wᵀx + b = 0 that maximizes the geometric margin, which is the distance between the hyperplane and the nearest data point from either class. This is equivalent to minimizing (1/2)||w||₂² subject to yᵢ(wᵀxᵢ + b) ≥ 1.

Support Vectors

The data points that lie closest to the hyperplane are called support vectors. These are the critical elements of the dataset, as they define the margin. The optimal hyperplane is determined only by these support vectors.

Soft Margin SVM

To handle data that is not perfectly separable, the soft margin formulation introduces slack variables ξᵢ that allow some data points to be on the wrong side of the margin or even the hyperplane. The optimization problem becomes a trade-off between maximizing the margin and minimizing the classification error, controlled by a regularization parameter C.

The Kernel Trick

For non-linearly separable data, SVMs use the kernel trick to map the original data into a higher-dimensional space where it becomes linearly separable. The decision function is computed using inner products in this high-dimensional space, which can be calculated efficiently by a kernel function K(xᵢ, xⱼ).

Common Kernels:

Advantages

Limitations


k-Nearest Neighbors (k-NN)

k-Nearest Neighbors is a simple, non-parametric, instance-based learning algorithm. It makes no assumptions about the underlying data distribution and is considered a "lazy learning" algorithm because it does not have a dedicated training phase. Instead, it stores all training data and makes predictions by finding the k most similar training examples (neighbors) for a new, unseen data point.

Mathematical Framework

Prediction Process

  1. Calculate Distances: For a new data point x_q, calculate the distance to all points in the training set. A common choice is the Euclidean distance: d(x_q, xᵢ) = √(Σ(x_qʲ - xᵢʲ)²)
  2. Find k-Nearest Neighbors: Identify the k training points that are closest to x_q.
  3. Make a Prediction:
    • Classification: Assign the class label that is most frequent among the k neighbors (majority vote).
    • Regression: Predict the average of the target values of the k neighbors.

Choosing the value of k

The choice of k is critical and represents a bias-variance trade-off:

k is a hyperparameter that is typically chosen using cross-validation.

Advantages

Limitations


Naive Bayes Classification

Naive Bayes is a family of probabilistic classifiers based on applying Bayes' theorem with a "naive" assumption of conditional independence between every pair of features. Despite this strong assumption, it performs surprisingly well in many real-world applications, especially text classification.

Mathematical Framework

Bayes' Theorem

The foundation of the classifier is Bayes' theorem: P(y | x) = (P(x | y) * P(y)) / P(x)

Naive Independence Assumption

The key assumption is that all features are conditionally independent given the class: P(x₁, ..., xₚ | y) = Π P(xᵢ | y)

This simplifies the decision rule to: ŷ = argmax_y [log P(y) + Σ log P(xᵢ | y)]

The model learns the prior probabilities P(y) and the conditional probabilities P(xᵢ | y) from the training data.

Variants

The way P(xᵢ | y) is calculated depends on the type of data:

Advantages

Limitations


Decision Trees

Decision trees are non-parametric supervised learning algorithms that create a model to predict a target value by learning simple, hierarchical decision rules inferred from data features. They produce a tree-like structure where each internal node represents a test on a feature, each branch represents the outcome of the test, and each leaf node represents a class label (in classification) or a continuous value (in regression).

Mathematical Framework

Tree Construction (Recursive Splitting)

The tree is built using a recursive partitioning algorithm. Starting with the entire dataset at the root, the algorithm selects the best feature and split point to divide the data into two or more subsets. This process is repeated for each subset until a stopping criterion is met.

Splitting Criteria

The "best" split is chosen by maximizing an impurity reduction metric.

Pruning

A fully grown decision tree is likely to overfit the training data. Pruning is a technique to reduce the size of the tree by removing sections that provide little predictive power.

Advantages

Limitations


Gaussian Processes

Gaussian Processes (GPs) are a non-parametric, Bayesian approach to supervised learning. Instead of learning the parameters of a specific function, GPs define a probability distribution over a collection of functions. This probabilistic framework makes them exceptionally powerful for tasks where quantifying uncertainty is as important as the prediction itself.

Mathematical Framework

Definition

A Gaussian Process is a collection of random variables, any finite number of which have a joint Gaussian distribution. A GP is fully specified by its mean function μ(x) and its covariance function (or kernel) k(x, x'). f(x) ~ GP(μ(x), k(x, x'))

The kernel function k(x, x') is the key component. It defines the similarity between data points, and the choice of kernel embeds assumptions about the function being modeled (e.g., smoothness, periodicity).

Regression

Given training data D = {(xᵢ, yᵢ)}, a GP model can make predictions for new inputs x*. The model assumes that the observed target values and the function values at the new inputs are jointly Gaussian. By conditioning this joint distribution on the observed data, we obtain a posterior distribution for the function values at the new inputs.

This posterior is also a Gaussian distribution with a predictive mean and variance:

Hyperparameter Optimization

The kernel function has its own parameters (e.g., the length-scale ℓ and signal variance σ² in the RBF kernel). These are learned by maximizing the log marginal likelihood of the training data, often using gradient-based optimization.

Advantages

Limitations

Use Cases

All writing