Advanced analytics and machine learning
By the end of this module you will be able to:
- Classify machine learning problems and explain overfitting and cross-validation
- Apply feature engineering principles to improve model performance
- Distinguish stream processing from batch processing and identify the right approach for time-sensitive workloads
Microsoft Tay: a language model that overfit to adversarial inputs within 16 hours
In March 2016, Microsoft deployed Tay, a conversational AI trained on social media conversations, with the intention of it learning from interactions with Twitter users. Within 16 hours, coordinated groups of users had fed Tay a continuous stream of offensive content. The model learned from these interactions in real time, producing increasingly harmful outputs. Microsoft took Tay offline after less than a day.
The failure illustrates a fundamental challenge in machine learning: a model trained on a sample of the data distribution may behave unpredictably on inputs that fall outside that distribution (out-of-distribution inputs), particularly adversarial inputs deliberately designed to exploit the model. Tay had no mechanism to detect that the inputs it was receiving differed systematically from its training distribution.
The incident accelerated adversarial testing as a research area and contributed to the safety evaluation frameworks now standard in large language model deployment. Every model deployed to real users encounters a noisier, more adversarial distribution than its training set. Measuring this gap is part of responsible ML deployment.
Tay was trained on public Twitter conversations. Its training process had no safeguards against adversarial examples. What does this teach about the difference between in-distribution and out-of-distribution inputs?
The learning signal decides how a model learns, what evidence it can use, and what failure modes must be tested before deployment.
23.1 Machine learning taxonomy
Machine learning problems are classified by the structure of the learning signal. Supervised learning uses labelled examples: each training example pairs an input with the correct output. Classification (predicting a discrete label) and regression (predicting a continuous value) are both supervised. Examples include spam detection, image classification, and house price prediction.
Unsupervised learning finds structure in unlabelled data. Clustering (k-means, DBSCAN, hierarchical clustering) groups similar examples. Dimensionality reduction (PCA, UMAP, t-SNE) compresses high-dimensional data to lower dimensions for visualisation or downstream processing. Anomaly detection identifies points that differ from the learned distribution.
Reinforcement learning trains an agent to take actions in an environment to maximise a cumulative reward signal. It requires no pre-labelled dataset but requires a defined reward function and either a simulation or a real environment. Applications include game playing (AlphaGo), robot control, and recommendation system optimisation where explicit labels are unavailable.
Self-supervised learning (the technique underlying transformer language models) creates labels from the data itself. BERT masks tokens and trains the model to predict them. GPT-style models train on next-token prediction. The label is derived from the input, which makes it possible to train on the entire web without manual annotation.
Once the learning signal is clear, the next question is whether the model can generalise beyond the examples it has already seen.
“All models are wrong, but some are useful.”
George E. P. Box, Journal of the American Statistical Association (1976) - Science and Statistics
Box's point is practical, not cynical. A model is judged by the decision it supports, the cost of being wrong, and the evidence that it will behave acceptably outside the training sample.
23.2 Overfitting, underfitting, and cross-validation
Overfitting occurs when a model learns the training data too well, including its noise and idiosyncrasies, and fails to generalise to new data. A decision tree with no depth limit will perfectly classify all training examples by memorising them, but will perform no better than chance on unseen data. High training accuracy with poor validation accuracy is the diagnostic signal.
Underfitting occurs when a model is too simple to capture the underlying structure of the data. A linear model applied to data with clear nonlinear relationships will underfit: both training and validation accuracy will be poor. The bias-variance trade-off describes this tension: high-bias (simple) models underfit; high-variance (complex) models overfit.
k-fold cross-validation addresses the problem of having too little data for a proper train/validation/test split. The dataset is divided into k folds. The model is trained k times, each time using k-1 folds for training and the held-out fold for validation. The k validation scores are averaged to estimate generalisation performance. Five-fold and ten-fold cross-validation are standard in practice.
Feature engineering transforms raw data into representations that make the underlying patterns more accessible to the model. Temporal features (day of week, hour of day, days since last purchase) extracted from timestamps often improve performance more than adding new raw features. Interaction terms (product of two features) capture non-additive effects. Scaling (standardisation, normalisation) is required for distance-based algorithms and gradient-based optimisation.
Common misconception
“More data always improves model performance.”
More data helps when the model is underfitting due to insufficient training examples. But additional data does not fix: a model applied to the wrong problem (wrong features), a model with a fundamental architecture mismatch, label noise in the training data, or distribution shift between training and deployment environments. The quality and representativeness of data matters more than quantity. A model trained on 10 million biased examples will be more confidently wrong than one trained on 100,000 balanced examples.
Common misconception
“Deep learning always outperforms traditional statistical methods.”
For many structured tabular problems, gradient-boosted trees or regularised linear models remain strong baselines. Neural networks are usually strongest where the raw signal is high-dimensional and weakly structured, such as text, image, audio, or embedding-heavy recommender systems. Start with the simplest credible baseline, then make any added model complexity earn its operational cost.
Evaluation answers whether a model should be trusted; processing architecture answers whether its result arrives while the decision is still alive.
23.3 Stream processing and time-sensitive analytics
Batch processing computes analytics over a fixed dataset: all the data from yesterday is processed in a single job that runs each night. Latency is acceptable (hours or overnight) because the output does not need to be real-time. Batch is simpler to reason about, easier to debug (fixed input, deterministic output), and less expensive to operate.
Stream processing computes analytics over a continuous, unbounded stream of events as they arrive. Apache Kafka handles ingestion and durability; Apache Flink, Apache Spark Streaming, and Amazon Kinesis handle the processing logic. Stream processing is required when the latency of batch is unacceptable: fraud detection (a fraudulent transaction must be flagged in milliseconds before it is authorised), system monitoring (an alert on a CPU spike must fire within seconds), and real-time personalisation.
Windowing is the mechanism stream processors use to compute aggregations over time: a tumbling window aggregates events within a fixed, non-overlapping time period (all events in the last 5 minutes); a sliding window aggregates events within a rolling period (all events in the 5 minutes ending at each second); a session window groups events that occur within a gap threshold of each other.
Graph analytics applies algorithms to graph-structured data. PageRank (the original Google algorithm) measures node importance by iteratively spreading rank from high-importance nodes to their neighbours. Community detection identifies densely connected clusters. Shortest path algorithms (Dijkstra, Bellman-Ford) power navigation and network routing. Graph neural networks extend deep learning to graph structures, enabling fraud detection at scale.
Analytics that feeds an AI system also needs governance over the data lifecycle. ISO/IEC 5259 sets out data quality management and governance for analytics and machine learning; ISO/IEC 8183 frames the AI data lifecycle from planning through decommissioning; ISO/IEC 42001 defines an AI management system; NIST AI RMF organises risk work around govern, map, measure, and manage; and EU AI Act Article 10 requires high-risk AI systems to use training, validation, and testing data that is relevant, representative, and, to the best extent possible, free of errors and complete. In practice, a fraud model is not production-ready until the team can show the dataset lineage, label definition, representativeness checks, bias checks, validation split design, drift monitor, rollback path, and named accountable owner.
A data scientist builds a customer churn prediction model. After training, the model achieves 97% accuracy on the training set but only 61% accuracy on the validation set. What is happening, and what is the most appropriate intervention?
A fraud detection system must flag suspicious transactions before they are authorised, with a maximum acceptable latency of 200 milliseconds from transaction submission to decision. Which processing architecture is appropriate?
A feature engineering step converts a raw transaction timestamp (2026-04-01 14:32:07 UTC) into: day_of_week=Tuesday, hour_of_day=14, is_weekend=False, days_since_account_opening=187. Why is this transformation typically beneficial for fraud detection models?
A model achieves 99.2% accuracy on the training set but only 71.4% on the test set. What is the most likely problem and the standard first response?
The 200ms fraud decision above assumed a platform that could already carry the event, keep the trained model in memory, and account for where each feature came from. Architectures and pipelines works through what building that platform costs: whether transformation runs before the load or inside the warehouse, whether raw events sit in a lake or under a lakehouse table format, and which layer notices when a feature stops arriving.
Core checks before moving on
- ML problems are classified by learning signal: supervised, unsupervised, reinforcement, and self-supervised learning each create different evidence and governance needs.
- Overfitting is a generalisation failure, not a high-score success. Use train, validation, and test separation, cross-validation where appropriate, and production monitoring for drift.
- Feature engineering, label quality, and representative data often decide model value before algorithm choice does.
- Stream processing is for decisions whose value expires quickly. Batch processing is for fixed datasets where correctness, cost, and repeatability matter more than sub-second latency.
- AI data governance now needs explicit evidence: dataset origin, labelling process, representativeness, bias checks, validation design, monitoring, and accountable ownership.
Standards and sources cited in this module
Scikit-learn documentation: Cross-validation
Practical guide to k-fold, stratified, and group cross-validation with Python examples.
Apache Kafka documentation: Streams
Reference for stream processing concepts including windowing, state stores, and exactly-once semantics.
Andriy Burkov, 'The Hundred-Page Machine Learning Book' (2019)
Concise, practical ML taxonomy covering supervised, unsupervised, and reinforcement learning with the bias-variance trade-off.
NIST AI Risk Management Framework (AI RMF 1.0), January 2023
Defines govern, map, measure, and manage as the core functions for AI risk management.
ISO/IEC 5259 series: data quality for analytics and machine learning
Current ISO source for data quality management requirements and guidance in analytics and ML.
ISO/IEC 42001:2023 AI management system
International standard for establishing, implementing, maintaining, and improving an AI management system.
EU AI Act Article 10: Data and data governance
Legal requirements for data governance, representativeness, bias checks, and data quality in high-risk AI systems.
Kleppmann, M. (2017). Designing Data-Intensive Applications. O'Reilly Media
Chapters on stream processing, batch processing, and the Lambda architecture are the definitive practitioner reference.
Module 19 of 52 · Modelling and statistics