Skip to main content
MahiDev — Learn. Build. Become AI-ready.

Lesson 12

Ensemble Methods

Nine beginner steps on one held-out mark of 60: bag guesses of 50, 75 and 55, and why their average can beat each single guess.

Beginner22 min
Bar chart: bag guesses 50, 75 and 55, and their average 60 matches the actual mark

What You Will Learn

Nine steps. One held-out student. The real mark is 60. Three bags guess before anyone averages them.

1. What is an ensemble?

An ensemble is several models asked for a guess, then one rule that turns those guesses into the answer you keep.

This student was not in any bag. The real mark is 60.

Bag Who it saw Its guess Signed miss
1 Marks 40 and 60, so it says their average 50 50 − 60 = −10
2 Marks 80 and 70 75 75 − 60 = +15
3 Marks 60 and 50 55 55 − 60 = −5

None of the three hit 60. The ensemble average does:

(50 + 75 + 55) / 3 = 180 / 3 = 60

The signed misses add up as well: −10 + 15 + −5 = 0. You average the guesses, not the sizes of the misses. The size of bag 2's miss is 15, but the sign is plus, so it pulls the low guesses back up.

2. Types of ensembles

Type When the models speak On this student
Bagging Together. Each bag sees its own rows, then you average 50, 75 and 55 at the same time. Average 60. A random forest is bagging of trees
Boosting In a line. The next model studies the miss that is still left That is the gradient boosting lesson: start at 60, add only a fraction of the next correction
Voting Together, but the answer is a label, and the majority wins Calling 60 a pass, the bags say fail, pass, fail. Majority says fail
Stacking A second model reads the guesses, not the original hours With one student the checkable stack is the plain average, 60. Weights need many students

Bagging asks "what do the bags say together?" Boosting asks "what is still wrong after the last guess?" Do not draw them as the same picture.

3. Where is it used?

Wherever one simple rule is noisy and several noisy rules disagree in different directions.

Job The single guess The ensemble
This mark 50 or 75 or 55 60
Prices One tree on one sample of houses The average of many trees
Pass or fail One model's label A vote, only when the labels really disagree both ways
Fraud One rule that misses a rare case Several rules, then a careful score. Accuracy alone still lies, as on the imbalanced data page

4. Where it sits in the workflow

  1. Train the bags on different slices. None of them trains on this student.
  2. Collect the guesses: 50, 75, 55.
  3. Average them if the target is a number. Vote if the target is a label and you have checked that the vote is the right tool.
  4. Read the result on students the bags did not see. A learning curve still applies: if every bag memorises its own slice, the average can look perfect on those slices and miss a new student.

5. What a single guess hides

Three bags each guess 50 and the average stays 50, ten below the actual mark of 60

Bag 1 alone is 10 marks low. Bag 2 alone is 15 marks high. Either report, by itself, looks like "the model missed."

The average hides a second trap. Suppose every bag had seen only low marks and each said 50.

(50 + 50 + 50) / 3 = 50

The ensemble is still 10 marks low. Same mistake, three times, does not cancel. The bags have to be wrong in different directions. That is the whole reason to show them different rows.

Voting has its own trap on this same student. Treat a guess of 60 or more as pass, and anything lower as fail.

Bag Guess Label
1 50 fail
2 75 pass
3 55 fail

Two fails, one pass. The majority says fail. The real mark is 60, a pass. Averaging the numbers got 60. Voting the labels got the wrong class. Use the vote when you only have labels. If you still have the numbers, average the numbers.

6. How the average fixes the mark

Signed misses of the three bags: -10, +15 and -5, which sum to 0

Write the three guesses, subtract 60, and add.

−10 + 15 = 5, then 5 + −5 = 0.

Divide by 3 only after you add the guesses, which is the same as adding the signed misses and dividing:

(−10 + 15 + −5) / 3 = 0

So the ensemble mark is 60 + 0 = 60.

Bag 2 looked like the worst guess if you only stare at the size 15. It is also the guess that cancelled bag 1. Dropping it because it looks wild leaves (50 + 55) / 2 = 52.5, which is 7.5 marks low. On this one student, keeping the odd bag was the right call. Stacking, a model that decides the weights, needs more than one held-out student before it can learn to drop anyone.

Pandas only prints the average

The bags were already guessed above. Pandas does not train them. import pandas as pd loads the table module. DataFrame holds the three guesses. Subtracting 60 builds the miss column. .mean() is the ensemble.

python
import pandas as pd

guesses = pd.DataFrame({
    "bag": ["bag 1", "bag 2", "bag 3"],
    "guess": [50, 75, 55],
})
guesses["miss"] = guesses["guess"] - 60
print(guesses)
print(guesses["guess"].mean())

The last print is 60.0. That is (50 + 75 + 55) / 3, not the average of the absolute misses. The absolute misses are 10, 15 and 5, and their average is 10, which is a different and worse summary. Keep the signs until you have added.

7. Key Takeaways

  • An ensemble turns several guesses into one answer.
  • On this student the guesses are 50, 75 and 55. The average is 60, the real mark.
  • The signed misses −10, +15 and −5 sum to 0. The sizes alone, 10, 15 and 5, do not.
  • If every bag says 50, the average stays 50. The errors must point different ways.
  • Bagging averages models that were trained together on different rows. Boosting adds the next model to fix what is still wrong.
  • Voting the labels here says fail. Averaging the marks says 60, a pass. They are not the same tool.
  • Pandas on this page only prints the table and the mean. It does not build the bags.

8. Mind Map

Bar chart: bag guesses 50, 75 and 55, and their average 60 matches the actual mark

Cover the average bar and add 50, 75 and 55. If you still get 60, and you still know that three 50s would stay at 50, the page is yours.

9. Quiz

The questions use this student. Real mark 60. Bags guess 50, 75 and 55.

Loading playground…

Check your understanding

Ensemble Methods Quiz

Three bags guess 50, 75 and 55 for a student whose mark is 60. What is the ensemble average?

1 / 6