Three Agents, One Prediction. Is Teamwork Enough?

Understanding the Multilayer Perceptron (MLP)

At the end of Article 3, Fernanda had built something rather useful: A little numerical decision-maker called a Threshold Logic Unit (TLU). It received Bedrooms and Location Code, combined them using weights and a bias, and returned YES or NO.

All very nice. But now Fernanda has a new problem. Some sellers do not want another YES or NO. Instead, they want an estimated price, preferably one that will not cause them to faint!

But hang on. We already tackled price prediction in Article 2. Fernanda combined Bedrooms and Location using a linear equation. Why complicate everybody’s afternoon by inviting Omar and Ricardo to the party?

Good point! However, consider a limitation of that earlier approach. In particular, recall that it adds a fixed amount for each bedroom, whether the house is in a Standard or a Prime location. Suppose, however, the historical pattern were different. Perhaps one extra bedroom is associated with an additional $30,000 in Standard locations but $70,000 in Prime locations. Then the effect of Bedrooms on the prediction depends on Location too. So, in this case, one fixed Bedroom Weight cannot represent both increases. Oh boy, what do we do now?

We could, of course, modify the original equation to include an additional calculation representing the interaction between Bedrooms and Location. That would be a perfectly reasonable approach. But what if we wanted a model that could learn useful combinations of its inputs without requiring us to specify every such interaction in advance?

This is where a different arrangement of calculations becomes interesting: Instead of passing Bedrooms and Location directly into one final price calculation, we could first let Omar and Ricardo process those inputs separately, using different numerical settings. Each would produce an intermediate response. Fernanda would then combine their responses to estimate the price.

The interesting possibility is that these intermediate responses, particularly when produced using nonlinear transformations, can allow the overall model to represent more complex relationships that our original linear calculation could not.

Sounds neat! But does that guarantee a better prediction? Not at all! However, the new approach does give us a more flexible way to reach a reasonable prediction.

And that, rather than simply providing employment opportunities for Omar and Ricardo, is why we are bringing them into the picture😊.

Let’s set the expectation here: We want to see how the team (Fernanda, Omar and Ricardo) produces a prediction. We note that a more flexible model is not necessarily a more accurate one. Whether the team can learn better settings from its mistakes is the subject of Article 5 so, hold your horses until then and let’s just concentrate on this article first!

For the remainder of this article the main question is:

We are keeping Properties A to F from Articles 1, 2 and 3 (see Table 1 below). There is no need to invent six new houses simply to give our three agents something to do. The location codes remain Standard = 0 and Prime = 1.

PropertyBedroomsLocationLocation CodeActual Sale Price
A2Standard0$200,000
B3Standard0$250,000
C4Standard0$280,000
D2Prime1$300,000
E3Prime1$400,000
F4Prime1$450,000
Table 1: The Same Six Historical Sales Used Throughout the Series

The inputs are still Bedrooms and Location Code. The output we now want is a predicted sale price. Actual sale prices are known only because these six houses have already sold. Remember that we do not feed their actual prices into the prediction.

NOTE: For easier arithmetic, this article measures money in units of $100,000. Thus $300,000 becomes 3. Article 2 used $10,000 units. We are changing the measuring unit, not the houses or their prices. We will convert back to dollars whenever the result needs to look like a house price.

Imagine Omar and Ricardo sitting at two desks. Both receive exactly the same two facts about a property: Its number of bedrooms and whether the location is Standard or Prime.

Each makes a separate calculation. Each sends one numerical response to Fernanda. Fernanda receives those two responses and combines them into the final price estimate. In our deliberately specified network, she does not receive the original bedroom count or location code directly.

Figure 1: Same Property, Multiple Viewpoints, One Final Prediction

Notice that information moves in one direction while the prediction is made:

We will shortly give that movement its technical name. Just wait a few seconds, OK?

Each junior (that is how we refer to Omar and Ricardo individually with their boss, Fernanda, being the senior) has two weights and one bias, just as the TLU in Article 3 had weights and a bias. But here is the beauty of this extra layer of estate agents (who said being a junior is not a big deal!): The two juniors need not use the same settings! In fact, for our first attempt, let’s choose the values as in Table 2 below:

AgentBedroom WeightLocation WeightBias
Omar21−4
Ricardo13−3
Table 2: Our Initial, Deliberately Simple Settings

So, Omar calculates his score as Score = (2 × Bedrooms) + (1 × Location Code) − 4, while Ricardo calculates his score as Score = (1 × Bedrooms) + (3 × Location Code) − 3.

Now let’s compare the 2 calculations:

Omar’s calculation is more responsive to one additional bedroom. Ricardo’s is more responsive to the change from Standard to Prime. It is important to note that what we just saw describes their numerical settings, not a discovery that a bedroom is more valuable than a location. In other words, these settings are initial values of weights and do not represent an underlying fact about the relative importance of Bedrooms and Location.

Their biases provide a fixed shift to each score. Because each junior has his own bias, the same property can produce different scores even before we decide what to pass on. This, my friends, is a sign of additional flexibility that the new approach offers us.

Article 3 gave us a Step Function such that: Below the cutoff it returned 0, otherwise 1. This was useful for a YES/NO decision. Our juniors, however, need to send numerical information that can vary by more than one bit. That is to say, we need Omar and Ricardo to communicate more than a simple Yes/No (1 or 0). But how?

Suppose each junior follows this simple rule: If the score is negative, send 0, otherwise, send the score itself (see Table 3 below for some examples):

Junior’s ScoreOutgoing Response
−20
−0.50
00
11
22
3.53.5
Table 3: The Response Rule for Each Junior

Now that we know what the rule does, we can name it: The Rectified Linear Unit (ReLU), a common Activation Function. We can write the ReLU function more formally as:

An Activation Function transforms a unit’s score into the value sent onward. The Step Function from Article 3 was another Activation Function, but its output was restricted to 0 and 1. You see how easy it is to learn seemingly complex terminology? You just need to wait a bit and carefully listen to me!

Anyway, ReLU is not a miniature estate agent exercising judgment. It is just the numerical rule described above. A negative score becomes zero, zero becomes zero, and a positive score passes through unchanged. Coooooool!

Fernanda, being a senior agent and quite an independent individual, has her own adjustable settings. To start, she gives each colleague’s message a weight of 0.5 and adds a bias of 1. So, her calculation (similar to what we saw in Article 2) is:

Note that her output is a price number, so we use a linear output. This means we do not need a Step Function or a ReLU after her final sum. She simply returns the numerical result, which we multiply by $100,000 to get to the actual price prediction.

The bias of 1 corresponds to a starting contribution of $100,000 (remember that bias always means a shift in this situation). The two weights control how much each message (from Omar and Ricardo respectively) affects the prediction. Their initial sum (0.5 for Omar and o.5 for Ricardo) happens to be 1 in our example. However, the network does not require weights to sum to 1. Nor do they have to remain positive when learning begins.

Now, let’s choose Property D, our familiar two-bedroom Prime property. Its input values are Bedrooms = 2 and Location Code = 1. Its actual historical sale price is $300,000, or 3 units. Here we have:

Let’s be precise then: $250,000 is a prediction; $300,000 is what Property D actually sold for. The prediction is $50,000 too low. The network has performed every calculation correctly so, why we are short of $50,000 in our prediction? Well, because the network’s chosen settings are simply not yet good enough. In other words, it still has some “learning” to do!

As before, the same weights and biases are applied to every property. Of course, the inputs change from house to house. But the parameters do not change during this first round of predictions.

HouseOmar:
Score → Response
Ricardo:
Score → Response
Fernanda:
Predicted Units
Actual Units
A0 → 0−1 → 01.02.0
B2 → 20 → 02.02.5
C4 → 41 → 13.52.8
D1 → 12 → 22.53.0
E3 → 33 → 34.04.0
F5 → 54 → 45.54.5
Table 4: The Complete First Calculation for All Six Historical Properties

For example, House C has four bedrooms and a Standard location. So, we have:

House E, meanwhile, is predicted exactly. Wonderful! Before we open the champagne, however, notice the other five houses. One lucky answer does not rescue an entire model.

Anyway, time for some useful terminology: The process of using current settings to move from inputs through the network to a prediction is called the forward pass and we have now completed it for all six properties.

This is a super important question so, well done for asking it!

The answer requires a bit of an effort on the reader’s side but I promise you that the answer is more interesting than the name “Rectified Linear Unit” might suggest! So, let me do this:

Let’s return to Omar, Ricardo and Fernanda, and use the numerical settings we introduced earlier in this article.

Remember that Omar and Ricardo each receive the same two inputs, Bedrooms and Location Code, but use different weights and biases to calculate their scores.

Their original calculations are as follows.

Omar:

  • Bedroom Weight = 2
  • Location Weight = 1
  • Bias = −4

Therefore,

Omar’s Score = (2 × Bedrooms) + (1 × Location Code) − 4

Ricardo:

  • Bedroom Weight = 1
  • Location Weight = 3
  • Bias = −3

Therefore,

Ricardo’s Score = (1 × Bedrooms) + (3 × Location Code) − 3

Fernanda then receives their responses and calculates the predicted price using her own settings.

Fernanda:

  • Weight on Omar = 0.5
  • Weight on Ricardo = 0.5
  • Bias = 1

Her calculation is:

Predicted Price Units = (0.5 × Omar’s Response) + (0.5 × Ricardo’s Response) + 1

And remember that one price unit represents $100,000.

So far, nothing new. These are exactly the settings we have already used.

Now, let’s ask an interesting question:

In other words, suppose they simply passed their calculated scores directly to Fernanda, including negative scores, without converting them to zero.

In that case, Omar’s Response would simply equal Omar’s Score, and Ricardo’s Response would equal Ricardo’s Score.

We can therefore replace their responses in Fernanda’s equation with their original calculations. OK then. Let’s do exactly that:

Predicted Price Units = 0.5 × ((2 × Bedrooms) + (1 × Location Code) − 4) + 0.5 × ((1 × Bedrooms) + (3 × Location Code) − 3) + 1

Now look carefully at what happens to Predicted Price Units when we first multiply out the terms:

Predicted Price Units = (0.5 × (2 × Bedrooms)) + (0.5 × (1 × Location Code)) + (0.5 × -4) + (0.5 × (1 × Bedrooms)) + (0.5 × (3 × Location Code)) + (0.5 × -3) + 1

And continue reading to see what we get for Predicted Price Units when we change the position of some of the terms so that:

  • Both entries for Bedrooms (Omar’s and Ricardo’s) are together,
  • Both entries for Location Code (Omar’s and Ricardo’s) are together, and
  • The three entries for Bias (Omar’s, Ricardo’s, and Fernanda’s) are together.

We end up with the following:

Predicted Price Units = (0.5 × (2 × Bedrooms)) + (0.5 × (1 × Bedrooms)) + (0.5 × (1 × Location Code)) + (0.5 × (3 × Location Code)) + (0.5 × -4) + (0.5 × -3) + 1

OK. We are almost there. Now, the total contributions from Bedrooms become:

(0.5 × 2 × Bedrooms) + (0.5 × 1 × Bedrooms)

= (1 + 0.5) × Bedrooms

= 1.5 × Bedrooms

Similarly, the total contributions from Location Code become:

(0.5 × 1 × Location Code) + (0.5 × 3 × Location Code)

= (0.5 + 1.5) × Location Code

= 2 × Location Code

And finally, the three constant values, representing the biases, combine to give us the following:

(0.5 × −4) + (0.5 × −3) + 1

= − 2 − 1.5 + 1

= −2.5

Phew! Putting everything together, Fernanda’s entire three-agent calculation becomes:

Predicted Price Units = (1.5 × Bedrooms) + (2 × Location Code) + (-2.5) or more simply:

Notice something rather surprising in the above equation: We started with three agents, each using their own weights and biases independently. Yet, after combining their calculations, we ended up with exactly the same general structure we encountered in Article 2. “What is the general structure?” I hear you ask. Well, this is the structure that includes a weighted contribution from Bedrooms, plus a weighted contribution from Location, plus a starting amount (bias)!

WHAAAAAT? Are you serious?

Yes, please relax, and don’t panic on me just yet! Let’s verify this using Property D, our familiar two-bedroom property in a Prime location to see what is happening.

For Property D we have:

  • Bedrooms = 2
  • Location Code = 1

So, using the simplified equation we get:

Predicted Price Units = (1.5 × 2) + (2 × 1) − 2.5

= 3 + 2 − 2.5

= 2.5 units, or $250,000

We can also obtain exactly the same result by calculating the agents’ responses separately, without ReLU.

Omar’s Score = (2 × 2) + (1 × 1) − 4 = 1

Ricardo’s Score = (1 × 2) + (3 × 1) − 3 = 2

Fernanda’s Prediction = (0.5 × 1) + (0.5 × 2) + 1 = 2.5 units, or $250,000

The two approaches produce identical results! Darn! Why should anyone care if both approaches produce the same result? In fact, is there any point to this line of reasoning?

Yes, there is. Because there is an important detail worth noticing here: For Property D, both Omar and Ricardo already produced positive scores (1 and 2 respectively). ReLU would therefore leave their responses unchanged anyway.

But, the difference between the two approaches becomes visible when a score is negative.

Aha! And here is how:

Consider Property A, our two-bedroom property in a Standard location.

Omar’s Score = (2 × 2) + (1 × 0) − 4 = 0

Ricardo’s Score = (1 × 2) + (3 × 0) − 3 = −1

Without ReLU, Ricardo sends −1 to Fernanda. So, we will get:

Fernanda’s Prediction = (0.5 × 0) + (0.5 × −1) + 1 = 0.5 units, or $50,000.

But with ReLU, Ricardo’s negative score becomes zero, and we get:

Fernanda’s Prediction = (0.5 × 0) + (0.5 × 0) + 1 = 1 unit, or $100,000.

So:

And here lies the important distinction:

Without ReLU (or any other nonlinear activation function), our three-agent network is mathematically equivalent to a single linear calculation of the original inputs. So, simply adding more agents and more layers of weighted sums would not change that fundamental limitation of our original model.

With ReLU, however, something important changes: Instead of passing their calculated scores directly to Fernanda, Omar and Ricardo replace any negative score with zero while leaving positive scores unchanged. This allows the network to respond differently to changes in the original inputs, depending on whether an intermediate score is positive or negative. As a result, the network can represent relationships that a single linear calculation cannot.

Figure 2 below shows the prices predicted by our three-agent network for Standard and Prime properties with two, three and four bedrooms using ReLU.

Figure 2: Predicted Prices for Standard and Prime Properties using ReLU

Look closely at the Standard properties in Figure 2. We see that:

  • With two bedrooms, the predicted price is 1 unit ($100,000),
  • With three bedrooms, the predicted price is 2 units ($200,000),
  • With four bedrooms, the predicted price is 3.5 units ($350,000).

Notice what happens when we add one bedroom:

  • Moving from two to three bedrooms increases the prediction by 1 unit (2 – 1), which is equivalent to $100,000,
  • But moving from three to four bedrooms increases it by 1.5 units (3.5 – 2), or $150,000.

Why the difference?

Because Ricardo’s score is negative for the two-bedroom Standard property (-1), zero for the three-bedroom property (0), and positive for the four-bedroom property (1). ReLU, therefore, blocks his negative score (equivalent to passing zero), passes zero when the score is zero, and passes his positive score, unchanged, onward. This changes how the network responds as the number of bedrooms increases.

Our original linear model in Article 2, using separate contributions from Bedrooms and Location, would always assign the same additional amount to one more bedroom, provided Location remained unchanged.

This is why ReLU matters. It allows the network to capture relationships that our earlier linear model could not represent.

Does this mean our three-agent network is necessarily a better price predictor? Not at all! As we saw in Table 4, its current predictions are rather poor😞. What we have gained is greater flexibility, not guaranteed accuracy.

And now that we understand why combining several artificial neurons can be useful, we are ready to give this arrangement its proper technical name…

The structure we have constructed is called a Multilayer Perceptron (MLP), a type of feedforward artificial neural network. Our particular network has two numerical inputs, one hidden layer containing two artificial neurons, and an output layer containing one artificial neuron.

“Hidden” does not mean mysterious or inaccessible. It simply means these two units are between the input and the final output. Omar and Ricardo each produce an intermediate response while Fernanda produces the final prediction.

Our Estate-Agency StoryWhat the Actual Network Does
Bedrooms and Location Code arriveReceives two numerical inputs
Omar and Ricardo each examine the property and calculate a scoreTwo hidden neurons each calculate a weighted sum + bias, then apply ReLU
Each junior sends Fernanda one messageTwo numerical hidden outputs become inputs to the output neuron
Fernanda combines their messagesOutput neuron computes its weighted sum + bias
Fernanda states a price estimateNetwork produces one numerical prediction
Table 5: The Human Analogy and the Actual Computation

A useful count: Omar has 3 parameters (two weights, one bias), Ricardo has 3 (two weights, one bias), and Fernanda has 3 (two incoming weights, one bias). That makes 9 adjustable parameters in our tiny network. The number of properties and their bedroom counts are inputs, not parameters.

For readers comfortable with notation, let x₁ be Bedrooms and x₂ be Location Code. Also, let O represent Omar’s score, R represent Ricardo’s score and ŷ represent Fernanda’s final prediction. Then:

Those symbols describe exactly what we have already calculated. No new magic has been smuggled in. However, you may probably look smarter amongst your friends if you wrote the calculations using these symbols instead😊.

We now have an MLP: Information flows through two intermediate neurons and reaches a final neuron, which predicts a price. This is an essential step toward understanding neural networks. Nice!

But the network does not improve itself merely because the three agents are cooperating. All nine settings were supplied by us. The model has not yet “learned” from the six historical sale prices.

Look at Property D again. $250,000 predicted against $300,000 of actual price. We could raise Fernanda’s bias, increase her weight on Ricardo, change Omar’s bedroom weight, or adjust several settings together. Each choice affects the prediction differently, and because the same parameters serve all six houses, improving D could make another house worse.

So which setting should change, in which direction, and by how much? That is not a question the forward pass can answer. We need a way to measure the mistake and trace how each setting contributes to it.

  1. Why do Omar and Ricardo receive the same two property inputs but produce different messages?
  2. What does ReLU send onward when its input is −1? What about 2?
  3. For Property D, reproduce the values 1, 2 and 2.5.
  4. Why is Fernanda’s final unit linear rather than a Step Function?
  5. If all activations were linear, what kind of calculation would the whole network reduce to?
  6. How many adjustable parameters does this MLP contain?
  7. Does an accurate forward calculation guarantee an accurate prediction?

We have progressed from one TLU to a network of three artificial neurons. We kept the same two property inputs and the same six houses, but changed how those inputs are processed. Omar and Ricardo create intermediate numerical responses, Fernanda combines them into a price. ReLU introduces nonlinear behavior, allowing the network to represent relationships beyond a single straight-line model. The forward pass tells us what the network predicts using its current settings.

What it does not tell us is how to improve those settings. An architecture that can make predictions is necessary but not sufficient for learning. That brings us to Article 5.

Property D is still staring at us: The team estimated $250,000 for a house that sold for $300,000. There are nine settings it could change, and guessing is not a particularly reassuring training strategy. In our next article, we will introduce loss, gradients, backpropagation and gradient descent, beginning with intuition and then mathematics, and follow one learning step from beginning to end.

  1. Because the agents use different weights and biases.
  2. ReLU(−1) = 0, ReLU(2) = 2.
  3. Omar: ReLU((2 × 2) + (1 × 1) − 4) = 1, Ricardo: ReLU((1 × 2) + (3 × 1) − 3 = 2) = 2, Fernanda: (0.5 × 1) + (0.5 × 2) + 1 = 2.5.
  4. We want a numerical price, rather than only 1 or 0 (yes or no).
  5. A single linear calculation of the original inputs, using weights and a bias, just like the model we introduced in Article 2.
  6. Nine: Three in each of three neurons.
  7. No. The calculations can be correct while the chosen parameter values produce a poor prediction.

I welcome your questions, corrections and suggestions. In particular, which part of the move from one neuron to several was least obvious?

NEXT ARTICLE

The Estimate Was Wrong. What Should the Team Change?
Understanding Loss, Gradients and Backpropagation

NEXT ARTICLE
Title: The Prediction Was $50,000 Too Low. Which Number Should Change?
Concept: Understanding Loss, Gradients and Backpropagation

← Back to the AI Explained Roadmap.

Can a Computer Make a Decision Without Understanding Anything?

Understanding the Threshold Logic Unit (TLU)

1. Why are we back to YES or NO?

What? “You must be joking”, I hear you say! Sadly, I am not.

At the end of Article 2, Fernanda had finally learned how to combine information about a property to produce something much more satisfying than YES or NO (an actual predicted sale price).

So why on earth are we going backwards?

Well, we are actually not! The question we are about to ask may be familiar, but the way we answer it will be different.

Back in Article 1, Fernanda had to decide whether a property belonged in the agency’s $300,000-and-above category:

There, we gave the computer several complete YES/NO rules and asked it to use historical examples to choose the one that worked best.

Then, in Article 2, we changed the task. Instead of choosing among complete rules, Fernanda learned how numerical information about a property could be combined to predict an actual sale price.

And now we are deliberately returning to the YES/NO question.

Couldn’t we simply take the price predicted in Article 2 and check whether it is at least $300,000?

Absolutely:-). And for our little property example, that would be perfectly reasonable. We do not actually need a new mechanism merely to answer this particular question.

But many real problems ask directly for a decision rather than for a numerical quantity first. For example:

  • Approve or reject,
  • Fraud or legitimate,
  • Spam or not spam,
  • Disease detected or not detected,
  • YES or NO.

In such cases, it can be useful to build the decision directly from the available inputs.

That is what we are going to explore here. In other words, instead of first predicting a price, we will let the available property information contribute to a numerical score, and then use that score to decide whether the answer should be YES or NO.

Admittedly, there is also another reason for taking this apparent detour: This simple mechanism turns out to be an important stepping stone toward something much more powerful: The artificial neuron and, eventually, Neural Networks. Yupee!

So, we are not returning to YES/NO because Article 1 failed. We are returning to it because we are now ready to understand a different mechanism for producing that decision. That is to say we are aiming for:

2. Let us keep the same six houses

There is no need to invent another housing market simply because we have started another article. So, we will continue using the same six historical properties that accompanied Fernanda through Article 1 and Article 2 (see Table 1 below).

PropertyBedroomsLocationActual Sale PriceCorrect Category
A2Standard$200,000NO
B3Standard$250,000NO
C4Standard$280,000NO
D2Prime$300,000YES
E3Prime$400,000YES
F4Prime$450,000YES
Table 1: The Same Six Historical Property Sales

As before:

  • NO means the property sold for less than $300,000,
  • YES means the property sold for $300,000 or more.

The Actual Sale Price is useful because these are historical properties, meaning we already know what actually happened. In contrast, when a new property arrives, its eventual sale price is not yet known.

Fernanda therefore needs to use the information she does have about that property to produce a prediction. And once again, for our deliberately simplified little world, she has only two pieces of information:

  • Number of Bedrooms
  • Location

So the difference this time is not the data, but is what we are going to do with it.

3. What information does Fernanda already have?

Fortunately, Fernanda is not starting from scratch. If you recall, in Article 2, we already saw that the two inputs do not arrive in quite the same form:

Bedrooms is already numerical. A house may have 2, 3 or 4 bedrooms, so that input is ready to take part in a calculation.

Location, however, arrives as a category:

  • Standard
  • Prime

In Article 2, we gave those two possibilities a simple numerical representation (see Table 2 below):

LocationLocation Code
Standard0
Prime1
Table 2: Numerical Representation of Location

Remember that this does not mean Standard is “worth zero” while Prime is “worth one”! The numbers simply provide a convenient numerical way to distinguish the two possibilities.

We also learned something else important in Article 2: Different inputs can make different numerical contributions to a calculation, and those contributions can be controlled by adjustable numerical settings. Later, we gave those settings their usual Machine Learning name: Weights.

We also encountered another adjustable setting: Bias. So Fernanda already has several useful ingredients available:

  • Numerical inputs,
  • Weights that control how those inputs contribute to a calculation,
  • And a bias that can shift the result.

But there is one important difference between Article 2 and the problem in front of us now; In Article 2, all those ingredients were combined to produce a predicted price. This time, however, Fernanda does not actually need a price. Instead, she needs a decision: YES or NO.

4. From a predicted price to a score

In Article 2, Fernanda combined numerical contributions from Bedrooms and Location to produce something with an obvious real-world meaning: A predicted sale price.

This time, however, she does not need to produce a price. She merely needs enough numerical information to help her decide between NO and YES.

So Fernanda tries a slightly different idea: What if Bedrooms and Location each contribute to a single number, but instead of interpreting that number as dollars, she simply uses it as a score? The score would not itself be the final answer, though. It would simply summarize the contributions from the information available to her.

To keep our first attempt easy to follow, suppose Fernanda starts with these trial settings:

  • Bedroom Weight = 1
  • Location Weight = 4

Remember what these numbers mean: A Bedroom Weight of 1 means that each additional bedroom contributes 1 score unit, while a Location Weight of 4 means that moving from Standard, whose Location Code is 0, to Prime, whose Location Code is 1, contributes an additional 4 score units.

Note that these are simply trial settings. We are not claiming that Location is somehow “four times more important” than Bedrooms in the real housing market!

Armed with these values, Fernanda can now calculate the following:

Score = (1 × Bedrooms) + (4 × Location Code)

Let us see what happens to our six historical properties (see Table 3 below):

PropertyBedroomsLocation CodeScore CalculationScoreCorrect Category
A20(1 × 2) + (4 × 0)2NO
B30(1 × 3) + (4 × 0)3NO
C40(1 × 4) + (4 × 0)4NO
D21(1 × 2) + (4 × 1)6YES
E31(1 × 3) + (4 × 1)7YES
F41(1 × 4) + (4 × 1)8YES
Table 3: Turning the Property Inputs into Scores

Fernanda has therefore transformed the six properties into the following sequence of scores:

A warning is useful here: A score of 8 does not mean $80,000, $800,000 or an 80% probability of anything. At this stage, the score is simply an internal numerical value produced from the inputs. Its usefulness will depend on what Fernanda does with it next.

5. A score is useful, but it is not yet a decision

Fernanda now has six scores. Very, very nice!

Unfortunately, none of her clients has asked: “Fernanda, what is my score?” Nope. They want an answer in an all-or-nothing kind of way: YES or NO.

So Fernanda needs another rule that turns the score into a decision. Look again at the historical examples (see Table 4 below):

Correct CategoryScores
NO2, 3, 4
YES6, 7, 8
Table 4: Scores for the Historical NO and YES Examples

Something rather convenient has happened, has it not? All the historical NO examples have scores of 4 or below, while all the historical YES examples have scores of 6 or above. Hmmm, interesting.

There is, therefore, a gap between the two groups. This means that Fernanda could place a dividing point (a cutoff, shall we say) somewhere inside that gap. For convenience, let us choose:

Cutoff = 5

Then her decision rule becomes:

  • If Score < 5 → Predict NO
  • If Score ≥ 5 → Predict YES

That dividing value has a standard technical name: Threshold. So our cutoff of 5 is also the decision threshold.

The idea itself is wonderfully simple:

6. Let us see whether the decision rule actually works

We can now apply Fernanda’s new rule to all six historical houses (see Table 5 below).

Remember:

  • Score < 5 → NO
  • Score ≥ 5 → YES
PropertyScoreDecision Using Threshold 5Correct CategoryResult
A2NONOCorrect
B3NONOCorrect
C4NONOCorrect
D6YESYESCorrect
E7YESYESCorrect
F8YESYESCorrect
Table 5: Turning Scores into YES/NO Predictions

Well, that went suspiciously well! For our deliberately simple historical examples, Fernanda’s trial weights together with a threshold of 5 classify all six properties correctly. Aren’t we lucky!

But let us concentrate on the mechanism rather than congratulate Fernanda too enthusiastically.

She performed two distinct operations:

First: Calculate

She combined the inputs to produce a numerical score.

Second: Decide

She compared that score with a threshold and produced either NO or YES.

We can write the second operation very simply:

  • Score below threshold → 0
  • Score at or above threshold → 1

where:

  • 0 represents NO
  • 1 represents YES

A rule that changes its output from one value to another when a threshold is crossed is commonly called a Step Function.

Again, the name is less important than the mechanism. We already understood what the rule did before learning what it was called!

7. But what happened to the bias?

You may have noticed something slightly odd.

In Article 2, bias played an important role in our calculation.

Yet so far in this article, Fernanda has used:

  • Bedroom Weight,
  • Location Weight,
  • And a threshold,

but no bias.

Has bias mysteriously disappeared? Nope. In fact, this gives us a useful opportunity to understand the relationship between bias and the threshold.

Our current scores are:

and Fernanda compares them against a threshold of:

So she currently asks: Is the score at least 5?

But suppose instead that she subtracts 5 from every score (see Table 6 below):

Original ScoreSubtract 5New Score
2−5−3
3−5−2
4−5−1
6−51
7−52
8−53
Table 6: Shifting Every Score by the Same Amount

Now look what has happened: The NO examples have become: −3, −2, −1 while the YES examples have become: 1, 2, 3.

So, Fernanda no longer needs to compare them with a threshold of 5. She can simply ask:

  • If Score < 0 → NO
  • If Score ≥ 0 → YES

Nothing about the final classifications has changed. We have simply moved the scores rather than moving the threshold.

And how do we shift every score downward by 5? We add the same value, −5, to every score to get:

That final value, −5, is our bias.

So there are two equivalent ways of describing what Fernanda just did:

Version 1

Calculate the score without bias and compare it with a threshold of 5.

Version 2

Add a bias of −5 to the score and compare the result with a threshold of 0.

For our example, both produce exactly the same decisions.

This also gives us a more concrete way to understand the bias we first encountered in Article 2:

Fernanda now has everything she needs:

  • Numerical inputs,
  • Weights,
  • A bias,
  • A score,
  • A threshold,
  • And a rule that turns the score into 0 or 1.

We have, without formally naming it yet, built something rather important.

8. We have just built a Threshold Logic Unit

A Threshold Logic Unit? What is that?

Let us pause for a moment and look at what Fernanda has actually constructed. She:

  1. Receives numerical inputs,
  2. Lets each input make a contribution controlled by a weight,
  3. Adds those contributions together with a bias to produce a score,
  4. Compares that score with a threshold,
  5. Produces one of two possible outputs: 0 or 1.

That complete mechanism has a technical name:

Or, rather mercifully for those of us who dislike unnecessarily long names:

So our simple property decision can now be written as:

Using our current settings:

  • Bedroom Weight = 1
  • Location Weight = 4
  • Bias = −5

we have:

Then the TLU applies its decision rule:

  • If Score < 0 → Output = 0
  • If Score ≥ 0 → Output = 1

where:

  • 0 means NO
  • 1 means YES

For example, consider Property D:

  • Bedrooms = 2
  • Location Code = 1

The score is:

Score = (1 × 2) + (4 × 1) − 5 = 1

Since 1 is at or above the threshold of 0:

Output = 1 → YES

And that matches the known historical category for Property D.

Note that nothing magical has happened. The TLU has simply performed a numerical calculation and then applied a threshold-based decision rule. Easy, right?

Figure 1 below summarizes the entire mechanism in one picture.

This is the basic mechanism we will eventually see again inside much larger Neural Networks.

9. Weights, bias and threshold do different jobs

Now that we have the complete mechanism in front of us, it is worth separating the roles of its three main numerical ingredients. They are related, but they do not do the same thing.

Weights control the contribution from each input:

Consider our score calculation:

The Bedroom Weight is 1. So increasing Bedrooms by one increases the score by 1, assuming everything else stays the same.

The Location Weight is 4. So moving from Standard (Location Code = 0) to Prime (Location Code = 1) increases the score by 4, assuming everything else stays the same.

The weights therefore control how strongly changes in each input affect the score.

But we should be careful with the word importance. A Location Weight of 4 and a Bedroom Weight of 1 do not automatically prove that Location is “four times more important” than Bedrooms in the real world.

The two inputs are represented on different numerical scales, and the weights belong to this particular model.

Bias shifts every score:

Now consider the bias: Bias = −5. Before adding the bias, our six scores were: 2, 3, 4, 6, 7, 8.

After adding −5, they became: −3, −2, −1, 1, 2, 3. So, every score moved downward by exactly the same amount. That means something important:

For example, before adding the bias: 2 < 3 < 4 < 6 < 7 < 8 and after adding the bias: −3 < −2 < −1 < 1 < 2 < 3. So, the order is unchanged.

This is one of the most useful distinctions to remember. If our weights arrange the properties in an unsuitable order, changing the bias alone cannot repair that problem.

The threshold decides where the output changes:

Finally, the threshold determines the point at which the TLU changes its output.

With our current convention:

  • Score < 0 → 0
  • Score ≥ 0 → 1

the threshold is 0.

So, the weights and bias produce the score, the threshold then decides what that score means for the final YES/NO decision.

A useful summary is:

  • Weights shape the score,
  • Bias shifts the score,
  • Threshold turns the score into a decision.

That distinction will become increasingly important as our models become more complicated.

10. Why keep the threshold at zero?

At this point you might reasonably ask:

Excellent question!

And for this particular example, we absolutely could. In fact, these two approaches are equivalent:

Approach A

Use Score = (1 × Bedrooms) + (4 × Location Code) and compare the score with Threshold = 5.

Approach B

Use Score = (1 × Bedrooms) + (4 × Location Code) − 5 and compare the score with Threshold = 0.

Both produce exactly the same classifications for our six properties. So why is the second form useful?

Because keeping the decision threshold fixed at zero gives us a convenient standard form:

That makes the mechanism easier to express consistently. And you know mathematicians: The just love consistency:-).

So, instead of moving the threshold around, we can let the bias absorb that shift. Pretty clever, right?

This is why you will often see simple threshold-based units written in the form:

followed by:

  • Score < 0 → 0
  • Score ≥ 0 → 1

It does not mean zero is somehow a naturally privileged decision point in the real world. We have simply chosen a convenient mathematical representation.

And that gives us another useful interpretation of bias:

At this stage, our chosen weights and bias happen to classify all six historical examples correctly.

But we should not get too comfortable. After all, we deliberately chose settings that make the mechanism easy to understand. But:

11. What happens when the TLU gets one wrong?

So far, Fernanda has enjoyed suspiciously good luck. With Bedroom Weight = 1, Location Weight = 4 and Bias = −5, her TLU classified all six historical properties correctly.

But learning becomes much more interesting when the settings are not already perfect. So let us deliberately make Fernanda’s starting point a little worse.

Suppose she had instead started with:

  • Bedroom Weight = 1
  • Location Weight = 4
  • Bias = −7

Her score calculation would then be:

Score = (1 × Bedrooms) + (4 × Location Code) − 7

Now consider Property D:

  • Bedrooms = 2
  • Location Code = 1
  • Correct Category = YES

Fernanda calculates:

Score = (1 × 2) + (4 × 1) − 7 = −1

Since the score is below zero, the TLU predicts:

Output = 0 → NO

But the historical answer is:

Output = 1 → YES

So the prediction is wrong. Noooooooooooooo!

Let’s cool down a bit. This is exactly the kind of situation a learning procedure needs to respond to. Let’s see what we can do here.

In Article 2, we measured prediction error by comparing the actual value with the predicted value. We can use the same basic idea here.

The actual output is 1. The predicted output is 0. So:

Error = Actual Output − Predicted Output = 1 − 0 = +1

The positive error tells us something important:

In other words, for this example, the score needs to move upward enough to cross the threshold.

But knowing that a mistake occurred is only the beginning.

12. One small adjustment

Let us look at Property D again.

Its current score is −1. The threshold is 0.

So Fernanda does not need an enormous change. She merely needs the score to move from below zero to zero or above.

One simple way to do that is to increase the bias. Suppose Fernanda changes Bias = −7 to Bias = −6.

The weights remain unchanged.

Now she recalculates Property D:

And since Score ≥ 0, the TLU now produces:

The prediction has changed from wrong to correct. But we should check that Fernanda has not repaired one property by breaking all the others.

So, with the new bias of −6, the six scores become (see Table 7 below):

PropertyScorePredictionCorrect CategoryResult
A−4NONOCorrect
B−3NONOCorrect
C−2NONOCorrect
D0YESYESCorrect
E1YESYESCorrect
F2YESYESCorrect
Table 7: Predictions After Adjusting the Bias

So this particular adjustment works nicely for our six historical examples. Great!

But do not read too much into the fact that Fernanda found a useful new bias immediately.

We chose a deliberately simple example so that we could see what an adjustment actually does. A real learning procedure needs a consistent rule for deciding the following:

  • Which setting or settings should change,
  • In which direction,
  • And by how much.

We are deliberately postponing that machinery here. Later in this series, we will return to the much deeper question of how learning procedures decide how numerical settings should change.

For now, the important idea is simpler:

13. Did Fernanda really “learn” the right answer?

This is worth slowing down for:

Fernanda did not rewrite the whole decision mechanism after seeing one mistake. She kept the same general structure:

What changed was one numerical setting inside that structure:

That distinction matters!

In Article 1, learning meant choosing among several complete YES/NO rules.

In Article 2, learning meant finding numerical settings inside a linear price-prediction model.

Here, learning can once again mean changing numerical settings. But this time inside a mechanism whose final output is a YES/NO decision.

So the basic pattern continues:

That does not mean Fernanda or the algorithm has somehow discovered everything worth knowing about housing.

People still decided:

  • That the task was to predict whether a property belongs in the $300,000-and-above category,
  • That Bedrooms and Location would be the available inputs,
  • How Location would be represented numerically,
  • That the decision mechanism would use a TLU,
  • That the output would be represented as 0 or 1,
  • And how the learning procedure would be allowed to adjust the model.

This is key so, remember it well:

This is the same boundary we have been building throughout the series:

And that prepares us for a useful question:

14. Time to attach the technical names

We have now used nearly all the important pieces of a Threshold Logic Unit.

As usual in this series, we deliberately met the ideas before worrying too much about their technical names. So let us now tidy things up (see Table 8 below):

What We Have Been UsingUsual Technical Name
Bedrooms and Location CodeInputs or Features
The numerical setting controlling each input’s contribution to the scoreWeight
The value added to shift all scoresBias
The number produced after combining the input contributions and biasScore
The dividing point at which the decision changesThreshold
The rule that converts the score into 0 or 1Step Function
0 or 1 produced after applying the Step FunctionOutput or Prediction
The complete decision unitThreshold Logic Unit (TLU)
Table 8: Technical Names for the TLU Concepts We Have Already Used

None of these names introduces a new mechanism. We already know what each part does.

The terminology simply gives us a shorter and more standard way to talk about it.

There is one additional term worth introducing: The weights and bias are numerical values that can be adjusted during learning. Such adjustable numerical settings are commonly called:

So, in our TLU:

  • Bedroom Weight is a parameter,
  • Location Weight is a parameter,
  • Bias is a parameter.

The inputs are not parameters. They are the property information supplied to the TLU.

That distinction matters so, read this last passage again to make sure you are absolutely clear about that!

For Property D, for example:

  • Bedrooms = 2 and Location Code = 1 are the inputs,
  • Bedroom Weight = 1, Location Weight = 4 and Bias = −6 are the parameters.

The inputs can change from one property to another. The parameters describe the model we are currently using.

15. How mathematicians write the same mechanism

So far, we have deliberately written our calculations using descriptive names (well, apart from in Figure 1 above, when my mathematical past got the better of me!):

That makes the meaning easy to follow. But as we saw in Article 2, mathematicians and Machine Learning texts usually shorten such expressions using symbols.

Our two-input TLU score can be written as:

where:

  • x1 is the first input,
  • x2 is the second input,
  • w1 is the weight attached to the first input,
  • w2 is the weight attached to the second input,
  • b is the bias.

For our property example:

  • x1 = Bedrooms,
  • x2 = Location Code,
  • w1 = Bedroom Weight,
  • w2 = Location Weight.

Using the settings from our latest example:

  • w1 = 1,
  • w2 = 4,
  • b = −6,

we have:

Score = (1 × Bedrooms) + (4 × Location Code) − 6

Then comes the second part of the TLU:

Output = Step(Score)

The Step Function simply applies the decision rule we already understand:

  • If Score < 0 → Output = 0
  • If Score ≥ 0 → Output = 1

So the compact mathematical version has not introduced anything new.

It has merely compressed the mechanism we have already built:

This is exactly the discipline we followed in Article 2:

And now that both the mechanism and notation are in place, we are ready to put Fernanda and the TLU side by side.

16. Fernanda and the TLU side by side

By now, the resemblance between Fernanda’s reasoning and the TLU mechanism should be fairly clear.

Fernanda receives information about a property, gives different pieces of information different numerical influence, combines them into a score and then makes a YES/NO decision.

The TLU does the same kind of numerical work, without Fernanda’s experience, intuition or appreciation for suspiciously enthusiastic estate-agent descriptions!

We can now compare the two directly (see Table 9 below):

FernandaThreshold Logic Unit
Receives information about a propertyReceives numerical inputs
Lets different facts contribute differently to the decisionMultiplies each input by its weight
Combines those contributions and a general shiftAdds the weighted contributions and bias
Obtains an overall impression or scoreProduces a numerical score
Decides whether the score is enough for YESCompares the score with the threshold
Says YES or NOProduces 1 or 0
Table 9: Fernanda and a TLU Performing the Same Basic Decision Process

This comparison is useful, but we should keep one boundary very clear:

Fernanda understands what a bedroom is. She knows what a Prime location means. She has experience of buyers, neighborhoods and housing markets.

The TLU, on the other hand, understands none of those things. It receives numerical values and performs numerical calculations.

Figure 2 below places the two processes side by side:

17. Is a TLU already an artificial neuron?

We are now very close to terminology that appears throughout Neural Networks.

A TLU has:

  • Inputs,
  • Weights,
  • A bias,
  • A calculation that combines them,
  • And a function that converts the resulting score into an output.

That structure should begin to look familiar if you have ever seen a diagram of an artificial neuron. And there is a good reason for that:

A Threshold Logic Unit is a very simple form of artificial neuron.

It receives numerical inputs, combines them using weights and bias, and then applies a threshold-based function to determine its output.

In our example:

This is an important milestone in our journey. We have moved from:

  • Choosing among complete rules in Article 1,
  • Learning numerical settings for a price prediction in Article 2,
  • Using weighted inputs, bias and a threshold to create a simple decision-making unit here.

That unit is now beginning to resemble the building block from which Neural Networks are constructed.

But there is an important limitation: One TLU can make only a relatively simple decision.

18. What can one TLU actually do?

Our TLU has done rather well in Fernanda’s deliberately simple little housing world. It successfully separates:

  • The three historical NO examples,
  • The three historical YES examples.

But this success depends heavily on the pattern in our data being simple enough for one threshold-based decision to separate the two groups.

One TLU essentially asks:

Sometimes the answer is yes. And sometimes it is not.

Suppose, for example, that the pattern in the data became more complicated. Perhaps something like this:

  • Some Prime properties should be NO,
  • Some Standard properties should be YES,
  • And the answer depended on several combinations of features rather than one simple dividing rule.

In this case then merely shifting the bias may not help. And changing the weights may still not be enough if the pattern itself cannot be separated by one simple threshold decision. Oh, no!

This is where the limitation of a single TLU becomes important. A single unit can combine information once and make one threshold-based decision.

But what if the problem needs several intermediate decisions? What if one simple score is not enough? What if different pieces of evidence need to be combined in stages?

These questions give us the natural next step in our journey:

19. Question time

Before moving on, consider the following questions:

  1. What are the two inputs used by Fernanda’s TLU?
  2. What role does a weight play in the score calculation?
  3. What does the bias do to the scores?
  4. What is the difference between a score and the final TLU output?
  5. Why can changing the bias move the decision boundary without changing the order of the scores?
  6. What is the role of the threshold?
  7. Why is a TLU different from the complete-rule approach we used in Article 1?
  8. If the TLU predicts 0 but the correct output is 1, what does that tell us?
  9. Why can one TLU struggle when the pattern in the data is more complicated?

20. What have we learned?

We started this article by returning to a familiar YES/NO problem.

In Article 1, Fernanda solved that problem by choosing among complete prediction rules.

In Article 2, she learned how numerical inputs could be combined to predict an actual sale price.

This time, we used those numerical ideas to construct a direct YES/NO decision mechanism.

We saw that:

  • Numerical inputs can contribute to a single score,
  • Weights control how each input contributes to that score,
  • Bias shifts the score,
  • A threshold determines where the output changes,
  • A Step Function converts the score into 0 or 1,
  • The complete mechanism is called a Threshold Logic Unit, or TLU,
  • Weights and bias are adjustable model parameters,
  • Prediction errors can provide information that helps a learning procedure change those parameters,
  • Bias can shift all scores but cannot change their order,
  • And one TLU can make only a relatively simple threshold-based decision.

Most importantly, we have now reached an important conceptual milestone:

That may sound modest. But it is anything but!

This basic structure is one of the foundations from which more powerful Neural Networks are built. And, as always, the human analogy has limits.

Fernanda understands houses, neighborhoods and clients.

The TLU does not. It receives numbers, performs calculations and produces an output.

Keeping that distinction clear allows the analogy to help us without pretending that the algorithm is thinking like a human.

21. Next: What if one decision is not enough?

Fernanda can now make one simple threshold-based decision. But what if the problem is more complicated? What if one useful decision depends on several smaller decisions? What if Bedrooms and Location are not enough to produce the answer directly, but different intermediate judgments could help? And what if several simple decision-making units could work together?

That takes us to the next step in our journey. Nice!

Instead of one Fernanda making one decision, imagine Fernanda working with two colleagues: Each one receives information, and each one contributes something. Their outputs can then become inputs to another decision.

And suddenly our simple TLU begins to grow into something much more powerful. Bring it on!

22. Question time: Suggested answers

Here are some answers to the questions posed in section 19 above:

  1. Bedrooms and Location Code.
  2. A weight controls how strongly its corresponding input contributes to the score.
  3. The bias shifts every score by the same amount.
  4. The score is the numerical value calculated before the decision is made. The final output is 0 or 1 after the threshold rule is applied.
  5. Because the same bias is added to every score, the scores all move together. Their relative order therefore remains unchanged.
  6. The threshold determines the point at which the TLU changes its output from 0 to 1.
  7. In Article 1, the learning procedure selected among complete rules. Here, the decision is produced by combining numerical inputs using weights and bias, calculating a score and applying a threshold.
  8. The model has made an error. The numerical settings may need to be adjusted so that similar examples are more likely to receive the correct output.
  9. A single TLU produces only one threshold-based separation. Some patterns require several intermediate decisions or combinations that one simple unit cannot represent.

Feedback please 😊.

NEXT ARTICLE
Title: Three Agents, One Prediction. Is Teamwork Enough?
Concept: Understanding the Multilayer Perceptron (MLP)

← Back to the AI Explained Roadmap

Can Three Numbers Really Predict the Price of a House?

Understanding Linear Regression

At the end of our previous article, we saw how Fernanda successfully used a small learning procedure to choose a useful prediction rule from historical house sales.

This was all very satisfying until a seller asked the rather inconvenient question that many sellers have a habit of asking:

That is lovely, Fernanda. But how much do you think my house will sell for?

Do you see the problem now (this is a place where self-help gurus remind us that we should never say “problem” and instead should be using the word “challenge”)?

Anyway, whether you call it a problem or a challenge, to successfully answer this seemingly innocent question a simple YES or NO answer is suddenly not quite enough. Saying, “Good news, madam, my model says YES” may be technically responsive, but it is unlikely to endear Fernanda to someone who wants to know whether to expect $310,000 or $510,000.

So, Fernanda now has a different task ahead: She needs to predict a number. Oh boy!

And with that small change, we will be moving from choosing among complete rules to something rather more interesting: Learning numerical settings inside a prediction process.

1. The problem: What price should Fernanda predict?

We will deliberately keep the same six historical house sales from the previous article. After all, we like Fernanda and there is no need to make her sell another six houses merely because we have started a new article (see Table 1 below):

PropertyNumber of BedroomsLocationActual
Sale Price
A2Standard$200,000
B3Standard$250,000
C4Standard$280,000
D2Prime$300,000
E3Prime$400,000
F4Prime$450,000
Table 1: The Same Six Historical Property Sales from Article 1

In Article 1, these sale prices were used to create the known YES/NO categories. This time, the sale price itself is the quantity we want to predict.

As before, Fernanda still has only two pieces of information about each property:

  • Number of Bedrooms, and
  • Location: Standard or Prime

Of course, real house prices typically depend on more than two facts and may include floor area, condition, street, transport, interest rates, view, and timing. And in many cases, even the question of whether “excellent nightlife” means a pleasant wine bar or a nightclub under the bedroom window matters too.

But our simplification is deliberate. If we cannot understand what happens with two inputs and six houses, adding another 127 variables is unlikely to improve our mood. So, keep reading and don’t underestimate the power of simple datasets. So, now we have:

Our objective:

Using the available property details to predict an actual sale price.

2. Inputs and output: Let us keep our roles straight

Before Fernanda starts to get her hands dirty with fancy calculations, let us be precise about what is known and what is not (see Table 2 below):

RoleIn Our Example
InputsBedrooms and Location
Output to be predictedSale Price
Known answers in historical dataActual Sale Prices
Table 2: Inputs and Output

The actual sale price is available for an old house that has already sold. That is why we can use it to train our model (unearthing a new rule that leads to a prediction) and to check any prediction during learning.

For a new property, however, the eventual sale price is not yet known. If Fernanda already knew it, predicting it would lose some of its intellectual glamour!

3. Fernanda gets tired of staring at tables

Fernanda could begin by inventing a formula immediately. But she decides first to see whether the historical values show any visible pattern (this is probably a good place to remind you that regardless of the complexity of the work of data or AI experts, looking at your data is the most sensible first step. You will be surprised how much one can learn about a problem at hand by simply looking at the data at hand, before doing anything fancy!).

To keep the first picture simple, she temporarily looks only at the three Standard-location properties A, B and C. This means location is being held fixed while she looks at the relationship between bedroom count and sale price.

For the next few calculations, we will also measure price in units of $10,000. Thus $200,000 becomes 20, $250,000 becomes 25, and so on. This saves us from dragging four zeros through every line of arithmetic (I want to stress that while I am personally making this choice to make my life easier during the writing of this article, this type of numerical manipulation is quite common in Machine Learning and, in fact, in many other numerical sciences so, I am also using this opportunity to make you get a feel for this type of technique). Table 3 below captures the simplified data for the 3 standard houses that Fernanda is looking at closely now:

PropertyBedroomsLocationPrice in $10,000 Units
A2Standard20
B3Standard25
C4Standard28
Table 3: The Three Standard-Location Properties

Fernanda now wants to see the relationship rather than simply stare at the numbers in a table. A simple two-dimensional graph seems like a good place to start.

Such a graph uses two number lines, which mathematicians call axes. The horizontal one is usually called the x-axis, while the vertical one is called the y-axis. Each axis can represent a different quantity, allowing us to see how the two quantities appear to relate to one another.

In Fernanda’s graph, the horizontal x-axis will represent Number of Bedrooms, while the vertical y-axis will represent Sale Price. So, for example, Property A, with two bedrooms and a sale price of 20 units, can be placed on the graph at the point where Bedrooms = 2 and Sale Price = 20.

She does the same for Properties B and C, producing the three points shown in Figure 1 below.

Figure 1: Three Historical Examples Shown as Points

Each dot is simply one historical example. Property A tells us that one two-bedroom Standard property sold for 20 price units, or $200,000. Property B tells us that a three-bedroom Standard property sold for 25 units, and so on.

The dots seem to rise as the bedroom count rises. That does not prove anything profound about the housing universe, but it does suggest that a simple increasing relationship might be worth trying. Hmm. Interesting. Let’s continue.

4. Fernanda tries a straight-line price calculation

Fernanda notices that Property A has two bedrooms and sold for 20 units, while Property B has three bedrooms and sold for 25 units.

It appears that one extra bedroom is associated, in these two examples, with an extra 5 price units, or $50,000.

So, she tries a very simple idea:

Predicted Price = Starting Amount + (5 × Number of Bedrooms)

But what should the Starting Amount be?

For Property A, Fernanda has two bedrooms. At 5 units for each bedroom, those two bedrooms contribute the following amount:

5 × 2 = 10 units

But Property A actually sold for 20 units. So, if Fernanda wants her first calculation to get Property A exactly right, another 10 units are still needed:

Amount Still Needed to Fully Account for Property A’s Price = 20 − 10 = 10

But hang on a minute! Could Fernanda use this remaining 10 units as the amount from which her calculation starts?

In other words, before adding anything for bedrooms, could she begin with 10 units and then add the bedroom contribution on top?

If so, that gives us:

Starting Amount = Amount Still Needed to Fully Account for Property A’s Price = 10

And, therefore, Fernanda can now rewrite her earlier calculation more specifically:

Predicted Price = 10 + (5 × Number of Bedrooms)

By the way, perhaps this is a good time to attach a mathematical name to what we have just written. When two quantities are stated as being equal in this way, mathematicians call the whole statement an equation. So, from now on, we can occasionally refer to Fernanda’s calculation as her price-prediction equation. Nothing has changed in what it does. We have simply learned its mathematical name.

If you look at the above equation, you may have noticed that it does not give Fernanda just one predicted price. She can use it for any number of bedrooms. For example:

  • 1 bedroom gives a predicted price of 15 units:
    • Predicted Price = 10 + (5 × 1) = 10 + 5 = 15
  • 2 bedrooms gives a predicted price of 20 units:
    • Predicted Price = 10 + (5 × 2) = 10 + 10 = 20
  • 3 bedrooms gives a predicted price of 25 units:
    • Predicted Price = 10 + (5 × 3) = 10 + 15 = 25
  • 4 bedrooms gives a predicted price of 30 units:
    • Predicted Price = 10 + (5 × 4) = 10 + 20 = 30

Fernanda can now place these predicted prices on the same graph as the historical sale prices.

This is useful, but we need to be clear about what we are looking at. The historical points and Fernanda’s predictions are not the same thing. The historical points show what the properties actually sold for. Fernanda’s calculation shows what she would predict for a given number of bedrooms.

They can nevertheless appear on the same graph because both use the same price scale on the vertical axis.

And something rather convenient happens when Fernanda plots the prices produced by her equation: They all fall along a straight line.

If we join those predicted points, we therefore get a line showing the prices produced by Fernanda’s equation. For any bedroom count shown on the horizontal axis, the height of the line tells us the corresponding predicted price.

Now let’s put both the historical sale prices and Fernanda’s prediction line on the graph (see Figure 2 below).

Figure 2: Three Historical Examples and Prediction Line

The distinction is now important (see Figure 2 above):

  • The historical points show the prices at which Properties A, B and C actually sold.
  • The prediction line shows the prices produced by Fernanda’s equation.

Where a historical point sits on the prediction line, Fernanda’s predicted price and the actual sale price are the same. Where it does not, her prediction differs from what actually happened.

Now look again at the number 5 in Fernanda’s equation:

Predicted Price = 10 + (5 × Number of Bedrooms)

Each time the number of bedrooms increases by one, the predicted price increases by 5 units, or $50,000. So, the number 5 controls how strongly the number of bedrooms affects the predicted price.

Next consider the number 10 in the same equation.

This is the amount from which Fernanda’s calculation starts before any bedroom contribution is added. That is why we have been calling it the Starting Amount.

We can also see this on the graph (Figure 2 above): If the Number of Bedrooms were zero, Fernanda’s calculation would give:

Predicted Price = 10 + (5 × 0) = 10

That gives us the distinctive point at 0 bedrooms and 10 price units shown on the graph.

A small warning is useful here: This does not mean Fernanda has discovered that a genuine zero-bedroom property must be worth $100,000. Zero bedrooms lies outside the little range of historical properties we are examining. The value of 10 is a setting in Fernanda’s prediction calculation (her equation). Sometimes such a starting value has a useful real-world interpretation. Then again, sometimes its main role is simply to position the prediction line appropriately.

We now understand what these parts of the calculation are doing. So, as we did in the previous article, we can attach their usual technical names:

What We Have Just UsedEquivalent Machine Learning Term
Prices produced by Fernanda’s calculationPredictions
The amount added for each additional bedroom
(for example, 5)
Slope
(also called a Coefficient. In later Machine Learning articles, we will encounter the related term Weight)
The Starting Amount
(for example, 10)
Intercept
(in later Machine Learning articles, we will also encounter the term Bias)
Fernanda’s complete price calculation (her equation)A Linear Model
Table 4: Some Useful Machine Learning Terminologies

The important point to note is that none of these names introduces a new mechanism. We have already seen what each part does. The new terminology simply gives us shorter names for ideas we now understand.

5. Two houses cooperate. The third one refuses!

Table 5 below shows Fernanda’s line prediction results so far:

PropertyBedroomsPredicted Price UnitsActual Price UnitsDifference: Actual − Predicted
A220200
B325250
C43028-2
Table 5: Fernanda’s First Prediction for the Three Standard-Location Sales

Looking at the “Difference: Actual – Predicted” column in Table 5 above, it appears that properties A and B sit exactly on the prediction line (no error so, the line predicted the price perfectly). However, property C does not.

For C, the model predicts 30 units, or $300,000, but the house actually sold for 28 units, or $280,000. This creates the following error:

Prediction Error = Actual Price − Predicted Price = 28 − 30 = −2

The negative sign tells us that the prediction was too high by 2 units. The size of the miss is $20,000.

On the graph (see Figure 3 below), we can literally see that mistake as the vertical gap between the observed point and Fernanda’s prediction line.

Figure 3: The Prediction Error Appears as a Vertical Gap

This picture is worth remembering. Later, when the mathematics becomes more compact, the basic idea will remain the same:

  • The model predicts a value,
  • Reality supplies the known historical value, and
  • The difference tells us how far away the prediction was.

6. Which line is better?

What do you think Fernanda should do next to cater for the annoying property C that does not play ball?

Perhaps she could now move the line around. How? Well, she could change the Starting Amount. Or she could change the amount added per bedroom. Each choice would give different predictions and therefore a different line.

In any case, “I rather like the look of this line” is not a particularly convincing learning algorithm, so Fernanda needs something more robust.

In other words, she needs a consistent way to compare one possible line with another.

We already know how to measure the error for an individual property:

Prediction Error = Actual Price − Predicted Price

But Fernanda has several historical properties. So how can she combine all those individual errors into one overall measure of how well a particular line performs?

A simple first idea might be to add the errors together.

Unfortunately, that creates a problem: Positive and negative errors can cancel each other out.

Imagine one house is underestimated by $100,000 and another is overestimated by $100,000. Adding the signed errors gives zero. Declaring the model perfect at this point would be the sort of achievement best kept away from the agency’s manager!

A common solution (frequently employed by clever mathematicians) is to square each error before adding them. Squaring does two useful things for us here:

  • Negative and positive errors both become positive contributions to the total, and
  • Larger mistakes are penalized more strongly than smaller ones.

For Fernanda’s first line, the three errors are 0, 0 and −2. This means the total of squared errors becomes:

Total Squared Error = 0 ² + 0 ² + (−2) ² = 4

In case you were wondering, error is not a good thing, so lower is better. And since we are adding squared errors, the total cannot be negative. A total of zero, however, would mean every historical point lies exactly on the line.

7. Let another line compete

Suppose Fernanda tries another simple calculation:

Predicted Price = 12 + 4 × Number of Bedrooms

Now the three predictions are 20, 24 and 28.

PropertyActualFirst Line PredictionSecond Line Prediction
A202020
B252524
C283028
Table 6: Two Possible Straight-Line Calculations

The first line has squared errors 0, 0 and 4, giving a total of 4.

The second line has errors 0, 1 and 0, so its total squared error is:

Total Squared Error = 0 ² + 1 ² + 0 ² = 1

By this measure, the second line is better for these three historical examples.

Notice what has changed compared with Article 1: There we compared three complete YES/NO rules. Here we are changing numerical settings inside the same kind of calculation.

8. So where should the line go?

Fernanda now has something useful. She can propose a line, calculate its predictions, measure the individual errors and combine those errors into a single Total Squared Error.

So, in principle, she could try another line. And another. And another. But I suspect even Fernanda has better things to do with her afternoon. What she really wants is a systematic way to answer this question:

Which Starting Amount and amount added per bedroom produce the smallest Total Squared Error?

What Fernanda is trying to do here is make the model’s predictions match the historical examples as well as possible, according to the measure she has chosen.

This process is commonly called fitting the model to the data. So, when we refer to the best-fitting line, we simply mean the line whose settings give the best result according to our chosen measure. In this case, the smallest Total Squared Error.

And what we mean by a “systematic way” is this: Rather than Fernanda choosing the settings herself and then checking how well they work, could a mathematical procedure use the historical examples to determine the best settings for her?

The answer is yes.

For our three Standard-location properties, that procedure produces approximately:

Starting Amount = 12.33

Amount added per bedroom = 4

giving Fernanda the following new price-prediction calculation:

Predicted Price = 12.33 + (4 × Number of Bedrooms)

Using this new equation, Fernanda can now obtain a new set of predictions for her 3 Standard-location properties. The predictions are approximately 20.33, 24.33 and 28.33 respectively:

  • Prediction for 2 bedrooms:
    • 12.33 + (4 * 2) = 20.33

Prediction for 3 bedrooms:

  • Prediction for 4 bedrooms:
    • 12.33 + (4 * 4) = 28.33

None of the three values is exact, but the total squared error is only about 0.67:

  • Prediction error for 2 bedrooms:
    • 20 – 20.33 = -0.33
  • Prediction error for 3 bedrooms:
    • 25 – 24.33 = +0.67
  • Prediction error for 4 bedrooms:
    • 28 – 28.33 = -0.33
  • Total Squared Error = (-0.33) 2 + (+0.67) 2 + (-0.33) 2 = 0.1089 + 0.4489 + 0.1089 = 0.6667 (approximately = 0.67)

So, compared with Fernanda’s earlier lines, this one produces a much smaller Total Squared Error.

There is another interesting difference, though. None of the three historical properties now sits exactly on the prediction line. Instead, the line passes between them, leaving a small prediction error for each property.

We can see this more clearly in Figure 4 below. The historical sale prices remain as individual points, while the new prediction line is determined by:

Predicted Price = 12.33 + (4 × Number of Bedrooms)

The small vertical gaps between the historical points and the line show the remaining prediction errors.

This is an important point:

The best-fitting line does not have to pass through every historical point.

Its job is to find the straight line that performs best across the examples as a whole, according to the fitting measure we have chosen. In our case, the measure is the Total Squared Error.

Figure 4: A Best-Fitting Line Balances the Errors Across the Examples

We are not yet going to derive the mathematics that produces the values 12.33 and 4. That is not because the mathematics is forbidden territory. Quite the opposite. Our immediate objective is to understand what the procedure is trying to achieve before worrying about how it finds those values.

For now, the arithmetic above is enough to verify that these settings give us a substantially smaller Total Squared Error than the alternatives we tried earlier.

And now we can finally attach a name to what we have just done.

We started with historical examples, used their prediction errors to judge different settings, and found the settings that make our linear model fit those examples as well as possible according to our chosen criterion.

This process of fitting a linear model to numerical data is called Linear Regression.

9. Location has not mysteriously disappeared

So far, Fernanda has deliberately held location fixed by looking only at Standard properties. That made the first graph easy to understand.

But our real task uses both Bedrooms and Location. We therefore need a way to include location in the same calculation. But how best can she do this?

Fernanda notes that Bedrooms are already numerical, so she can multiply the number of bedrooms by a chosen amount and add that contribution to the Starting Amount. However, location is different: Standard and Prime are words, not numbers.

If Fernanda wants to include Location in the same kind of calculation, she first needs a numerical way to represent those two possibilities.

One simple choice is as shown in Table 7 below:

LocationLocation Code
Standard0
Prime1
Table 7: Turning the Two Location Categories into a Numerical Input

This does not mean Standard is “worth zero” and Prime is “worth one”. The numbers are being used as a form of switch, to easily differentiate between the two.

If the Location Code is 0, the extra Prime-location contribution is switched off (meaning the location is Standard).

If the Location Code is 1, that contribution is included (meaning the location is Prime).

Armed with this new technique (thanks to mathematicians again), Fernanda now proposes a three-part equation:

Predicted Price = Starting Amount + (Bedroom Contribution × Bedrooms) + (Location Contribution × Location Code)

Note that in Machine Learning, these adjustable contributions are often controlled by numbers called weights (or coefficients in Linear Regression). So, later, we may refer to the Bedroom Contribution and Location Contribution as the Bedroom Weight and Location Weight.

In the above equation:

  • The Bedroom Contribution controls how much the predicted price changes when Bedrooms increases by one, while Location is kept fixed,
  • The Location Contribution controls how much the predicted price changes when we move from Standard to Prime, while Bedrooms is kept fixed.

10. Fernanda’s first complete two-input model

Let us begin with settings that are easy to understand and easy to calculate:

  • Starting Amount = 10
  • Bedroom Weight = 5
  • Location Weight = 10

So, assuming Fernanda chooses these values, her first full calculation will be:

Predicted Price = 10 + (5 × Bedrooms) + (10 × Location Code)

For a two-bedroom Prime property, the calculation generates:

Predicted Price = 10 + (5 × 2) + (10 × 1) = 30

That means the predicted price is estimated as $300,000.

Similarly, for a four-bedroom Standard property we have:

Predicted Price = 10 + (5 × 4) + (10 × 0) = 30

Again, resulting in $300,000. Here, the Prime contribution disappeared because its Location Code is zero.

11. Let us test those settings against all six houses

Applying the new equation to the 6 historical cases, we get a new set of predictions to compare against the actual sale values (see Table 8 below):

PropertyBedroomsLocation CodeActualPredictedError
A2020200
B3025250
C402830-2
D2130300
E3140355
F4145405
Table 8: Predictions from Fernanda’s First Complete Model

This model is not disastrous. It even gets three of the six prices exactly right in our artificial little world.

But exact hits are not the right way to judge a continuous price prediction. The three misses have different sizes, and that information matters.

Using the squared-error approach, the score is:

Total Squared Error = 0 ² + 0 ² + (−2) ² + 0 ² + 5 ² + 5 ² = 54

That gives us one number with which to compare another set of settings.

12. Learning now means changing numerical settings

Suppose the learning procedure keeps the same form of calculation but changes the numbers inside it.

For example, if it increases the Location Weight from 10 to 13 while leaving the other two settings unchanged, we get:

Predicted Price = 10 + (5 × Bedrooms) + (13 × Location Code)

The new predictions are 20, 25, 30, 33, 38 and 43.

If you get some time, try and reproduce Table 8 entries with the new settings. This will be a short but good exercise to complete and will help you gain a little more confidence in this type of activity.

Using the new set of predictions, the corresponding errors are 0, 0, −2, −3, 2 and 2, resulting in:

Total Squared Error = 0 ² + 0 ² + (−2) ² + (-3) ² + 2 ² + 2 ² = 21

The new Total Squared Error is clearly a substantial improvement from 54.

This is the central learning idea in this article:

Keep the general prediction structure, use historical examples to assess its performance, and change the numerical settings to improve the fit.

Of course, a real Linear Regression procedure does not normally sit there trying random numbers with Fernanda cheering whenever the score falls. There are systematic mathematical ways to find the settings that minimize the chosen error measure.

We will go deeper into how learning algorithms decide which settings to change, and by how much, later in this series. For now, it is enough to see what is being adjusted and what makes one set of settings better than another.

13. What settings does Linear Regression choose for all six houses?

In the previous section, Fernanda improved the model by changing just one setting: The Location Contribution. That reduced the Total Squared Error from 54 to 21.

But our complete two-input model has three adjustable settings:

  • the Starting Amount,
  • the Bedroom Contribution, and
  • the Location Contribution.

There is no particular reason why Fernanda should change only one of them while leaving the other two untouched.

So, instead of adjusting the settings manually, we can now use the same fitting process we introduced earlier and ask a broader question:

Which combination of all three settings gives the smallest Total Squared Error across all six historical properties?

Using Bedrooms and Location Code as the two inputs, Linear Regression gives approximately:

  • Starting Amount = 7.08
  • Bedroom Weight = 5.75
  • Location Weight = 14.00

Note: Later in this series, we will see one important way a learning procedure can work out how numerical settings should change. For now, you will have to take my word for these three values. Fortunately, my word is gold😊!

Using the above settings, Fernanda’s fitted two-input model therefore becomes:

Predicted Price = 7.08 + (5.75 × Bedrooms) + (14 × Location Code)

Let us now see what this model predicts for all six historical properties (see Table 9 below):

PropertyActual Price UnitsPredicted Price UnitsApprox. Predicted Dollars
A2018.58$185,800
B2524.33$243,300
C2830.08$300,800
D3032.58$325,800
E4038.33$383,300
F4544.08$440,800
Table 9: Predictions from Fernanda’s Fitted Two-Input Linear Model

The Total Squared Error is now approximately 17.08:

Total Squared Error = (20 – 18.58) 2 + (25 – 24.33) 2 + (28 – 30.08) 2 + (30 – 32.58) 2 + (40 – 38.33) 2 + (45 – 44.08) 2 = 17.0834 (approximately 17.08)

So, the progression has been:

  • 54 with Fernanda’s first complete set of settings,
  • 21 after manually improving one setting,
  • 17.08 after Linear Regression adjusts all three settings together.

That is the important point: Linear Regression is not trying to make one or two houses come out perfectly. It is trying to find the combination of settings that performs best across all six historical properties together, according to the measure we chose: Total Squared Error.

And please do not be disturbed by predictions such as $243,300 (property B). A mathematical model is perfectly happy to produce a number that an estate agent might consider slightly awkward for a glossy brochure!

Also notice that the fitted model does not reproduce every historical sale price exactly. That is not a failure. It is balancing the errors across all six examples while staying within the simple linear model we gave it.

One final point is worth stressing here: Linear Regression did not decide that Bedrooms and Location were the right inputs to use. We supplied those inputs. It also did not invent the structure of Fernanda’s equation. We supplied that too.

What it determined from the historical examples were the numerical settings inside that model:

7.08, 5.75 and 14.00.

That is the learning taking place here.

14. What exactly has been learned?

This is worth being precise about because the word “learned” is easily asked to do too much work.

In our example, people decided:

  • The question: predict sale price,
  • The inputs available to the model: Bedrooms and Location,
  • How Location would be represented numerically: 0, 1 instead of Standard and Prime,
  • The general form of the calculation: Starting Amount plus a contribution from each input,
  • Which historical examples would be used: Our 6 examples,
  • That squared prediction errors would be used to judge the fit: Total Squared Error.

The learning procedure used those examples to determine the numerical settings that best satisfy that criterion. It did not independently discover that Bedrooms and Location are the only important facts about a house. We gave it those inputs.

It did not understand that Prime locations contain better cafés, nicer parks or more persuasive estate-agent adjectives!

And it did not reason like Fernanda.

The human story helps us understand the mechanism. The algorithm itself manipulates numerical inputs and numerical settings according to a specified procedure.

15. Time to attach the technical names

We have now used most of the important ingredients of a simple Linear Regression model. Let us attach their usual names (see Table 10 below):

What We Have Been UsingUsual Technical Name
Bedrooms and Location CodeInputs or Features
The known Actual Sale Price used during fittingTarget or known Output
Actual Price − Predicted PricePrediction Error (often called a Residual in regression)
Adding the squared prediction errors across the historical examplesTotal Squared Error (our chosen fitting measure)
The numerical values determined from the historical examples, such as 7.08, 5.75 and 14Fitted Settings or Model Parameters
Fernanda’s complete equation after those settings have been determinedFitted Linear Model
Table 10: Names for Concepts We Have Already Used

At this point, the name Linear Regression should hopefully feel a little less mysterious. We can now see where both parts of the name come from:

  • Regression: In our example, we are predicting a numerical value (the sale price), rather than a YES/NO category,
  • Linear: Our predicted price is produced by starting with an intercept (Starting Amount) and then adding a contribution from each input. Each contribution is calculated by multiplying the input by its coefficient (or weight).

With only Bedrooms as an input, this relationship appeared as the straight line we could see on our graph. With Bedrooms and Location together, the same basic idea continues even though it is no longer as convenient to show on a simple two-dimensional graph.

16. How mathematicians write the same calculation

So far, we have deliberately used proper names in our equations. They make it easier to see what each quantity means.

However, mathematicians and Machine Learning texts usually compress the same idea into symbols. For example, our two-input calculation may be written as:

Predicted Price = Bias + Bedroom Weight × Bedrooms + Location Weight × Location

and then shortened to something like:

ŷ = b + w₁x₁ + w₂x₂

where:

  • ŷ means the predicted output,
  • b is the bias or intercept,
  • w₁ is the weight attached to the first input,
  • x₁ is the first input value,
  • w2 is the weight attached to the second input,
  • x2 is the second input value.

Nothing new has happened mathematically. The notation is simply shorter.

This is a useful discipline throughout the series: Understand the mechanism first, then let notation save space rather than create mystery.

17. But where did our nice straight line go?

With one numerical input, such as Bedrooms alone, the model can be shown as an ordinary straight line on a two-dimensional graph.

With two numerical inputs, the geometry extends to a flat surface rather than a single line on a page. With still more inputs, the same linear idea continues into dimensions that are less convenient to draw and rather less useful for decorating a blog post (I like my readers but there is a limit to how far I am willing to go to satisfy their curiosity).

The important point is that the model remains linear in its input contributions:

Start with the bias, multiply each input by its weight, and add the contributions.

So, the graph was not a childish simplification that we now throw away. It showed the one-input version of the same mechanism.

18. A new house arrives

Life is never easy though.

Suppose Fernanda receives another three-bedroom property in a Prime location.

Using the fitted settings she gets:

Predicted Price = 7.08 + 5.75 × 3 + 14 × 1

Predicted Price = 38.33 price units ≈ $383,300

Notice something slightly uncomfortable but important. Property E in our historical data also had three bedrooms and a Prime location. Our model therefore gives the new property exactly the same prediction.

That is not because the two houses are identical. It is because, as far as this model is concerned, they are identical: After all, Bedrooms and Location are the only inputs it receives.

If the new house has a leaking roof, a spectacular terrace or a nightclub masquerading as “local vibrancy”, the model knows none of that unless we supply additional information.

19. Does the Bedroom Weight tell us what a bedroom is worth?

Hmm. Not quite.

In this fitted model, the Bedroom Weight is 5.75 price units. It is tempting to translate that directly as “an extra bedroom is worth $57,500”.

A more careful statement would be:

Holding the Location Code fixed, this model increases its predicted price by $57,500 when the bedroom count increases by one.

That is a statement about the model’s prediction, not automatically a causal fact about the real world. For example, perhaps larger homes tend to have more bedrooms. Or perhaps certain neighborhoods have both larger houses and higher prices. Or perhaps our six examples are simply too small to support grand declarations about property economics.

Linear Regression can reveal predictive relationships. Establishing that changing one feature would cause the outcome to change by a particular amount is a different question, which we will tackle later in this series.

20. Is Linear Regression too simple to matter?

Nope.

A simple method can be useful because it is understandable, quick to compute and easy to inspect. In some problems a linear relationship is genuinely a good approximation. In others it provides a useful baseline against which more complicated methods can be judged.

Its limitations, however, are equally important:

  • The model can use only the inputs we provide,
  • It assumes the input contributions combine in the specified linear way,
  • Unusual cases may be badly predicted,
  • A good fit to historical examples does not guarantee good predictions on new properties,
  • The fitted weights describe the model and data. They should not automatically be treated as causal effects.

In other words, Linear Regression is neither magic nor merely a classroom toy. It is a precise tool with assumptions, strengths and limits. Use it wisely!

21. Question time

Before moving on, consider the following:

  1. In our full model, what are the inputs and what is the output?
  2. If the Bedroom Weight increases while everything else stays fixed, what happens to the predicted price of a four-bedroom property?
  3. Why is the Starting Amount also called the intercept?
  4. Why can “simply adding signed prediction errors” be misleading?
  5. Did the learning procedure discover that Location should be an input, or did people make that choice?
  6. If two properties have the same Bedrooms and Location Code but differ greatly in condition, what will this model do?

22. What have we learned?

Fernanda began with a new kind of question. Instead of predicting a YES/NO category, she needed to predict an actual price.

We first isolated one input and drew historical examples on a graph. That let us see how a straight-line prediction combines:

  • A Starting Amount, which appears as the intercept, and
  • An input contribution controlled by a weight.

We then saw that historical points do not generally sit perfectly on one line. The vertical gaps between actual values and predictions are prediction errors.

To compare possible lines and settings, we squared those errors and added them. This gave us a single number to minimize.

We then brought Location back into the model, represented it numerically, and saw that learning can mean changing several numerical settings rather than choosing among a few complete rules.

Most importantly, we learned that:

  • The prediction combines a bias/intercept with a contribution from each input,
  • The weights and bias are adjustable settings,
  • Historical examples allow a learning procedure to choose settings that reduce prediction error,
  • The best-fitting model need not reproduce every historical example exactly,
  • The human analogy explains the mechanism and illustrates the kind of numerical calculations the algorithm performs,
  • A fitted weight describes the model’s predictive relationship and is not automatically a causal statement.

We have therefore moved one step beyond Article 1.

There, learning selected which complete rule to use. Here, learning selected numerical settings inside a prediction calculation.

23. Next: What if Fernanda only needs YES or NO again?

What? “You must be joking”, I hear you say!

Sadly, I am not. After all this effort to predict an actual price, we are going back to a familiar question:

Is this property expected to sell for at least $300,000: YES or NO?

And you would be perfectly entitled to ask two questions.

First: Didn’t we already do this in Article 1?

Yes. But there we approached the problem by giving the computer several complete YES/NO rules and asking it to choose the one that worked best.

Second: Couldn’t we simply use the price prediction from this article and check whether it is at least $300,000?

Absolutely. And for our little property example, that would be perfectly reasonable. We do not actually need another mechanism just to answer this particular question.

However, many real problems ask directly for a decision rather than for a numerical quantity first, including YES or NO, approve or reject, fraud or legitimate, and so on. In such cases, it can be useful to build the decision directly from the available inputs.

That is what we are going to explore next. The inputs will contribute to a numerical score, and we will ask whether that score crosses a cutoff.

There is another reason for taking this apparent detour: This simple mechanism turns out to be an important stepping stone toward something much more powerful: The artificial neuron and, eventually, Neural Networks.

So, we are not returning to YES/NO because Article 1 failed. We are returning to it because we are now ready to understand a different mechanism for producing that decision.

24. Question time: Suggested answers

Here are some answers to the questions posed in section 21 above:

  1. Bedrooms and Location Code are the inputs. The output is Predicted Sale Price.
  2. The prediction rises. The increase equals the change in Bedroom Weight multiplied by four, because that property has four bedrooms.
  3. With one input, the Starting Amount is the value where the prediction line crosses the vertical axis, which occurs when the input value is zero.
  4. Positive and negative errors can cancel. A large overprediction and a large underprediction can therefore misleadingly sum to zero.
  5. People chose to provide Location as an input. The learning procedure adjusted the numerical setting attached to it.
  6. It gives them the same prediction because, from the model’s point of view, the supplied inputs are identical. Condition is invisible to this model.

Feedback please 😊

NEXT ARTICLE
Title: Can a Computer Make a Decision Without Understanding Anything?
Concept: Understanding the Threshold Logic Unit (TLU)

← Back to the AI Explained Roadmap

The Computer Followed the Rule. Why Was It Wrong Half the Time?

Understanding Rules and Machine Learning

Fernanda is an estate agent (a girl’s gotta do what a girl’s gotta do!). She knows her neighborhood, enjoys helping people find homes and has developed an admirable patience with common phrases such as “deceptively spacious” and “easily-maintained garden”.

Like most experienced professionals, Fernanda uses a mixture of direct instructions she has to follows, and lessons from her previous work. More specifically, some of Fernanda’s decisions follow instructions supplied by her agency, while others draw on patterns she has noticed through experience (for example, which property details tend to be associated with higher sale prices).

Now suppose Fernanda wants a computer to help her make an initial assessment of new properties. How should she teach it what to do?

She could give it an explicit instruction, such as “Follow this rule when assessing a property”.

Alternatively, she could give it records of previous sales and a procedure for finding a useful pattern. In this case her instruction is less explicit: “Use these examples to work out how to assess another property”.

That brings us to the question at the heart of this article:

Before answering, let us give Fernanda a specific problem:

Fernanda’s agency wants to organize incoming properties into two categories. Why? Well, I am not an estate agent but I guess this may help match properties to buyers’ budgets (maybe Fernanda is a bit posh and prefers to only work on the houses that rich clients can afford?). Anyway, let’s call the 2 possible categories YES and NO (see Table 1 below):

Category Description
YES Includes houses which are expected to sell for at least $300,000
NO Includes houses which are expected to sell for less than $300,000

Table 1: Categorizing Houses Based on Price

Note that at this stage, the agency does not need an exact price estimate. It simply wants an initial category to help organize its work.

Now, imagine that for our example, Fernanda receives only two details about each property (see Table 2 below):

Available Information Possible Values in Our Example
Number of Bedrooms 2, 3, 4
Location Standard or Prime

Table 2: Property Details Available to Fernanda

Her required answer, for each property under consideration, is simply YES (it is at least $300,000) or NO (It is less than $300,000).

Of course, actual property prices depend on many other things, such as floor area, condition, transport links, whether the “peaceful outlook” overlooks a nightclub, etc., but for our purpose here, considering only 2 features are good enough. In other words, we are deliberately simplifying the situation so that we can see what happens at every step of the decision-making process. We can make the world more complicated later. Just wait and we get there!

“What is your objective?”, I hear you yelling at me. Here it is:

Suppose the agency manager (good chap but not important enough to have a specific name for now!) gives Fernanda this instruction:

Property Price Rule:

If a property has at least three bedrooms, predict YES. Otherwise, predict NO.

The reasoning sounds plausible. Larger homes often sell for more, and bedroom count seems a convenient starting point. Beside plausibility, this rule is rather simple. It will not take much to remember and apply it to new houses so, it certainly sounds useful.

With this rule in place, Fernanda can now apply the instruction immediately (see Table 3 below):

Number of Bedrooms Location Apply the Rule Prediction
2 Prime Fewer than three bedrooms NO
3 Standard At least three bedrooms YES
4 Prime At least three bedrooms YES

Table 3: Applying a Property Price Rule to Selected Properties

Notice from the results in Table 3 above that location makes no difference under this rule. The information about location is available, but the instruction does not use it at all.

A computer could also follow exactly the same procedure:

  1. Receive the bedroom count,
  2. Check whether it is at least three,
  3. Return YES if it is, and NO otherwise.

This is a rule-based approach. A person has supplied the decision rule, and the computer applies it.

There is nothing inherently wrong with that. The instruction is certainly clear, easy to inspect and inexpensive to apply. The question (an important one) is whether it gives useful answers.

The manager’s confidence, although impressive, does not answer that question (managers rarely gives their charges confidence so, in some sense, this is not surprising😊).

Fernanda being who she is (meticulous and not blindly trusting her manager), collects six completed sales. Because these properties have already sold, she can compare a prediction with what actually happened (see Table 4 below).

Property Number of
Bedrooms
Location Actual
Sales Price
Correct
Category
Prediction:
Using “At Least
3-Bedroom” Rule
A 2 Standard $200,000 NO NO (correct)
B 3 Standard $250,000 NO YES (incorrect)
C 4 Standard $280,000 NO YES (incorrect)
D 2 Prime $300,000 YES NO (incorrect)
E 3 Prime $400,000 YES YES (correct)
F 4 Prime $450,000 YES YES (correct)

Table 4: First Round of Prediction

As you can see from Table 4 above, the rule gets three out of six cases right.

That is to say that the manager’s particular rule (based on the rule-based approach) is 50% correct as seen below:

No. of Correct Predictions ÷ Total Number of Cases = 3 ÷ 6 = 0.5, or 50% correct

For now, counting mistakes is enough. We will examine more detailed ways to assess predictions later in the series.

Look closely at the three errors:

  • The three-bedroom Standard property (Property B) sold for less than $300,000.
  • So did the four-bedroom Standard property (Property C).
  • The two-bedroom Prime property (Property D) reached $300,000 despite having fewer bedrooms.

Fernanda begins to suspect that bedroom count alone is not a particularly useful basis for this decision.

Note something important:

The rule is being followed perfectly. It is the rule’s predictive usefulness that is in question.

That distinction matters. A computer can execute an instruction flawlessly and still produce a poor answer.

Now what?

As we just observed, the manager’s rule has made three mistakes across six properties. What should Fernanda do next?

She could inspect the historical sales herself, decide that location looks more useful and write a replacement rule. That would be Fernanda learning from experience. A computer given her replacement rule would simply follow this new instruction.

But we want to explore a different possibility and ask:

To see how this might work, let’s start with something small enough that we can follow every step.

Let’s assume that Fernanda has three suggestions:

  • Perhaps the manager’s original bedroom rule is still worth considering, or
  • Perhaps requiring at least four bedrooms would produce better results, or
  • Perhaps location is more useful than bedroom count.

Each suggestion proposes a possible relationship between information about a property and its price category. We can express those suggestions as three alternative prediction rules (see Table 5 below):

Candidate Rule Rule Description
Rule 1 Predict YES if the property has at least three bedrooms, otherwise predict NO
Rule 2 Predict YES if the property has at least four bedrooms, otherwise, predict NO
Rule 3 Predict YES if the property is in a Prime location, otherwise, predict NO

Table 5: Enhanced Property Price Rules

Note that these rules are alternatives. The computer will compare them and select one. It will not apply all three in sequence to reach a single answer.

Now we give the computer the following procedure:

  1. Apply each candidate rule to all six historical properties,
  2. Compare each prediction with the correct category, obtained from the actual sale price,
  3. Count how many mistakes each candidate makes,
  4. Select the candidate with the fewest mistakes. If candidates tie for the fewest mistakes, select the one listed first (the tie instruction simply makes our procedure complete. It does not mean that an earlier position in the table makes a rule more trustworthy. This, also demonstrates that sometime there are more than one way to get to a reasonable answer😊).

But hang on. Aren’t we still using rules?

Yes. But the important change is how the prediction rule is chosen.

Previously, the manager chose a rule and instructed the computer to use it (no fuss, no muss). Its performance on the historical examples played no part in that choice.

Now, we supply several candidates and a method for comparing them. The computer uses the historical examples to determine which candidate to select. With different examples, a different candidate could win (ain’t life wonderful!).

There are therefore two separate activities to consider this time:

  • Learning: Using historical examples with known answers to select a prediction rule.
  • Predicting: Applying the selected rule to another property whose sale price is not yet known.

The selected rule is what we call the learned model in this example (it is “learned” because it has used actual examples to “learn” what works best and it is “model” because it aims to model or capture or predict what happens with new or unseen examples).

We should also be clear about the limits of what the computer is doing. We supplied the three candidates. The computer is testing the possible relationships we suggested. So, it is not inventing new ones or discovering other property characteristics for itself.

This is a deliberately limited (stylized) example of Machine Learning. Later, we will explore methods that adjust numerical settings rather than select from a short list of complete rules. For now, this small example lets us see exactly what is supplied by people and what is determined from the data (existing examples).

Learning from examples can therefore produce a rule. There is no contradiction: The rule is the result of the learning procedure.

Let’s now run that procedure and determine which of our three candidates makes the fewest mistakes on the six historical sales.

We already know that Rule 1 makes three mistakes. What happens with the other two? Table 6 below summarizes the results for all 3 rules:

Property Number of
Bedrooms
Location Actual
Sales Price
Correct
Category
Rule 1:
At Least 3
Bedrooms
Rule 2:
At Least 4
Bedrooms
Rule 3:
Prime
Location
A 2 Standard $200,000 NO NO NO NO
B 3 Standard $250,000 NO YES NO NO
C 4 Standard $280,000 NO YES YES NO
D 2 Prime $300,000 YES NO NO YES
E 3 Prime $400,000 YES YES NO YES
F 4 Prime $450,000 YES YES YES YES
Number of Mistakes (out of 6) 3 3 0

Table 6: Second Round of Predictions

It is clear now: Rule 3 wins😊.

So, the selected rule is:

Note that the computer did not select this rule because it understands the appeal of a neighborhood café! It selected it because, among the permitted candidates, this rule made the fewest mistakes on the supplied examples.

And this is the important transformation:

  • We began with examples of properties and their known outcomes,
  • We applied a procedure for comparing possible rules,
  • We ended with a selected rule that can be used on another (new) property.

CRITICAL POINT TO STRESS: If the historical outcomes had been different, another candidate could have won. That dependence on the examples is what makes this a learning process.

We have already encountered several ideas that appear throughout Machine Learning. Now we can attach their usual names (see Table 7 below).

What We Have
Just Used
Equivalent Machine
Learning Term
Bedrooms and location Features:
The input details available for making a prediction
The known YES or NO category for each historical property Label (or Target):
The answer used to assess a prediction
The six historical examples used to choose a rule Training Data
The procedure that compares candidates and selects one Learning Algorithm
The selected rule Trained Model
Applying that selected rule to another property Prediction (or Inference)

Table 7: Some Useful Machine Learning Terminologies

Let us pause on the difference between the learning algorithm and the learned model as this is a common point of confusion among many students:

  • A learning algorithm is the procedure we used to select a rule.
    • Example: Try the three candidates and select the one with the fewest mistakes.
  • A learned model is the rule we obtained:
    • Example: Predict YES for Prime locations and NO for Standard locations.

These two concepts are related, but they are not the same thing.

Also notice that the actual sale price helped us establish the correct historical category. It is not an input to our prediction. This is an important distinction to make because for a new property, we do not yet know its eventual sale price (there is no actual price yet). That is what we want to predict (output, rather than input to our model). If we supplied that price to predict its price category, our apparent brilliance would be rather less impressive than advertised😊.

OK. Here is another Machine Learning definition that I could have (maybe prematurely) added to Table 7 above:

The “supervision” consists of those supplied answers. It does not require Fernanda to stand beside a laptop saying, “Come on, you can do better” (knowing her, she may actually do that but that is not the standard practice in Machine Learning!).

Yes. Absolutely. We did not remove human choices from the process. In particular, people decided:

  • What question to answer,
  • Which property details to provide,
  • Which historical examples to use,
  • Which candidate rules to consider,
  • How to compare their performance.

Our learning procedure searches only among the possibilities we allow. In this example, it cannot suddenly invent a sophisticated calculation involving floor area, interest rates and the emotional value of a south-facing balcony. After all, we did not provide those inputs or permit that kind of calculation.

The difference is which part people specify directly (see Table 8 below).

Approach What People Specify What the Computer Does
Supplied decision rule The rule that produces YES or NO Applies the rule
Learning from examples The candidate rules, examples and selection procedure Uses the examples to select a rule, then applies it

Table 8: Some Useful Machine Learning

Machine Learning is still implemented through programmed procedures. The defining feature here is that the resulting predictive behavior is determined through the examples.

We will come back to this during this 24-topic series we are embarking on but for now, let’s just note that in more complex methods, the procedure may adjust thousands or millions of numerical settings rather than choose among three short rules. We will reach those methods gradually. So, let’s hold our horses and simply appreciate the fact that the essential idea is already visible through this simple example, thanks to Fernanda with a little help from her, as yet, nameless boss!

Yes, my dear and this is worth making explicit:

If we inspect Fernanda’s final model, it looks like an ordinary instruction:

If Prime, predict YES. Otherwise, predict NO.

Someone could have written exactly that instruction without running a learning procedure (I mean someone really clever). But the final wording of the rule alone would not tell us how it was obtained.

The distinction lies in its origin: Was it supplied directly, or selected through a learning procedure using examples?

That is why “rules versus learning” is a useful opening question, but not a complete division of the world. Learning can produce rules.

In fact, some methods produce rules that are easy to read. Others produce numerical relationships that are much harder to inspect. Both can be learned from examples.

Hmmm. Err! It means that the selected rule made zero mistakes on the six examples used (training data) to select it. That is certainly encouraging. And yet, it is also a very specific statement. I mean, it is specific to these 6 samples only. What would happen if we can extend the training data from 6 to, say, 8?

Let’s for example, assume that Fernanda now checks two additional historical sales that she deliberately kept out of the selection process (if I forgot to tell you before, let’s settle this now: Fernanda is quite clever. Can you see why keeping some historical examples out of the training data for further verification after a rule is selected is a particularly useful idea?). Now let’s see the 2 new examples left out of the original list (see Table 9 below):

Property Number of
Bedrooms
Location Actual
Sales Price
Correct
Category
Model’s
Prediction
G 2 Standard $220,000 NO NO
H 2 Prime $320,000 YES YES

Table 9: Second Round of Predictions

Well, well, well! Both predictions are correct.

These examples provide a small check of how the model performs on cases it did not use for learning. They do not establish that it will perform well on every future property. Let’s face it: Two successful checks are only two successful checks, however enthusiastically the agency’s marketing department might describe them.

Later, we will examine how to separate data for learning, choosing between approaches and evaluating the final result.

For now, just remember this:

And today is the day when Fernanda receives a special two-bedroom property in a Prime location. And as expected by now, the model predicts YES. Loud and clear.

But eventually, the property sells for $260,000. What can I say. Sometimes you are desperate to cash in and can’t wait for the price to reach or exceed the minimum value Fernanda is expecting! The world does not revolve around Fernanda, does it?

The bottom line is that in this case the prediction was wrong.

There are legitimate reasons for this: Perhaps the property needed major repairs. Perhaps its floor area was unusually small. Perhaps market conditions had changed. We would need evidence to find out, of course.

Whatever the explanation though, our model had access only to bedrooms and location. And it could not take account of details it never received.

As for Fernanda, she now has several possible responses:

  • Collect more examples to see whether the mistake reflects a wider pattern,
  • Investigate whether important information is missing,
  • Consider a broader set of possible models,
  • Decide whether this simple method is useful enough for its intended role.

Notice what she should not do: Assume that every mistake can be fixed by shouting “more AI” at the problem!

The truth is that sometimes we need a better learning method. Sometimes we need better data. Sometimes we need a clearer question. Often times we need a combination of these 3.

And sometimes a simple prediction should remain a suggestion for a person to review and reject or approve (we call this human in the loop).

One more point: Our model does not automatically change because a sale has occurred. In this example, someone must record the new outcome and run the learning procedure again. Automatic updating would be an additional design choice. It is certainly possible but it has less to do with Artificial Intelligence or Machine Learning and more to do with the Engineering aspect of predictive work.

Not every task requires learning from examples. Sometimes, the agency has already decided what must happen, and Fernanda’s job is to follow that instruction. For example:

In this case Fernanda checks whether permission has been recorded. If it has, she may send the details. Otherwise, she must not. It is really that simple.

In other words, there is nothing here for the computer to learn about whether permission should be required. That requirement comes from the agency’s policy. Even if employees sometimes ignored it in the past, their behavior would not make it acceptable and should not be considered as valid training data for extracting the correct patterns regarding the application of the policy (it may be used to identify patterns of bad or wrong behavior but that is a different question altogether). A bad habit does not become a good policy just because we have plenty of examples of it!

Predicting a property’s price category is different. The agency cannot issue an instruction that makes a house sell for at least $300,000 (although I suspect the manager would welcome that option😊). The sale price is an unknown outcome, and historical examples may help us predict it.

The key is to ask what the task needs:

  • Applying an established requirement, or
  • Estimating an unknown outcome?

Even for prediction, Machine Learning is not always necessary. A simple supplied rule may work well enough. We should choose an approach because it helps solve the problem, not because it sounds more impressive.

For example, Fernanda’s agency might combine:

  • A learned model that suggests a property’s price category,
  • A supplied rule requiring certain checks before a listing is published,
  • A person who reviews unusual cases and remains responsible for the final action.

Each part performs a different role.

The prediction draws on patterns in examples. The checks enforce explicit requirements. The person deals with context and responsibility beyond the narrow prediction task.

As we progress through this series, we will return to this question:

That is often more useful than asking whether a system is “AI-powered”, even though just mentioning anything AI fits nicely on a company website!

Before moving on, consider these questions:

  1. Fernanda writes “If Prime, predict YES” after personally inspecting the sales table. A computer then follows it. Has the computer learned the rule?
  2. A programmed procedure compares three candidate rules using historical examples and selects the one with the fewest mistakes. Where does learning occur?
  3. Our selected rule gets every training example right. What should we check next?
  4. Does the pattern in our small dataset prove that changing a property’s location would cause its price to rise?

We saw how Fernanda began with a simple instruction about bedrooms. By comparing its predictions with historical sales, she discovered that it made several mistakes. We then followed her as she specified a procedure that used those historical sales examples to select a better rule candidate for prediction.

Along the way, we saw that:

  • A computer can follow a rule correctly while making poor predictions,
  • A learning algorithm uses examples to determine a model,
  • The learned model can itself be a readable rule,
  • People still choose the objective, data, available inputs and learning procedure,
  • Success on training examples does not guarantee success on new cases,
  • Explicit rules, learned models and human judgment can work together.

Our example is intentionally small. Real learning systems can search through far more possibilities, and they do not all learn by counting mistakes across three candidate rules.

Nor does our comparison claim that people and computers learn in the same way. Fernanda brings understanding and experience to her work. Our little procedure compares predictions and counts errors. Keeping that boundary clear helps the analogy do its job.

A YES or NO category is useful, but sooner or later a seller will ask:

That is lovely, Fernanda. But how much do you think my house will sell for?

And in this case, a simple YES is unlikely to be accepted as a price estimate.

In the next article, Fernanda will combine property details to produce a number. We will examine what each detail contributes, why a starting amount matters and how a simple calculation can be adjusted using past sales.

Here are some answers to the questions posed in section 13 above:

  1. No. In this description, Fernanda learned from the examples. The computer received and applied her rule.
  2. Learning occurs when the procedure uses the examples to select the model. Applying the selected model afterwards is prediction.
  3. Check its performance on examples not used to select it. Also consider whether the data represent the properties on which it will be used.
  4. No. We have identified a predictive pattern in a small dataset. We have not established a causal effect. We will explore that distinction in a later article.

Feedback please😊

NEXT ARTICLE
Title: Can Three Numbers Really Predict the Price of a House?
Concept: Understanding Linear Regression

← Back to the AI Explained Roadmap