Chapter 1: Introduction to Machine Learning#
Welcome to machine learning — teaching computers to learn from experience. In this chapter, we'll explore the basic ideas behind everything from recommendation systems to self‑driving cars, and get ready to turn data into useful insights.
The Big Picture#
Machine learning (ML) answers a simple but powerful question: can a computer get better at a task just by looking at data, without being told exactly what to do? This chapter gives you a map of what ML is, the main ways to train models, and how a typical project goes from a real problem to a working solution. By the end, you'll understand the differences between supervised, unsupervised, and reinforcement learning, why deep learning and natural language processing matter so much, and how defining the problem clearly guides everything else.
Problem Definition and the ML Workflow#
Before any code or data, the most important step is to ask: What exactly are we trying to do, and how will the solution be used? Good machine learning is not just about algorithms — it’s about solving a well‑defined problem.
Think of building an ML system like cooking a meal. You need to know who you’re cooking for, what they want to eat, what ingredients you have, and how you’ll know if the dish is good. In ML, we do that through problem definition: we pin down the goal, the available data, the assumptions we can make, and the way success will be measured.
A typical ML life‑cycle, after the problem is defined, includes:
- Collect and clean data – gather relevant examples and fix missing or messy values.
- Explore and understand – look for patterns, imbalances, and surprises.
- Preprocess and create features – turn raw data into useful inputs for a model.
- Choose and train a model – pick the right type of learner (supervised, unsupervised, etc.) and teach it with data.
- Evaluate – check how well the model works on fresh examples it hasn’t seen.
- Deploy and monitor – put the model into a real system, track its performance, and update as needed.
Throughout this whole process, the original problem definition acts as your north star. It prevents you from building a brilliant model that solves the wrong problem.
Problem definition: A clear statement of the goal, the data that can be collected, the assumptions about the world, and the way the model’s output will be used in practice.
📝 Section Recap: A machine learning project starts with a well‑defined problem. The workflow then moves through data collection, model building, evaluation, and deployment, all anchored to the original goal.
Supervised Learning: Learning from Labeled Examples#
Supervised learning is like studying with an answer key. You give the algorithm a dataset where each example has both input information and the correct output (the label). The model learns a mapping from inputs to outputs, and later it can predict labels for new, unseen inputs.
Features: The measurable properties or characteristics of the data that we feed into the model. For example, in house price prediction, features might be the size, number of bedrooms, and location.
Imagine you want to predict the price of a house. You have a table of past sales with features like size, number of bedrooms, and location — and, crucially, the actual sale price. That price is the label. A supervised regression model finds the relationship between those features and the price.
There are two main flavours of supervised learning:
-
Classification – the output is a category or a class. Examples: is an email spam or not? Is this image a cat, a dog, or something else? Does a patient have a disease? The model assigns a discrete label.
-
Regression – the output is a continuous number. Examples: house price, temperature tomorrow, a person’s age from a photo.
Mathematically, supervised learning tries to approximate a function
We measure that closeness with a loss function — a formula that tells us how bad a wrong prediction is. For regression, a common loss is the mean squared error:
Labeled data: A dataset where each example includes the “right answer” (the label) alongside the input features.
Generalisation: The ability of a model to perform well on new, unseen data — not just memorising the training set.
A key risk in supervised learning is overfitting: the model memorises the training set quirks and fails on new data. We combat it by using a separate test set — data the model never saw during training — and by keeping models simple enough to capture patterns, not noise.
📝 Section Recap: Supervised learning uses labeled data to learn input‑output mappings. It splits into classification (categorical outputs) and regression (continuous outputs), and success is measured by how well the model generalises.
Unsupervised Learning: Finding Hidden Structures#
Unsupervised learning works without an answer key. The data comes with no labels — only inputs. The goal is not to predict a known output, but to discover patterns, groupings, or structure that exist in the data itself.
Imagine a music streaming service that knows what songs you’ve listened to but doesn’t have a label “like” or “dislike”. Unsupervised learning can group users with similar taste or group songs that tend to be played together. No right answer is given; the algorithm finds natural clusters.
Common unsupervised tasks include:
-
Clustering – grouping similar examples together. For instance, segmenting customers into distinct groups based on their purchasing behaviour. Popular algorithms: k‑means, hierarchical clustering, DBSCAN.
-
Dimensionality reduction – compressing data into fewer, more meaningful measurements while keeping the important structure. This is extremely useful for visualising high‑dimensional data or speeding up other algorithms. Techniques like Principal Component Analysis (PCA) and t‑SNE are widely used.
-
Anomaly detection – finding rare, unusual items that don’t fit normal patterns. This is often done by learning what “normal” looks like and flagging big deviations.
-
Association rule learning – discovering interesting relationships between items, such as market‑basket analysis (“people who buy bread also buy butter”).
Unsupervised learning is harder to evaluate than supervised learning because there is no gold‑standard output to compare against. We often rely on measures like cluster compactness, visual inspection, or domain‑specific usefulness.
📝 Section Recap: Unsupervised learning finds hidden patterns in unlabeled data through clustering, dimensionality reduction, or anomaly detection. It’s about discovery rather than prediction.
Reinforcement Learning: Learning by Trial and Error#
Reinforcement learning (RL) is about learning to make sequences of decisions. Unlike supervised learning, where the right answer is given for each example, RL provides only a reward signal — a number that tells the agent how well it’s doing after it takes actions.
Think of training a dog. You don’t tell the dog the exact right sequence of muscle movements; instead, you give a treat when it does something good and a scolding when it does something bad. Over time, the dog learns a policy — a strategy for choosing actions that leads to the most treats.
In an RL system, the agent interacts with an environment. At each step, the agent observes the current state, selects an action, and the environment transitions to a new state and gives back a reward. The agent’s goal is to maximise the total cumulative reward over time.
Key RL concepts:
- Agent – the learner or decision‑maker.
- Environment – everything the agent interacts with.
- State – a snapshot of the environment at a given moment.
- Action – one of the choices the agent can make.
- Reward – immediate feedback from the environment after an action.
- Policy – the agent’s strategy mapping states to actions. It can be deterministic or stochastic.
- Value function – an estimate of the long‑term reward expected from a particular state or state‑action pair.
A classic example is a game‑playing agent. In chess, the state is the board position, actions are legal moves, and the reward might be +1 for a win, -1 for a loss, and 0 during the game. The agent learns to choose moves that lead to high future rewards.
RL problems come in many forms, from simple grid worlds to complex robotics and game AI. They require balancing exploration (trying new actions to discover their effects) and exploitation (using known good actions to get rewards). Too much exploration wastes time; too much exploitation can miss better strategies.
Markov Decision Process (MDP): A way to describe a decision‑making situation where outcomes are partly random and partly under the control of a decision maker. RL problems are often formalised as MDPs.
📝 Section Recap: Reinforcement learning teaches an agent to make sequential decisions by maximising a cumulative reward signal. It differs from supervised learning because the feedback is indirect and delayed, requiring a balance between exploration and exploitation.
Deep Learning#
Deep learning is a subfield of machine learning built on artificial neural networks with many layers (hence “deep”). These networks are loosely inspired by the way neurons in the brain work, but they are mathematical functions that learn hierarchical representations of data.
An early, simple neural network might have only one or two layers. Deep learning uses designs with tens or hundreds of layers, each one turning the data into slightly more abstract forms. For example, in an image recognition task, early layers might detect edges, middle layers combine edges into shapes, and later layers combine shapes into objects like eyes or wheels.
The key invention that made deep learning work so well is backpropagation combined with powerful computers. During training, the network makes a prediction, compares it to the true label (in supervised settings) using a loss function, and then uses backpropagation to send the error backwards through all the layers, adjusting millions of weights. This lets the network automatically learn useful features from raw data, reducing the need for manual feature engineering.
Backpropagation: An algorithm that computes how much each weight in a neural network contributed to the overall error, and then adjusts the weights to reduce that error. It works by sending error information backward through the network.
Deep learning has achieved breakthrough results in:
- Computer vision – recognising objects, faces, and scenes in images and video.
- Speech recognition – turning spoken words into text.
- Natural language processing – understanding and generating human language (see next section).
- Generative models – creating realistic images, music, or text.
However, deep learning typically needs large amounts of labeled data and a lot of computational resources. It also can be a “black box” — it’s often hard to explain exactly why a deep network made a particular decision. Still, it’s one of the most exciting areas in modern ML and underlies many services we use every day.
Neural network: A computational graph of interconnected nodes (neurons) that apply simple mathematical operations to their inputs. Depth refers to the number of layers between input and output.
📝 Section Recap: Deep learning uses many‑layered neural networks to automatically learn hierarchical features from data. It has revolutionised fields like vision and language, but demands big datasets and computing power.
Natural Language Processing#
Natural Language Processing (NLP) is the branch of machine learning that deals with human language — text and speech. Its goal is to enable computers to read, understand, interpret, and even generate language in a way that is both meaningful and useful.
Every tweet, news article, medical record, or customer review is a piece of natural language. NLP sits at the intersection of linguistics, computer science, and statistics. Some classic NLP tasks include:
- Text classification – assigning a label to a piece of text, like sentiment analysis (positive/negative), spam detection, or topic labeling.
- Named entity recognition (NER) – identifying and pulling out names of people, organisations, dates, and other specific terms.
- Machine translation – converting text from one language to another.
- Question answering and chatbots – understanding a user’s question and retrieving or generating a correct answer.
- Text summarisation – distilling a long document into a short, coherent summary.
In the past, NLP relied heavily on hand‑crafted rules and statistical models like n‑grams (short sequences of consecutive words). Today, deep learning has transformed NLP. Models such as recurrent neural networks (RNNs), which process sequences step by step; transformers, which can look at all words in a sentence simultaneously; and large pre‑trained language models (like BERT and GPT) learn contextual representations of words, capturing meaning far better than earlier methods.
A major challenge in NLP is that language is ambiguous and context‑dependent — the word “bank” could mean a financial institution or the side of a river. Modern NLP systems handle this by training on enormous amounts of text and learning to predict words based on surrounding context.
Tokenisation: The process of splitting text into smaller chunks (words, subwords, or characters) before feeding it to a model.
Word embedding: A dense vector representation of a word that captures its meaning in a semantic space, where similar words are close together.
NLP is not a separate type of machine learning like supervised or unsupervised; it is an application domain. You can use supervised, unsupervised, or reinforcement learning for NLP tasks. For example, training a spam filter is supervised NLP; clustering news articles by topic is unsupervised NLP.
📝 Section Recap: Natural Language Processing applies ML to human language, enabling tasks like translation and sentiment analysis. Deep learning has dramatically improved NLP by learning contextual meaning from massive text corpora.
Summary#
We began with the idea that machine learning is about building systems that improve from experience. The first and most crucial step is defining the problem clearly — what are we trying to solve, and how will we know when we’ve succeeded? From there, you can choose your approach: if you have labeled data and want to predict something, supervised learning is your go‑to. If labels are missing and you want to explore or simplify, unsupervised learning shines. And if your problem involves making a sequence of decisions with delayed feedback, reinforcement learning is the right framework. Deep learning, with its many‑layered neural networks, powers many of today’s most useful applications, especially in vision and language. Finally, natural language processing brings machine learning to the words we speak and write every day, opening up a world of communication and understanding.
| Key idea | What it means (plain English) | Why it matters |
|---|---|---|
| Machine Learning | A field where computers learn patterns from data instead of being explicitly programmed with rules. | It’s the foundation for modern AI systems that adapt and improve over time. |
| Problem Definition | Clarifying the goal, the data, assumptions, and how success will be measured before building a model. | Without a clear problem, you can build a brilliant model that solves nothing useful. |
| Supervised Learning | Training a model on input‑output pairs (labeled data) so it can predict outputs for new inputs. | This is the most common ML approach for tasks like spam detection, pricing, and medical diagnosis. |
| Unsupervised Learning | Finding hidden structure in data without any labels — clustering, simplifying, or detecting anomalies. | It helps discover customer segments, compress data, and spot unusual patterns. |
| Reinforcement Learning | An agent learns to take actions in an environment to maximise a long‑term reward signal. | It’s ideal for sequential decision‑making problems like game playing, robotics, and dynamic pricing. |
| Deep Learning | Using many‑layered artificial neural networks to automatically learn complex features from data. | It has achieved state‑of‑the‑art results in vision, speech, and language tasks, often replacing manual feature engineering. |
| Natural Language Processing (NLP) | Bridging human language and computers — understanding, interpreting, and generating text. | It enables search engines, virtual assistants, translation, and analysis of massive text data. |
| Overfitting | When a model memorises the training data too closely and fails on new, unseen examples. | Avoiding overfitting is crucial for building models that work in the real world, not just on static datasets. |