Chapter 18: Data Modelling and Evaluation

Every morning before the first bell, the canteen in Meera’s school has to decide one thing: how many bottles of chilled buttermilk to prepare. Get it wrong in one direction and the bottles sit unsold till evening. Get it wrong in the other and the queue at lunch goes away empty-handed.

The canteen in-charge keeps a small notebook. For each day she writes down two things — the highest temperature that day, and how many bottles were sold. After a few weeks the notebook has a story in it. On cool days the number is small. On hot days it is large. Nobody wrote a rule saying so. The pattern was simply there, waiting in the numbers.

Now the useful question. Tomorrow’s forecast says it will be hot. Can those old numbers tell her how many bottles to prepare — for a day that has not happened yet?

That is what this chapter is about: turning collected data into something that can answer a question about the future.

Diagram

Fig. 18.0-1 – a school canteen counter on one side and an open notebook on the other; the notebook page shows two columns headed “Temperature” and “Bottles sold” with a few rows filled in, and a dotted arrow leads from the notebook to a question mark above the next day’s empty row

NoteCuriosity Corner

Q1. What does it actually mean to “build a model” from a set of numbers? Q2. How can a straight line drawn through past readings predict something that has not happened yet? Q3. When is it better to write the rules yourself than to let a computer find them? Q4. How can a computer sort things into groups when nobody has told it what the groups are? Q5. How do we find out whether a model is any good before we trust it?

Each of these five questions is answered somewhere in this chapter, at the point where the idea behind it has been explained.

18.1 Why a Computer Needs a Model

Think about the globe kept in your school library. It is not the Earth. It is much smaller, it is made of plastic, and it leaves out almost everything. Yet you can use it to work out which countries the equator passes through. A small, simplified stand-in has answered a question about the real thing.

Computers work the same way. When we want a computer to answer a question about the real world, we do not hand it the real world. We hand it a simplified stand-in built from numbers.

Data alone does not answer a question

By the end of Class 9 you had already met the idea of data — the raw facts and figures collected about something, such as temperatures, marks, or the number of items sold. Collecting data is where every project starts. But a table of collected data is only a record of things that have already happened.

Look back at the canteen notebook from the start of this chapter. It faithfully records what happened on every past day. It does not contain a row for tomorrow, because tomorrow has not happened. No amount of reading the old rows will produce that missing row.

So the notebook is not enough on its own. What is needed is something built from those rows that can take a fresh piece of information — tomorrow’s expected temperature — and turn it into an answer.

The stand-in we build from data

That something is called a model. A model is a simplified description of a pattern found in data, built so that it can be given a new input and produce an output.

Two ordinary words are worth pinning down here. An input is whatever you feed into the model — a temperature, a photograph, a set of marks. An output is what it gives back — a number, a label, or a group.

NoteDefinition

A model is a simplified representation of the pattern in a set of data, which takes an input about a new case and produces an output for it.

Q. If the model is built from old data, how can it say anything about a day that has not happened?

A. It cannot see the future. It assumes the pattern that held in the past will still hold. When that assumption breaks — a festival, a strike, a sudden change in weather — the output can be badly wrong.

What a model is not

A model does not know anything, and it does not understand the situation. It carries out a calculation that was fitted to past data, and nothing more. A model built only from summer readings has never seen a cold day, and it will still confidently produce a number if you ask it about one.

This is the honest limitation to keep in mind through the whole chapter. A model is only as trustworthy as the data it was built from, and it has no way of telling you when it has been pushed beyond what it has seen.

When a model’s output affects a person — who gets a loan, who is shortlisted for a place — a wrong output is not just a wrong number. Anyone building such a model has to check whose data it was built from, and say plainly what it does not cover.

18.2 What Data Modelling Means

A tailor does not begin by cutting cloth. He first takes measurements, decides which measurement matters for which part of the garment, and then makes a paper pattern. The paper pattern is not the shirt, but every shirt he stitches afterwards comes out of it.

Building a model works in much the same order. The step in which the pattern is chosen and fitted to the collected data has its own name.

Data modelling is the process of choosing a suitable method and fitting it to collected data, so that the result can be used to answer a question about a new case.

NoteDefinition

Data modelling is the process of organising data and fitting a method to it, so that the pattern in the data can be used to produce an output for a new input.

Notice that the word describes an activity, not a thing. The activity is data modelling. The thing it produces is the model.

18.2.1 From Raw Data to a Working Model

In earlier chapters you scoped a problem, acquired data, and explored it with graphs. Modelling is the step that follows. It takes the tidy, explored data and turns it into something that can produce answers.

The first job is to decide which columns of the data do which work. Suppose a farmer near Bharatpur has recorded, for each of his fields, the amount of rainfall that season, the amount of fertiliser used, and the weight of the crop harvested.

Two of those columns describe the conditions. One of them is the thing he actually wants to know in advance. A feature is an input column, one of the facts we already have about a case. The target is the output column, the thing the model is meant to produce. Here rainfall and fertiliser are the features, and the weight of the crop is the target.

Once features and target are fixed, modelling has three parts. See Fig. 18.2-1.

Diagram

Fig. 18.2-1 – a left-to-right flow with four labelled blocks: “Explored data” → “Choose features and target” → “Fit a method to the data” → “Model”; below the last block a small side arrow labelled “new input” enters it and an arrow labelled “output” leaves it

First, a method is chosen — a straight line, a set of rules, or a learning algorithm. Second, the method is fitted, which means its adjustable parts are set to the values that match the collected data most closely. Third, the fitted method is kept and used on new inputs.

Q1. What does it actually mean to “build a model” from a set of numbers?

A. It means picking a method that could describe the pattern in those numbers, then adjusting that method’s settings until it matches the recorded data as closely as it can. What you are left with is a rule for turning a new input into an output. Nothing is understood or memorised; only settings are adjusted.

Q. Can any column be chosen as the target?

A. Only a column you genuinely want to predict, and only if it is missing at the time you need the answer. Predicting the crop weight from the crop weight itself would be perfect and useless.

18.2.2 The Three Ways of Building a Model

There is no single way to build a model. The approach depends on what kind of answer is wanted and on how much reliable data is available.

Three approaches are used in artificial intelligence, and this chapter takes them one at a time. Fig. 18.2-2 shows them together.

Diagram

Fig. 18.2-2 – a single box labelled “Data modelling” at the top, with three arrows branching down to three boxes labelled “Predictive approach”, “Rule based approach” and “Learning based approach”; under each box a one-line caption reads “predicts a number”, “follows written rules” and “finds the pattern itself”

The predictive approach uses past data to work out a value that is not yet known, such as a quantity or a price. Its most common method is linear regression, which you meet in the next section.

The rule based approach does not search the data for a pattern at all. A person who knows the subject writes the rules, and the computer follows them exactly as written.

The learning based approach is the opposite. Nobody writes the rules. The method is shown a large amount of past data and adjusts itself until it matches that data, and the pattern it settles on is whatever the data suggested.

These three are not sealed off from one another. Linear regression, which sits under the predictive approach, is also a form of learning based modelling, because its slope and intercept are worked out from data rather than written down by a person.

18.3 Prediction – Linear Regression

Some questions ask for a label: is this fruit an apple or a pineapple? Other questions ask for a quantity: how many bottles, how many rupees, how many kilograms? The predictive approach is used when the answer wanted is a quantity.

The simplest and most widely used method for this is linear regression. It answers a quantity question by finding a straight line that runs through past data, and then reading the answer off that line.

The name is worth unpacking once. “Linear” means the pattern is described by a straight line. “Regression” is the old statistical word for working out how one quantity moves with another.

18.3.1 Spotting a Straight-Line Relationship

Before fitting any line, you have to be sure a line is a sensible shape for the data. Otherwise you are forcing a shape onto numbers that do not have it.

Here is the canteen’s record, tidied up into a table. Each row is one day. Table 18.1 shows the highest temperature recorded that day and the number of bottles sold.

Table 18.1 – daily highest temperature and bottles of buttermilk sold, for eight school days

Highest temperature (°C) Bottles sold
24 46
26 54
28 70
30 78
32 90
34 106
36 114
38 130

Reading the rows top to bottom, the second column climbs steadily as the first column climbs. That steady climb is the clue. Plotting the rows as points makes it obvious — see Fig. 18.3-1.

Diagram

Fig. 18.3-1 – a scatter plot with “Highest temperature (°C)” on the x-axis marked from 22 to 40 and “Bottles sold” on the y-axis marked from 40 to 140; the eight rows of Table 18.1 shown as dots, which lie close to but not exactly on a straight rising line

The dots do not sit on one perfect line. Real records never do, because plenty of other things affect sales. But they lie close enough to a rising line for a line to be a fair description of the pattern.

Q. What if the dots formed a curve, or a shapeless cloud?

A. Then linear regression is the wrong method. A curve needs a different method, and a shapeless cloud means these two columns are not related, so no line will help.

Here the feature is the temperature and the target is the number of bottles. The temperature is known the evening before, from the forecast. The bottle count is not, which is exactly why it is worth predicting.

18.3.2 Slope, Intercept and the Line of Best Fit

Any straight line can be written with just two numbers:

\[y = mx + b\]

Here \(x\) is the feature, \(y\) is the predicted value of the target, \(m\) is the slope and \(b\) is the intercept. The slope says how much \(y\) changes when \(x\) goes up by one. The intercept says where the line would cut the vertical axis, that is, the value of \(y\) when \(x\) is zero.

Change either number and you get a different line. So fitting a line to data means choosing the pair of numbers that places the line as close as possible to all the recorded points at once.

How “as close as possible” is measured

For any candidate line, take each recorded point and measure the gap between the actual value and the value the line gives. Those gaps are the errors. The line that makes the errors smallest overall is kept, and it is called the line of best fit.

NoteDefinition

Linear regression is a modelling method that fits a straight line to past data, so that the value of a target can be predicted from a feature.

fitting a line of best fit Input: a table of past records, each with one feature value and one target value Output: the slope and the intercept of the best-fitting line Steps:

1. Start with a candidate slope and intercept.
2. For every record, use the line to work out a predicted target value.
3. Subtract the predicted value from the actual value to get that
   record's error.
4. Combine all the errors into a single total, so that errors on both
   sides count as errors and do not cancel out.
5. Adjust the slope and intercept and repeat, keeping whichever pair
   gives the smaller total.
6. Stop when no further adjustment makes the total smaller.
7. Report the slope and intercept that were kept.

Why it works: the pair of numbers that leaves the least total error is the pair whose line passes closest to the recorded data, so it is the best straight-line description of that data.

Software does this arithmetic. For the eight rows of Table 18.1, the best-fitting line comes out as

\[y = 6x - 100\]

That is, a slope of 6 bottles per °C and an intercept of −100 bottles.

The slope is the useful half. It says that one degree of extra heat goes with about six more bottles sold. Table 18.2 checks the line against the record it came from.

Table 18.2 – actual sales compared with the value the fitted line gives, for the same eight days

Temperature (°C) Actual bottles Line gives \(6x-100\) Difference
24 46 44 +2
26 54 56 −2
28 70 68 +2
30 78 80 −2
32 90 92 −2
34 106 104 +2
36 114 116 −2
38 130 128 +2

The line is never exactly right, and it is never far wrong either. Being off by two bottles on a day when about a hundred are sold is a small enough error for the canteen to work with.

reading meaning into the intercept What goes wrong: students see the intercept −100 and conclude that at 0 °C the canteen sells minus a hundred bottles, which is nonsense. The fix: the intercept is only a number that positions the line. It carries a real meaning only if \(x = 0\) is a value the data actually covers. Here the recorded temperatures run from 24 °C to 38 °C, so the line says nothing at all about 0 °C.

18.3.3 Using the Line to Predict

Once the slope and intercept are fixed, prediction is just substitution. The model is asked a question by putting a new value of \(x\) into the equation.

predicting tomorrow’s bottles from the forecast

Given: fitted line \(y = 6x - 100\), where \(x\) is the highest temperature in °C and \(y\) is the number of bottles; tomorrow’s forecast highest temperature \(x = 39\) °C

Find: the number of bottles the model predicts

Working:

\(y = 6 \times 39 - 100\)

\(y = 234 - 100\)

\(y = 134\)

134 bottles

The canteen in-charge would not prepare exactly 134. She would round to a convenient number and keep a small margin, because Table 18.2 already showed the line runs about two bottles off either way.

Q2. How can a straight line drawn through past readings predict something that has not happened yet?

A. The line is not a picture of the past days. It is a rule — multiply the temperature by 6, then subtract 100 — that was chosen because it came closest to every past day at once. A rule with numbers in it can be applied to any temperature, including one that has not occurred yet. The prediction holds only as long as the relationship between heat and sales stays the same as it was while the data was collected.

Where the line stops being trustworthy

Substitution will happily give you an answer for any number you put in. Ask the model about 55 °C and it returns 230 bottles without hesitating.

But no day in Table 18.1 was anywhere near 55 °C. The model has no evidence that the same steady climb continues that far, and in reality it would not — the school itself would be shut. Using a fitted line outside the range of the data it was built from is the commonest way to get a confident, useless number.

Q. The line fits these eight days well. Does that make it a good model?

A. Not yet. Fitting the days it was built from is the least it must do. Whether it works on days it has never seen is a separate test, and Section 18.6 is about exactly that.

Using the line \(y = 6x - 100\), work out the predicted sales at 25 °C and at 35 °C. Then find the difference between your two answers, and check whether the slope alone would have told you the same thing.

18.4 Rule Based Approach

Not every question needs data to answer it. Think of how your school turns marks into a grade. Anyone above ninety gets the top grade, anyone between eighty and ninety gets the next one, and so on down the list. No pattern has to be discovered here, because the pattern was decided by people and written down before any student sat the exam.

Whenever the correct answer is already known and can be stated in words, there is no sense in making a computer search for it. You simply write the rules and let the computer follow them.

NoteDefinition

The rule based approach builds a model out of conditions written by people, which the computer applies exactly as written, instead of finding a pattern in data.

18.4.1 Writing the Rules

Every rule in such a system has the same shape: a condition, and an action to take when that condition holds. This is usually called an if-then rule, and you have already written many of them in Python without calling them that.

Consider a stationery shop near a school that gives a discount on larger bills. The owner decides the policy herself. Bills of ₹1000 or more get ten per cent off, bills of ₹500 or more get five per cent off, and smaller bills get nothing.

applying the shop’s discount rules Input: the bill amount in rupees, before discount Output: the discount percentage to be applied Steps:

1. Read the bill amount.
2. If the bill amount is 1000 or more, the discount is 10 per cent.
3. Otherwise, if the bill amount is 500 or more, the discount is
   5 per cent.
4. Otherwise, the discount is 0 per cent.
5. Report the discount.

Why it works: the conditions are checked from the largest amount downwards, so every bill matches exactly one of them and no bill is left without an answer.

The order of checking matters more than it looks. A bill of ₹1200 satisfies both the first condition and the second one. Because the larger condition is tested first, it gets the ten per cent it should.

what a customer pays on a ₹1200 bill

Given: bill amount = ₹1200; the discount rules above

Find: the amount payable after discount

Working:

The bill is 1000 or more, so the discount is 10 per cent.

Discount = 10% of ₹1200 = \(\dfrac{10}{100} \times 1200\) = ₹120

Amount payable = ₹1200 − ₹120

₹1080

Who writes the rules

The rules come from someone who knows the subject well — the shop owner here, a bank officer in a bank, an engineer in a factory. That knowledge is then written into conditions the computer can test. The computer supplies no judgement of its own; it supplies speed and consistency.

Because of this, a rule based system is deterministic, which means the same input always produces the same output, every single time. Run the ₹1200 bill through it a thousand times and you get ₹1080 a thousand times.

Q. Why is always giving the same answer worth calling an advantage?

A. Because the system can be checked. If a customer disputes a bill, you can point to the exact rule that fired and show that it was applied correctly. There is nothing hidden to argue about.

leaving a gap between the rules What goes wrong: rules are written for bills above ₹1000 and for bills below ₹500, and nothing is said about a bill of exactly ₹700. That bill matches no rule, and the program either stops or returns nothing. The fix: make sure the conditions cover every possible input, with a final “otherwise” branch that catches whatever the earlier conditions missed.

What rules cannot do

A rule based system only knows what somebody thought to write down. It handles a situation nobody anticipated by doing nothing sensible at all.

There is a second limit. Rules are easy to manage while there are ten of them. A system with several thousand rules, written by different people over several years, becomes very hard to change safely, because no one can be sure which other rule a small edit will disturb.

18.4.2 Where Rule Based Systems Are Used

Rule based systems suit any setting where the policy is fixed, the reasoning must be open to inspection, and there is little or no past data to learn from.

Fraud detection. Banks flag transactions that break conditions known to be suspicious — for example, a card used for several large withdrawals in different cities within a few minutes. Nothing about that has to be learned. It is written as a rule and the transaction is held for checking.

a rule based fraud alert What it is: a set of written conditions that a bank’s software tests against every transaction as it happens. How it works: each transaction is checked against every condition in turn. If any condition holds, the transaction is flagged and sent for a human to review; if none holds, it goes through. Used for: holding back a card payment made in a second city minutes after one in the first. Watch out: it can only catch the tricks somebody has already thought of and written down. A new method of fraud passes through untouched until a new rule is added.

Business process automation. Routine office decisions that follow a fixed policy can be handed to a rule based system — checking whether an application carries every required document, or whether a claim falls within the limits of a policy.

Recommendation. A simple online store can suggest related items with plain rules: someone buying a laptop is shown laptop bags, someone buying a printer is shown ink cartridges. The suggestions are sensible without any pattern having been learned from customers.

A rule that flags a person — as a suspect transaction, as an incomplete application — will sometimes be wrong about someone who did nothing unusual. A flagged case should therefore go to a person for review, not to an automatic rejection, and the individual should be able to ask which rule was applied.

Q. The shop wants to know how many customers will come next Saturday. Can it write a rule for that?

A. No. A rule can only apply a policy the owner has decided. Nobody decides how many customers arrive, so that question needs data and a fitted model, not a written rule.

18.5 Learning Based Approach

Try to write down, in words, the rule you use to tell a ripe mango from an unripe one. You will get as far as colour and firmness, and then run out. Some ripe mangoes stay green. Some unripe ones look yellow in the right light. You can do it in half a second, and you cannot explain how.

This is where the rule based approach runs out of road. The rule cannot be written because nobody can state it. But there is another way in: you can easily collect a thousand mangoes and say which ones are ripe.

That is the shift the learning based approach makes. Instead of supplying the rule, you supply the examples, and the pattern is worked out from them.

18.5.1 How a Model Learns from Data

The word “learning” is doing a lot of quiet work here, so it is worth saying exactly what happens. Nothing is understood. Numbers are adjusted.

The process is called training, and it repeats one small loop many thousands of times. The model is given a past example whose correct answer is known. It produces its own answer. The two are compared, and the gap between them is the error. The model’s internal numbers are then nudged in the direction that would have made that error smaller.

One pass over one example changes very little. Millions of passes over thousands of examples settle the numbers into values that work across the whole collection.

training a learning based model Input: a large set of past examples, each with its features and its correct answer Output: a trained model Steps:

1. Set the model's internal numbers to starting values.
2. Take one example from the set.
3. Feed its features to the model and read the model's answer.
4. Compare that answer with the correct answer and measure the error.
5. Adjust the internal numbers slightly, in the direction that reduces
   this error.
6. Repeat from step 2 with the next example.
7. Stop when the error over the whole set is no longer falling.

Why it works: each adjustment is small, so no single example can pull the model far. What survives many passes is whatever suits most of the examples at once, which is the pattern shared by all of them.

You have already seen this in Section 18.3. Fitting a slope and an intercept by making the total error as small as possible is the same idea, with only two numbers to adjust instead of thousands.

The point of all this is not to get the training examples right. It is to generalise — to give sensible answers on new cases the model has never been shown. A model that gets the training examples right and new cases wrong has learned nothing useful.

Q. Then why not just store all the training examples and look up the answer?

A. Because the next mango will not exactly match any stored one. Looking things up only works for cases you have already seen, which are precisely the cases you do not need help with.

The honest limitation of the whole approach follows from how it works. A learning based model needs a large amount of good past data, and it cannot tell you which rule it settled on. You can see what it outputs, not why.

There are three main kinds of learning based approach, and they differ in what the past data contains.

18.5.2 Supervised Learning

Go back to the mangoes. Suppose a fruit trader photographs a thousand mangoes and, for each photograph, writes down “ripe” or “unripe” after checking it himself.

That written answer attached to each example is called a label, and a collection of examples that carries labels is a labelled dataset. Training a model on such a dataset is called supervised learning. The name comes from the labels acting like a teacher: for every example, the correct answer is available for comparison.

NoteDefinition

Supervised learning is a learning based approach in which the model is trained on a labelled dataset, where every example carries the correct output the model is meant to produce.

Training works exactly as described in 18.5.1, with the label supplying the correct answer at step 4. Fig. 18.5-1 shows the two stages.

Diagram

Fig. 18.5-1 – two stacked panels. Upper panel labelled “Training”: a stack of mango photographs, each tagged “ripe” or “unripe”, feeding into a box marked “model”, with a return arrow labelled “adjust to reduce error”. Lower panel labelled “Use”: a single new untagged photograph entering the same box, now marked “trained model”, with one arrow out carrying the output “ripe”

When the label is one of a fixed set of categories, the task is called classification, and the model’s job is to put each new case into the right category. Ripe or unripe, spam or not spam, and the digit written in a box on a form are all classification tasks.

When the label is a quantity instead of a category, the task is prediction of a number — which is what linear regression does. This is why linear regression counts as supervised learning too: every day in Table 18.1 came with its correct bottle count attached.

A supervised model can only reproduce what its labels contain. If every mango in the training photographs came from one orchard, the model will do poorly on fruit from anywhere else, and it will fail without warning. Whoever assembles the dataset has to check that it covers the range of cases the model will actually meet.

Q. Where do the labels come from?

A. Usually from people, one example at a time. This is slow and expensive, and it is the main reason supervised learning is not used everywhere.

18.5.3 Unsupervised Learning

Labels are not always available. A shop’s billing counter records what every customer bought, but no one has ever written down what kind of customer each one is. There are no categories, because nobody has decided what the categories should be.

Unsupervised learning is used when the data carries no labels at all. The model is given the input data on its own and asked to find whatever structure is already in it.

The commonest form is clustering, in which the model groups examples that resemble one another and separates those that do not. Fig. 18.5-2 shows the idea.

Diagram

Fig. 18.5-2 – on the left, a scattered mix of unlabelled shapes; an arrow through a box marked “clustering” leads to the right, where the same shapes now sit inside three circles, each circle holding shapes that are similar to one another

Run this on the shop’s billing records and three groups might emerge — customers who buy only stationery, customers who buy mostly snacks, and customers who buy a little of everything. The model does not name these groups. It reports that three distinct patterns of buying exist and which customer belongs to which. Naming them, and deciding what to do about them, is the shopkeeper’s job.

Q4. How can a computer sort things into groups when nobody has told it what the groups are?

A. It measures how similar the examples are to each other, using the columns of data it has, and puts examples that are close together into the same group. It never learns what the groups mean, because that information was never in the data. It only reports that these examples resemble one another more than they resemble the rest.

Table 18.3 – supervised learning vs unsupervised learning

Feature Supervised learning Unsupervised learning
Data needed Examples with labels Examples without labels
Correct answer during training Available for every example Not available at all
What the model produces A label or a quantity for a new case Groups or structure found in the data
Typical task Classification, prediction of a number Clustering
How the result is checked Compared against known correct answers Judged by a person for usefulness

Q. If there is no correct answer, how does anyone know the grouping is any good?

A. Nobody can check it against a right answer, because there isn’t one. The groups are judged by whether they turn out to be useful — whether they hold together on new data and tell the shopkeeper something she can act on.

18.5.4 Reinforcement Learning

There is a third situation, different from both. Think of learning to ride a cycle. Nobody labelled each movement as correct or wrong. You tried things, and staying upright was its own signal.

Reinforcement learning works this way. The learner, called the agent, acts inside an environment, and after each action it receives a reward — a number saying how well that turned out. Over many attempts it settles on the actions that earn the most reward in the long run.

reinforcement learning What it is: a learning based approach in which an agent improves by acting and receiving rewards, rather than by studying labelled examples. How it works: the agent observes the state of the environment, chooses an action, and receives a reward and a new state in return. Actions followed by good rewards become more likely; actions followed by poor rewards become less likely. Used for: training a small robot to cross a room without hitting the furniture, where each safe step earns a small reward and a collision costs one. Watch out: it needs an enormous number of attempts, and most of the early attempts fail. That is affordable in a simulation and expensive with real hardware.

The loop is shown in Fig. 18.5-3.

Diagram

Fig. 18.5-3 – a closed loop between two boxes labelled “Agent” and “Environment”; an arrow from agent to environment labelled “action”, and two arrows back from environment to agent labelled “state” and “reward”

Reinforcement learning suits problems where the answer is a sequence of decisions rather than a single output, and where the effect of a decision only shows up much later.

18.5.5 Rule Based and Learning Based Compared

Neither approach is better in general. Each fits a different kind of problem, and the choice is made by looking at what you have and what you need.

Table 18.4 – rule based approach vs learning based approach

Feature Rule based approach Learning based approach
Where the pattern comes from Written by a person who knows the subject Worked out from past data during training
Data needed Little or none A large set of past examples
Handling a new kind of case Fails until someone adds a rule Often manages, if the case resembles the training data
Can the reasoning be inspected Yes, the exact rule can be shown Usually not; only the output is visible
Same input, same output Always Yes once trained, but the trained model may change if it is retrained
Suits Fixed policies, small clear problems Patterns too complicated to write down

Q3. When is it better to write the rules yourself than to let a computer find them?

A. When the correct answer is a policy somebody has decided rather than a pattern hiding in data, when there is little past data to learn from, and when you must be able to show exactly why a particular decision was made. A discount slab or a grade boundary should be a written rule. Telling a ripe mango from an unripe one should not, because nobody can write that rule down.

Real systems often use both. A bank may train a model to score how unusual a transaction looks, and then apply a written rule that holds any transaction above a fixed score. The model supplies the judgement that is hard to write down; the rule supplies the part that must be open to inspection.

18.6 Evaluating the Model

A model that has just finished training is not yet a model anybody should use. It is a candidate. Before it is allowed to decide anything, somebody has to find out how often it is right, and how wrong it is when it is wrong.

That checking step is called evaluation, and it is the last part of the AI project framework.

NoteDefinition

Evaluation is the process of measuring how well a trained model performs on data it was not trained on, in order to decide whether it can be used.

18.6.1 Training Data and Testing Data

Suppose a teacher gives the class twenty questions to practise, and then sets exactly those twenty questions in the test. Everyone scores well. Nobody learns anything about who understood the subject.

Testing a model on the same data it was trained on has the same flaw. The model was adjusted, example by example, until it matched that data. Of course it matches it. That score tells you nothing about a case it has never met.

So the collected data is split into two parts before training begins. The training data is the portion used to fit the model. The testing data is a portion set aside, never shown during training, and used only at the end to measure performance. Fig. 18.6-1 shows the split.

Diagram

Fig. 18.6-1 – a single long bar labelled “All collected data” divided into a large left part labelled “Training data” and a smaller right part labelled “Testing data”; an arrow from the left part leads to a box marked “training”, and an arrow from the right part leads to a separate box marked “evaluation”, with a dashed line and a cross showing that testing data never enters the training box

Most of the data goes into training, because more examples make for a better fit. A useful working split is around four-fifths for training and one-fifth for testing, though the exact proportion varies with the size of the dataset.

peeking at the testing data What goes wrong: the model is trained, tested, adjusted because the test score was poor, tested again, adjusted again. After a few rounds the testing data has quietly shaped the model, and its score is no longer an honest measure. The fix: set the testing data aside and use it once, at the end. If you need to compare several versions while building, split off a third portion for that and leave the test set untouched.

18.6.2 Measuring How Well a Model Works

What counts as “well” depends on what the model produces. A model that outputs a category is measured differently from one that outputs a quantity.

Measuring a model that outputs a category

For classification the natural measure is accuracy — the share of test cases the model got right, usually written as a percentage.

\[\text{accuracy} = \frac{\text{number of correct predictions}}{\text{total number of predictions}} \times 100\]

Table 18.5 shows the trader’s ripeness model run on ten photographs it had never seen during training.

Table 18.5 – actual and predicted ripeness for ten test photographs

Photograph Actual Model’s output Correct?
1 ripe ripe yes
2 unripe unripe yes
3 ripe unripe no
4 ripe ripe yes
5 unripe unripe yes
6 unripe ripe no
7 ripe ripe yes
8 unripe unripe yes
9 ripe ripe yes
10 unripe unripe yes

accuracy of the ripeness model

Given: 10 test photographs; the model’s output matches the actual ripeness on 8 of them

Find: the accuracy of the model on this test set

Working:

\(\text{accuracy} = \dfrac{8}{10} \times 100\)

\(\text{accuracy} = 0.8 \times 100\)

80 per cent

Measuring a model that outputs a quantity

Accuracy makes no sense for a quantity, because a prediction of 134 bottles against an actual 133 is not simply “wrong”. What matters is the size of the gap.

So the errors themselves are measured. Take the gap between predicted and actual for each test case, ignore whether it is above or below, and average them. For the eight days checked in Table 18.2, every gap was 2 bottles in size, so the average gap is 2 bottles. Whether that is good enough is a decision for the canteen, not for the mathematics.

Q5. How do we find out whether a model is any good before we trust it?

A. By keeping back part of the collected data, never letting the model see it during training, and then measuring performance on that untouched part. For a model that outputs a category, count the share it gets right. For one that outputs a quantity, measure the average size of its errors. A score obtained on data the model has already been trained on proves nothing.

Why one number is never the whole story

A high accuracy figure can hide a useless model. Imagine a factory where 95 out of every 100 parts are fine and 5 are faulty. A model that simply answers “fine” every single time, without looking at anything, is right 95 times out of 100. Its accuracy is 95 per cent, and it catches not one faulty part.

So accuracy has to be read alongside what the test set contained. When one category is far commoner than the other, a good score on the common category can drown out complete failure on the rare one — and the rare one is usually the case you built the model to catch.

An overall score can look healthy while the model fails badly for one group within the test data. Before a model is used on people, its performance should be checked separately for each group it will be applied to, not just once for everyone together.

Take the ten rows of Table 18.5 and cover the last column. Work out what the accuracy would have been if the model had answered “ripe” for every photograph. Compare that with the 80 per cent it actually scored.

18.6.3 Overfitting and Underfitting

When a model does poorly, there are two opposite ways it can have gone wrong, and the cures are opposite too.

Overfitting happens when a model matches its training data very closely but performs poorly on new data. It has fitted the accidental details of those particular examples — the noise — instead of the pattern underneath. The tell-tale sign is a very high score on training data and a much lower one on testing data.

Underfitting is the reverse. The model is too simple to capture the pattern at all, so it performs poorly on both the training data and the testing data. Fitting a straight line to data that clearly curves would underfit it.

Fig. 18.6-2 shows all three cases on the same set of points.

Diagram

Fig. 18.6-2 – three small scatter plots side by side, each showing the same set of dots. In the first, labelled “Underfitting”, a flat straight line misses most of the dots. In the second, labelled “Good fit”, a gently curving line passes near all of them. In the third, labelled “Overfitting”, a wildly wiggly line passes exactly through every dot

Table 18.6 – overfitting vs underfitting

Feature Overfitting Underfitting
Score on training data Very high Low
Score on testing data Low Low
Cause Model too complicated, or too little training data Model too simple for the pattern
What it has learned The noise as well as the pattern Not even the pattern
Usual remedy Simplify the model, or collect more training data Use a method that can describe a richer pattern

Q. How would you spot overfitting without a test set?

A. You could not. Overfitting is defined by the difference between the training score and the testing score, so without data held back there is nothing to compare against. This is the strongest single reason for the split in 18.6.1.

Once the model has been evaluated and the scores are acceptable, it can be put to use. If they are not, the work goes back a step — to a different method, a better set of features, or more data — and the whole cycle runs again.

18.7 Summary

  1. A record of past data only describes what has already happened. To answer a question about a new case, a model is needed — a simplified description of the pattern in the data that turns an input into an output.
  2. Data modelling is the activity of organising data and fitting a method to it. The model is what that activity produces.
  3. Within the data, a feature is an input column and the target is the output column the model is meant to produce.
  4. Three approaches are used: the predictive approach, the rule based approach and the learning based approach.
  5. Linear regression is the commonest predictive method. It fits a straight line, written as \(y = mx + b\), where the slope \(m\) says how much the target changes for a one-unit change in the feature, and the intercept \(b\) positions the line.
  6. The line of best fit is the one that leaves the smallest total error across all the recorded points. A prediction is made by substituting a new feature value into the equation.
  7. A fitted line should not be used outside the range of the data it was built from, and the intercept has a real meaning only if a feature value of zero is covered by that data.
  8. In the rule based approach, a person who knows the subject writes if-then rules and the computer applies them exactly as written. Such systems are deterministic, easy to inspect, and need little data.
  9. Rule based systems cannot handle a situation nobody wrote a rule for, and become hard to manage once the rules run into thousands. They are used for fraud alerts, routine office decisions and simple recommendations.
  10. In the learning based approach nobody writes the rule. Training repeats a loop of predicting, measuring the error and adjusting the model’s internal numbers, until the error over the whole set stops falling.
  11. The aim of training is to generalise — to work on new cases, not merely to match the training examples.
  12. Supervised learning trains on a labelled dataset, where every example carries its correct output. Classification is the supervised task of placing a case into one of a fixed set of categories.
  13. Unsupervised learning works on data with no labels and looks for structure already present, most often by clustering similar examples together. It does not name the groups it finds.
  14. Reinforcement learning has an agent act inside an environment and receive a reward after each action, so that actions leading to better rewards become more likely.
  15. Data is split before training into training data, used to fit the model, and testing data, kept back and used once at the end to measure performance.
  16. Accuracy — the share of test cases the model gets right — measures a model that outputs a category. A model that outputs a quantity is measured by the average size of its errors.
  17. A single score can mislead. When one category is far commoner than the other, a high accuracy can hide complete failure on the rare category.
  18. Overfitting means the model matches its training data closely but does poorly on new data. Underfitting means the model is too simple and does poorly on both. The gap between the training score and the testing score is what tells them apart.

18.8 Key Terms

  1. Accuracy – the share of test cases a model gets right, usually written as a percentage.

  2. Agent – in reinforcement learning, the learner that acts inside an environment and receives rewards.

  3. Classification – a supervised task in which the model places each case into one of a fixed set of categories.

  4. Clustering – grouping examples that resemble one another, without being told what the groups are.

  5. Data – the raw facts and figures collected about something.

  6. Data modelling – the process of organising data and fitting a method to it, so the pattern can produce an output for a new input.

  7. Deterministic – always producing the same output for the same input.

  8. Feature – an input column of the data, a fact already known about a case.

  9. Generalise – to give sensible answers on new cases the model was never trained on.

  10. Input – whatever is fed into a model.

  11. Intercept – the number \(b\) in a straight line, giving the value of \(y\) where the line crosses the vertical axis.

  12. Label – the correct output written against a training example.

  13. Labelled dataset – a collection of examples in which every example carries its correct output.

  14. Learning based approach – building a model by letting a method work the pattern out from past data instead of being told it.

  15. Line of best fit – the straight line that leaves the smallest total error across all the recorded points.

  16. Linear regression – a modelling method that fits a straight line to past data so a target can be predicted from a feature.

  17. Model – a simplified representation of the pattern in a set of data, which turns a new input into an output.

  18. Output – what a model gives back for a given input.

  19. Overfitting – matching the training data closely while performing poorly on new data.

  20. Predictive approach – using past data to work out a quantity that is not yet known.

  21. Reinforcement learning – a learning based approach in which an agent improves by acting and receiving rewards.

  22. Reward – the number an agent receives after an action, saying how well that action turned out.

  23. Rule based approach – building a model from conditions written by people, which the computer applies exactly as written.

  24. Slope – the number \(m\) in a straight line, giving the change in \(y\) for a one-unit rise in \(x\).

  25. Supervised learning – training a model on a labelled dataset, where the correct output is available for every example.

  26. Target – the output column of the data, the thing the model is meant to produce.

  27. Testing data – the portion of the data kept back from training and used at the end to measure performance.

  28. Training – the repeated process of predicting, measuring the error and adjusting a model’s internal numbers.

  29. Training data – the portion of the data used to fit the model.

  30. Underfitting – being too simple to capture the pattern, so performing poorly on both training and testing data.

  31. Unsupervised learning – finding structure in data that carries no labels.

18.9 Objective Questions

  1. Fill in the blanks: An input column of a dataset is called a ____________, while the column the model is meant to produce is called the ____________.

  2. Choose the correct option. In the equation \(y = mx + b\), the number that tells you how much \(y\) changes when \(x\) rises by one is the

    1. intercept
    2. slope
    3. target
    4. label
  3. State whether true or false: Testing a model on the same data it was trained on gives an honest measure of how it will perform on new cases.

  4. Choose the correct option. Which of these can be built without any past data at all?

    1. supervised learning
    2. unsupervised learning
    3. a rule based system
    4. linear regression
  5. Fill in the blank: A model that matches its training data closely but performs poorly on new data is said to be ____________.

  6. Match each term in Column A with its correct description in Column B.

Column A Column B
a. Clustering i. placing a case into one of a fixed set of categories
b. Classification ii. the signal an agent receives after an action
c. Reward iii. the portion of data kept back until the end
d. Testing data iv. grouping similar examples that carry no labels
  1. A model has been fitted as \(y = 4x + 12\). State the value it predicts when \(x = 7\).

18.10 Concept Questions

  1. Define a model. Explain why a table of past records, on its own, cannot answer a question about a new case.

  2. Give one reason why a rule based system may be preferred for a task even when a large amount of past data is available.

  3. Differentiate between supervised learning and unsupervised learning, giving one example of a task suited to each.

  4. A canteen’s sales model has been fitted as \(y = 5x - 60\), where \(x\) is the day’s highest temperature in °C and \(y\) is the number of bottles sold. Work out the number of bottles the model predicts for a day with a highest temperature of 32 °C, and state in one sentence what the slope tells the canteen.

  5. State the difference between overfitting and underfitting. Explain how holding back a portion of the data makes it possible to tell one from the other.

  6. In a test set of 200 items, 190 belong to one category and 10 to the other. A model that answers with the commoner category every time, without examining the item at all, is right 190 times. Work out its accuracy and explain why that figure does not show the model to be useful.

18.11 Case-Based Questions

  1. A teacher at a coaching centre in Kota records, for five students, the number of hours each spent practising before a class test and the marks each scored out of 80.
Hours of practice Marks scored
1 28
2 36
3 44
4 52
5 60

Answer the following. a. Looking at the two columns, state whether a straight line is a fair description of this data, and give the reason for your answer. b. Write the equation of the line that fits this data, in the form \(y = mx + b\), and state what the slope means here. c. Use your equation to predict the marks for a student who practises for 6 hours. Then give one reason why the real result may not match that prediction.

  1. A small workshop wants to catch faulty parts before they are packed. A supervisor photographs 500 parts and marks each photograph as “fine” or “faulty”. Of the 500, 470 are fine and 30 are faulty.

Answer the following. a. Name the learning based approach that suits this data, and give the reason for your choice. b. Explain how the 500 photographs should be divided before training begins, and why they should not all be used for training. c. The trained model reports an accuracy of 94 per cent on the test set. Explain why the workshop should not accept this figure as proof that the model is working.