
Models that Learn from Data
Acquire a deep understanding of statistical learning, model training, feature engineering, and performance evaluation. Build competency in supervised, unsupervised, and ensemble methods to develop algorithms that generalize, adapt, and make informed predictions.
Course: Machine Learning
Module 1: Foundations of Machine Learning
This module introduces the conceptual, mathematical, and practical foundations of machine learning. It is designed for learners with a basic background in linear algebra, probability, and calculus.
Page 1 – What Is Machine Learning?
1.1 Motivation and Context
Modern organisations collect unprecedented volumes of data from sensors, transactions, social media, electronic health records, and industrial systems. Extracting actionable patterns from such data at scale is beyond manual analytical capacity. Machine learning (ML) provides a set of algorithms and principles that allow computer systems to learn from data and improve performance on a task over time without explicit reprogramming.
In a narrow but practical sense, machine learning focuses on building models that map inputs to outputs:
- Predicting house prices from features such as size, location, and age.
- Classifying email messages as spam or not spam.
- Estimating the probability of disease from clinical measurements.
- Recommending content based on past interactions.
Rather than hard-coding a set of rules, ML systems infer the mapping by optimising a parameterised function using data. This paradigm shift enables solutions in domains where explicit rule-writing is infeasible, ambiguous, or constantly changing.
1.2 A Formal Working Definition
A widely cited definition by Tom Mitchell states: “A computer program is said to learn from experience E with respect to some class of tasks T and performance measure P, if its performance at tasks in T, as measured by P, improves with experience E.”
This definition highlights three key ingredients:
- Task (T) – what the system is trying to do (e.g., classification, prediction, control).
- Experience (E) – the data or interaction through which learning occurs.
- Performance (P) – a quantitative measure of success (e.g., accuracy, mean squared error, reward).
When designing any ML system, it is important to precisely articulate what is being learned, from which data, and how performance is evaluated.
1.3 Types of Machine Learning
Although there are many variations, most methods fall into one of the following categories:
-
Supervised learning – learning from labelled examples.
Each training instance is a pair
(x, y), wherexis the input (features) andyis the target (label). The objective is to learn a functionf(x) ≈ ythat generalises to unseen data. - Unsupervised learning – learning from unlabelled data. The goal is to uncover structure, clusters, or lower-dimensional representations in the data without explicit labels.
- Semi-supervised learning – combines a small amount of labelled data with a larger pool of unlabelled data to improve performance.
- Reinforcement learning – an agent interacts with an environment, making sequential decisions and receiving rewards. The goal is to learn a policy that maximises long-term cumulative reward.
This module concentrates primarily on classical supervised and unsupervised learning. Reinforcement learning is introduced conceptually and may be treated in a later module.
1.4 Example: Email Spam Classification
As a concrete example, consider spam detection in email systems. The:
- Task (T) is to classify an email as spam or not spam.
- Experience (E) is a dataset of emails labelled by users or experts.
- Performance (P) can be measured via accuracy or F1-score on a held-out test set.
The ML system processes each email, extracts features (such as word frequencies, sender domain, presence of suspicious links), and trains a classifier that outputs the probability that an email is spam. Deployed at scale, this system adapts over time as new types of spam emerge.
Page 2 – Mathematical Foundations of Machine Learning
2.1 Data as Vectors in Feature Space
Machine learning algorithms typically operate on data represented as
points in a feature space. Given a dataset with
n examples and d features, we can represent it
as a matrix:
X ∈ ℝ^(n × d)
where each row xi is a d-dimensional
feature vector corresponding to one observation. For supervised learning,
we also maintain a target vector:
y ∈ ℝ^n (regression) or y ∈ {1, …, K}^n (classification)
The choice and construction of features (feature engineering) strongly influences model performance and interpretability.
2.2 Hypothesis Space and Parameterisation
Machine learning selects a function from a class of candidate functions known as the hypothesis space. For instance, in linear regression, the hypothesis space consists of linear maps:
hw(x) = wᵀx + b
where w ∈ ℝ^d and b ∈ ℝ are parameters. More
complex models (such as neural networks) introduce non-linear basis
functions, hidden layers, and non-linear activation functions, but the
underlying principle remains: choose a parameterised function and find
parameter values that minimise some loss function on the training data.
2.3 Loss Functions
A loss function quantifies the discrepancy between model predictions and the true targets. Common choices include:
-
Mean squared error (MSE) for regression:
L = (1/n) Σ (yi − ŷi)² -
Cross-entropy loss for classification:
L = −(1/n) Σ Σ yi,k log ŷi,k, whereyi,kis the one-hot encoded true label. - Hinge loss for support vector machines.
The learning process searches for parameters that minimise this loss over the training set, often with additional regularisation terms to prevent overfitting.
2.4 Optimisation and Gradient Descent
Many ML models are trained via gradient-based optimisation.
Given a differentiable loss function L(w), we can compute
its gradient with respect to parameters w and iteratively
update:
w ← w − η ∇L(w)
where η is the learning rate. Variants such as
stochastic gradient descent (SGD), mini-batch SGD,
and adaptive methods (Adam, RMSProp) are used extensively in practice.
2.5 Probabilistic Viewpoint
Many algorithms can be interpreted probabilistically. For instance, linear regression with Gaussian noise assumptions corresponds to maximising the likelihood of the observed data under a Gaussian model. Logistic regression can be viewed as maximum likelihood estimation for Bernoulli-distributed outputs with a logistic link function.
The probabilistic perspective provides a principled framework for:
- Quantifying uncertainty in predictions.
- Combining prior knowledge with observed data (Bayesian methods).
- Handling missing data via probabilistic inference.
Page 3 – Supervised Learning: Regression and Classification
3.1 Supervised Learning Setup
In supervised learning, we are given a dataset of input–output pairs:
D = {(xi, yi)} for i = 1, …, n
The goal is to learn a function f that maps
x to y such that the prediction error on
new, unseen data is minimised. Two core problem types are:
- Regression: outputs are continuous values.
- Classification: outputs are discrete class labels.
3.2 Regression: Predicting Continuous Quantities
In regression, the target y is a real-valued scalar
or vector. Examples include:
- Predicting energy consumption from weather and load patterns.
- Estimating patient length-of-stay from demographic and clinical variables.
- Forecasting stock prices or demand in supply chains.
Basic regression models include:
- Linear regression.
- Polynomial regression.
- Regularised regression (Ridge, Lasso, Elastic Net).
- Tree-based regression (Random Forests, Gradient Boosted Trees).
3.3 Classification: Discrete Decision-Making
In classification, each example is assigned to one of a finite number of classes. Typical tasks include:
- Disease vs. no disease in a screening program.
- Fraudulent vs. legitimate financial transactions.
- Identifying object categories in images (e.g., cat, dog, car).
Common classification algorithms include:
- Logistic regression.
- k-Nearest Neighbours (k-NN).
- Support Vector Machines (SVMs).
- Decision trees and ensemble methods (Random Forest, XGBoost).
- Neural networks and deep learning models.
3.4 Generalisation and Overfitting
A crucial concept in machine learning is generalisation – the ability of a model to perform well on unseen data. A model that fits the training data perfectly but performs poorly on new data is said to be overfitting.
Overfitting typically arises when:
- The model is excessively complex relative to the amount of data.
- Noise or outliers are captured as if they were signal.
- Hyperparameters are tuned too aggressively on a small validation set.
Techniques to mitigate overfitting include:
- Regularisation (e.g., L2 and L1 penalties) to constrain model complexity.
- Cross-validation to obtain robust estimates of generalisation performance.
- Early stopping in iterative training procedures.
- Data augmentation and noise injection where appropriate.
3.5 Bias–Variance Trade-off
The bias–variance trade-off is a central concept describing how model complexity influences generalisation:
- High-bias models (e.g., overly simple) may underfit the data, failing to capture important patterns.
- High-variance models (e.g., overly complex) may overfit, memorising noise in the training data.
Effective machine learning requires balancing bias and variance so that the total expected error on unseen data is minimised.
Page 4 – Unsupervised Learning and Data Representation
4.1 Discovering Structure Without Labels
Unsupervised learning deals with datasets where no explicit labels are provided. Instead of predicting a target, the objective is to discover useful structure in the data, such as clusters, low-dimensional manifolds, or latent factors.
Typical goals include:
- Grouping similar customers or patients (clustering).
- Reducing dimensionality for visualisation or preprocessing.
- Detecting anomalies or rare patterns.
- Learning compact latent representations for downstream tasks.
4.2 Clustering
Clustering algorithms partition the dataset into groups such that points within a cluster are more similar to each other than to points in different clusters. Well-known methods include:
- k-means clustering.
- Hierarchical clustering (agglomerative or divisive).
- Density-based clustering (e.g., DBSCAN).
- Gaussian Mixture Models (GMMs).
Clustering is often used for exploratory data analysis, preliminary segmentation, or as a feature engineering step.
4.3 Dimensionality Reduction
High-dimensional data can be difficult to interpret and may lead to computational challenges. Dimensionality reduction techniques aim to embed data into a lower-dimensional space while preserving key structural properties.
Common methods include:
- Principal Component Analysis (PCA) – linear projection that maximises variance along successive orthogonal components.
- t-SNE and UMAP – non-linear methods suited to visualising complex manifolds in 2D or 3D.
- Autoencoders – neural network-based encoders–decoders that learn compressed latent representations.
4.4 Feature Learning and Representation Learning
Many modern approaches blur the line between supervised and unsupervised learning. Representation learning focuses on learning intermediate feature representations that make downstream tasks easier.
For example, autoencoders, contrastive learning, and self-supervised methods can learn meaningful embeddings of images, text, or time series without explicit labels. These embeddings are then used as inputs to simpler supervised models.
4.5 Anomaly Detection
Anomaly detection (or outlier detection) identifies instances that do not conform to the expected pattern of the data. Typical applications:
- Fraud detection in finance.
- Fault detection in industrial systems.
- Identifying abnormal physiological signals in healthcare.
Techniques include statistical models, distance-based methods, density estimation, and one-class classifiers. Anomalies are often rare and poorly labelled, making unsupervised or semi-supervised methods particularly relevant.
Page 5 – Machine Learning Workflow, Evaluation, and Ethics
5.1 End-to-End Machine Learning Workflow
Successful machine learning projects require more than choosing an algorithm. A typical end-to-end workflow comprises:
- Problem formulation – clearly define the task, the stakeholders, and the success metrics.
- Data collection and integration – obtain data from relevant sources, ensuring proper permissions and governance.
- Data preprocessing – handle missing values, outliers, normalisation, encoding categorical variables, and time alignment where required.
- Model selection – choose candidate models appropriate for the data type, scale, and interpretability requirements.
- Training and validation – fit models on training data, tune hyperparameters using validation sets or cross-validation.
- Evaluation – assess performance on a held-out test set using relevant metrics.
- Deployment – integrate the model into production systems, with monitoring and logging.
- Monitoring and maintenance – track performance drift, retrain as data distributions change, and manage model lifecycle.
5.2 Evaluation Metrics
The choice of evaluation metric must align with the problem context. For regression:
- Mean Squared Error (MSE).
- Root Mean Squared Error (RMSE).
- Mean Absolute Error (MAE).
- R² (coefficient of determination).
For classification:
- Accuracy.
- Precision, Recall, and F1-score.
- Receiver Operating Characteristic (ROC) curve and AUC.
- Precision–Recall curves (important with imbalanced data).
- Confusion matrix analysis for error patterns.
In imbalanced scenarios (e.g., rare disease detection), accuracy may be misleading, and metrics focused on minority-class performance (Recall, F1, AUC-PR) become more appropriate.
5.3 Data Quality and Dataset Shift
The performance of any ML system is constrained by data quality. Issues include:
- Missing or inconsistent values.
- Measurement errors and noise.
- Label noise or ambiguous labels.
- Sampling bias or non-representative training data.
Over time, dataset shift (changes in the data distribution) can degrade performance. Monitoring input distributions and output errors in production is essential, with mechanisms for retraining or recalibrating models.
5.4 Interpretability and Explainability
As ML systems influence high-stakes decisions (e.g., healthcare, finance, criminal justice), interpretability becomes crucial. Stakeholders may require:
- Feature importance scores.
- Local explanations (e.g., LIME, SHAP).
- Transparent models (e.g., decision trees, linear models).
Interpretability is not only a technical challenge but also socio-technical: explanations must be meaningful to the intended audience, not just mathematically precise.
5.5 Fairness, Ethics, and Responsible ML
Machine learning systems can unintentionally encode or amplify biases present in historical data. Responsible ML involves:
- Assessing datasets for representation bias.
- Evaluating models using fairness metrics where applicable.
- Ensuring transparency around model limitations.
- Complying with relevant legal and regulatory frameworks.
In many application domains, particularly healthcare and public policy, the ethical design and deployment of ML systems are as important as their predictive accuracy.
5.6 Summary and Looking Ahead
This first module has introduced the conceptual and mathematical foundations of machine learning, the key learning paradigms, the structure of supervised and unsupervised tasks, and the broader workflow and ethical considerations involved in deploying ML systems.
Subsequent modules in this course can build on this foundation to explore:
- Linear and logistic regression in depth.
- Tree-based models and ensemble learning.
- Neural networks and deep learning architectures.
- Model deployment, MLOps, and scalable infrastructure.
Learners are encouraged to review the definitions, key equations, and workflow steps presented in this module, as they form the conceptual backbone for the remainder of the course.
