Skip to content
Pixel
العربية

7-1Unit 7 · Machine Learning and Artificial Intelligence

The Basics of Machine Learning

12 min readStart exercises
In this lesson
  1. The Big Idea
  2. The Idea of Machine Learning: Rules Come from Data
  3. Three Types of Learning Methods
  4. Classification: The Classic Example of Supervised Learning
  5. Clustering: The Classic Example of Unsupervised Learning
  6. Reinforcement Learning: Learning from Rewards
  7. The Limits of Machine Learning: Bias and the Black Box
  8. Try It Yourself
  9. Common Mistakes
  10. Key Points
  11. FAQ

The Big Idea

In conventional programming, people write the rules and the computer follows them. In machine learning (ML) we give the computer data and examples, and it works out the rules itself, in the form of a model that can judge new data. This lesson focuses on three main ways of learning: supervised (from examples with correct answers), unsupervised (looking for structure in data with no answers) and reinforcement (learning from rewards by trial and error). In every case the results are only as good as the data, which is why machine learning supports human judgment rather than replacing it.

The Idea of Machine Learning: Rules Come from Data

How would you write a program that tells a photo of a cat from a photo of a dog? Try writing the rules: "If the ears are pointed, it's a cat"… but some dogs have pointed ears, some photos are taken from the side or in poor light. You would end up writing hundreds of exceptions and never finish.

Machine learning takes a different route. We give the system training data, thousands of photos each with its correct answer, and an algorithm gradually adjusts the model's parameters until it has learned patterns that help it predict new examples it has never seen.

The book's definition (Arabic edition): machine learning is a field that uses algorithms which learn patterns from data to build a model that performs a specific task, such as predicting, classifying or supporting a decision on new data. It is one of the main approaches used to build AI systems.

Conventional programming vs machine learning

The key difference is who supplies the rules:

| Approach | What humans provide | What the computer does | |---|---|---| | Conventional programming | Decision rules (procedures) and input data | Follows the rules to produce answers | | Machine learning | Large amounts of data and examples of correct answers | Finds the decision rules from the data on its own |

Conventional programming vs machine learning Two panels. In conventional programming, people provide decision rules plus input data; the computer follows the rules and outputs answers such as spam or normal. In machine learning, people provide lots of data plus the correct answers (a label per example); the computer trains, finds the patterns, and the output is the rules in the form of a model that is used on new data. Footer: in machine learning the rules are an output, not an input. 1) Conventional programming Decision rules written by people Input data e.g. a new email Computer follows the rules Answers spam or normal 2) Machine learning Lots of data thousands of emails Correct answers a label per example Computer trains finds the patterns Rules (a model) used on new data In machine learning the rules are an output, not an input
The roles flip: in conventional programming the rules go in; in machine learning the rules come out.

When do we use machine learning?

We use it for problems where it is hard for people to put the rules into words, because the rules are too complex to write down completely. The book's examples:

  • Deciding whether a photo shows a dog or a cat.
  • Telling a spam email from a normal one.
  • Turning speech into text.

A task with a simple, clear rule, such as adding two whole numbers, needs no learning at all: the rule is known and exact, and ordinary programming always gets it right.

People are still in the picture

It can look as if the computer "learns by itself", but people define the task, choose the data, set the objective function (what the model tries to improve) and decide how it is evaluated. So the model never works independently of their choices, and its results depend on the data, the training and the evaluation.

Three Types of Learning Methods

The lesson focuses on three main types of machine learning; other methods exist too. They differ in what data is available and what is learned:

| Learning method | Data given | What is learned | |---|---|---| | Supervised learning | Examples of inputs with known target values or classes (labels) | A model that maps inputs to the target value or class | | Unsupervised learning | Data with no known targets | Possible patterns, structure or groups in the data | | Reinforcement learning | States, actions and rewards from interacting with an environment | A policy for choosing actions that give the best results over time |

A label is the known correct answer for each example in the training data: the word "dog" next to a photo of a dog, or "spam" next to a spam email.

The three types of machine learning Three columns. Supervised learning: inputs with known labels, such as Image 1 Dog, Image 2 Cat, Image 3 Dog; it learns to map input to label; example: spam filtering. Unsupervised learning: inputs only, shown as grey points inside three dashed circles; it learns possible structure or groups; example: grouping customers. Reinforcement learning: an agent sends an action to the world and receives a reward and a new state; it learns a policy for choosing actions; example: a game or simulation. Footer: the key question is whether there are correct answers (labels) or rewards from interaction. The three types of machine learning Supervised Data given Inputs + known labels Image 1 Dog Image 2 Cat Image 3 Dog What is learned Map input to label e.g. spam filtering Unsupervised Data given Inputs only, no labels What is learned Possible structure/groups e.g. grouping customers Reinforcement Data given States, actions, rewards Agent Environment action reward + state What is learned A policy for actions e.g. a game or simulation Key question: are there correct answers (labels)? Or rewards from interaction?
Ask first: are there correct answers, none at all, or rewards from interaction?

How do you choose the right method?

The choice depends on the data you have and the nature of the problem:

| Situation | Suitable method | |---|---| | The training data has known target values or classes, and you want predictions for new data | Supervised learning | | You want to explore structure or patterns in data with no known targets | Unsupervised learning | | You can define a goal or reward, and an agent can interact repeatedly with a suitable environment | Reinforcement learning |

Classification: The Classic Example of Supervised Learning

Classification is a method of predicting which type (class) the input data belongs to. It is a representative example of supervised learning:

  • Sorting emails into "spam" or "normal".
  • Sorting medical images into "abnormal" or "normal".
  • Classifying animals in photos as "dog", "cat" or "bird".

Classification needs training data with examples labelled with known classes. How much data is needed is not a fixed number; it depends on the task, the data quality and the model. Classification has two stages:

| Stage | What happens | |---|---| | Training | The model's parameters are learned from examples of inputs and their target classes | | Prediction | What the model learned is used to decide the class of new data |

Features: the clues the model relies on

A feature is information in the training data that serves as a useful clue for prediction. In spam classification, features include the kinds of words in the email, the sender's address and the length of the text.

Imagine the model plotting each email as a dot: "number of suspicious words" across, "message length" up. During training it learns a boundary between the "spam" dots and the "normal" dots. To predict, it checks which side of the boundary the new email falls on.

Choosing suitable features, or learning a suitable representation of them, affects how well the model performs:

  • Some features are chosen by the developer by hand.
  • Some modern models learn their representations automatically (you will see this in the neural-networks lesson).
  • Avoid data leakage, where a feature quietly reveals the answer in a way that won't be available in real use, and avoid unnecessary bias, such as relying on a feature that has nothing to do with the task but disadvantages a group of people.

Clustering: The Classic Example of Unsupervised Learning

Clustering divides data into clusters according to the features, the similarity measure and the settings used. It may reveal a possible grouping structure in the data, but it does not prove that real, "natural" groups exist.

Example: an online shop has data on its customers: how often they buy and how much they spend. Nobody knows in advance who belongs to which group. A clustering algorithm puts similar customers together in clusters, and then a person takes over: they look at each cluster and interpret it ("regular customers who spend a little each time") and use it in marketing and product development.

Classification vs clustering in one word: labels

| Method | Correct answers (labels) | Purpose | |---|---|---| | Classification | Given in advance | Predict which class new data belongs to | | Clustering | Not given | Look for a possible grouping structure according to the similarity measure used |

Classification vs clustering Two panels plotted on Feature 1 and Feature 2. Classification: blue points labelled spam and green points labelled normal, separated by a dashed boundary learned by the model; a new point marked with a question mark falls on the normal side, so it is classified as normal. Clustering: grey points with no labels, enclosed by three dashed circles called Cluster A, Cluster B and Cluster C; the algorithm only measures similarity, and a human interprets what each cluster means. Footer: the difference in one word is whether labels are given or not given. Classification (supervised) Feature 1 Feature 2 boundary ? label: spam label: normal The new point falls on the "normal" side Clustering (unsupervised) Feature 1 Feature 2 Cluster A Cluster B Cluster C No labels: only similarity is measured A human interprets each cluster The difference in one word: labels given, or not given
In classification we know the class names in advance; in clustering we don't. The algorithm suggests groups and a person interprets them.

The designer decides what "similar" means

The algorithm measures similarity using the features, the measure and the settings chosen by the designer. Some algorithms, such as k-means, need the number of clusters set in advance, while others don't need it in the same way. So the clusters can change if we change the features or the number of clusters, and after running it the results must be interpreted and checked for whether they make sense.

Clustering is useful when you want an overall picture of the data or a fresh way of looking at it.

Reinforcement Learning: Learning from Rewards

In reinforcement learning an agent (the learning side) interacts with an environment: it chooses actions, receives reward signals and learns a policy that aims to maximise the total expected reward over time. A typical example is training inside a game or a suitable simulation.

Its three elements:

| Element | Description | |---|---| | State | The situation the agent is in | | Action | The choices available in that situation | | Reward | A numerical signal the agent gets from the environment after an action or during the interaction, steering learning towards a goal |

Example: a program learning a racing game. State: the car's position and speed. Actions: left, right, accelerate, brake. Reward: positive points for progress, negative for crashing. At first it moves almost at random; after thousands of attempts it learns which actions bring more reward in the long run, not just in the next step.

What makes it different from the other two?

In supervised learning there is a clear mapping: "this input has this correct answer". In reinforcement learning the correct answer is not fixed to a single possibility: nobody tells the agent the right move; it learns better choices gradually from the results of the actions it tried.

Reinforcement learning suits tasks where a goal or reward can be defined and the agent can interact with a safe, suitable environment. That is why simulators and games are so common: they let the experiment repeat thousands of times without real-world risk.

The Limits of Machine Learning: Bias and the Black Box

Machine learning's strength is that it learns from data, and that is also its weak point. The book names two important limits:

1) Bias

Skewed data leads to skewed judgments. A model only knows what it has seen. If a face-recognition system was trained mostly on photos of one group of people, it may work poorly for other groups because it never saw enough examples of them. The fix starts with the data: make it representative and balanced across every group the system will be used with, then measure performance for each group separately.

2) The black-box problem

The basis of a judgment is hard to see. Some models give you an answer, but people can't easily tell why. If a teacher acts on a model's flag saying "this student is at risk" without understanding the reason, they might treat the student unfairly or miss the real cause of the problem.

Human-in-the-loop

For these two reasons machine learning is used to support human judgment, not replace it. One way to do this is a human-in-the-loop: a person checks or approves the model's output before a decision is made. The working rule: the higher the cost of a mistake for people, the more a human must stay in charge.

Example: a bank uses a model that reads loan applications and flags the ones that look risky. That is classification (supervised), learned from past applications whose outcome is known, using features such as monthly income. But the loan officer reviews each flagged application and decides, because past data may be biased and the model may not explain its flag.

Try It Yourself

Try
Which type of learning?
Sort each situation by the learning method that fits it best.
Items (8)
Tap an item to select it, then tap the category it belongs to.
Nothing here yet
Nothing here yet
Nothing here yet
Try
Building a spam classifier
Put the steps for building and using a supervised classification model in order, from start to finish.
1. Collect past emails with a correct label on each one: spam or normal
2. Define the task: tell spam emails from normal ones
3. Use the model to predict the class of a new email it has never seen
4. Evaluate the model and check its errors and bias before relying on it
5. Train the model on the labelled examples so it learns the boundary between the two classes
6. Choose useful features such as the words, the sender's address and the text length
Use the up and down buttons to move each step, then press Check.
Try
The school's student-support model
The situation
A school in Cairo has several years of data and wants a model that flags early the students who may need extra support. Your team discovers that the training data only includes students who asked for help themselves. What is the best advice for the school management?
What would you do? You can try more than one choice.

Common Mistakes

"Machine learning means the computer works with no human involvement"

People define the task, choose the data, set the objective and the evaluation, and then review the results.

The computer finds the rules from data, but inside a frame people design, and it needs oversight that grows with the stakes of the decision.

"Classification makes a few groups and clustering makes many groups"

The number of groups isn't the difference, and neither is the type of data (numbers or text).

The difference is labels: classification learns from data with correct answers given; clustering works on data without correct answers.

"Clustering discovers the real groups that exist in nature"

Clusters change when you change the features, the similarity measure or the settings, such as the number of clusters in k-means.

Clustering suggests a possible structure; it doesn't prove the groups are "natural". A person interprets the results and checks they make sense.

"In reinforcement learning we give the agent the correct move for every situation"

That describes supervised learning, where each input has a correct answer.

In reinforcement learning there is no fixed correct answer; the agent tries actions, receives rewards, and learns a policy that increases the total reward over time.

"More data automatically makes a model fair and correct"

Lots of skewed data just produces skewed judgments with more confidence.

What matters is good, representative data for every group, measuring performance per group, and keeping a human in the loop.

Key Points

  • Machine learning uses algorithms that learn patterns from data to build a model for a task on new data; the rules are an output the computer discovers, not an input a person writes.
  • It suits problems whose rules are hard to put into words (images, spam, speech); a task with a simple rule, such as adding two numbers, doesn't need it.
  • Supervised learning learns from examples with labels; its classic example is classification, which relies on features and has a training stage and a prediction stage.
  • Unsupervised learning looks for structure in unlabelled data; its classic example is clustering, which suggests clusters using a similarity measure chosen by the designer, and a person interprets them.
  • Reinforcement learning: an agent interacts with an environment (state, action, reward) and learns a policy that maximises total reward over time, often in a safe simulation.
  • Classification vs clustering in one word: are the labels given or not?
  • Bias: skewed data gives skewed judgments. Black box: the basis of a judgment is hard to see.
  • So machine learning supports human judgment instead of replacing it, with a human in the loop as the stakes rise.

FAQ

What is the difference between supervised and unsupervised learning, in short?

Supervised learning learns from examples whose correct answers (labels) are known, so it can predict the answer for new data; its classic example is classification. Unsupervised learning works on data with no answers and looks for possible structure or groups in it; its classic example is clustering.

Is reinforcement learning a kind of supervised learning because it has rewards?

No. A reward is not the correct answer for each input; it is a numerical signal scoring how good an action's result was. The agent is never told what is right; it tries actions and learns from the rewards a policy that increases their total over time, which is why it is a separate third type.

What is a feature, and is it the same as a label?

No. A feature is a piece of information in the input that helps prediction, such as the words in an email, the sender's address or its length. A label is the known correct answer for an example, such as spam or normal. The model uses the features to predict the label.

Why shouldn't a machine-learning model make important decisions on its own?

For two reasons the book gives: bias, because skewed data produces skewed judgments, and the black-box problem, because it is hard to see why the model decided something. So we use it as a tool that supports people, and keep a responsible human in decisions that affect people.

Done reading? Test yourself