# Bagging vs Boosting

> How bagging trains many models side by side to steady them, and how boosting trains them one after another to fix each other's mistakes, for your AI interview.

**In short:** Bagging trains many models separately, each on a random sample of the data, then averages them to steady the result. Boosting trains models one after another, each one fixing the mistakes of the last, to build one strong model.

AI and LLM interview question Q439, Classic ML and deep-learning basics. Intermediate, occasional (our estimate). From Venkat R. Murty's free Interview Kit: https://designervenkat.online/interview/ai/bagging-vs-boosting

## Say it in 30 seconds

Bagging and boosting both combine many simple models into one, but they build them differently. Bagging trains each model on its own random sample and averages the results, which steadies a model that swings too much. Boosting trains models in order, and each new one focuses on what the last one got wrong. Boosting can reach high accuracy, but it can overfit if I add too many rounds.

## Say it in 2 minutes

Bagging and boosting both combine many simple models into one, but they build the models in different ways. Bagging trains each model on its own random sample of the past cases, then averages their answers. Boosting trains the models one after another, and each new model works on the mistakes of the ones before it.

The reason bagging helps is variance. A single fully grown tree, one that splits the rows until it fits its training data closely, can memorise the noise in that data, so its answers swing when the data changes. Averaging many trees trained on different samples cancels out those swings. scikit-learn says bagging works best with strong, complex models such as fully grown trees, because it reduces overfitting.

Boosting starts from weak models, such as shallow trees, and chains them. AdaBoost, the classic boosting method, gives the cases the last model got wrong more weight, so the next model focuses on them. Gradient boosting fits each new tree to the current errors. The risk is overfitting if I keep adding rounds, so I would set a learning rate, which is how big a step each new tree may take, and use early stopping, which ends training once the score on rows kept back from training stops improving.

In practice I would start with a random forest, which is bagging with a random set of features at each split, and test boosting against it on new data. If the labels are noisy, I would check the boosted model on rows kept back from training before trusting it.

## What are bagging and boosting, in plain words?

Both are ensembles. An ensemble combines several simple models into one predictor, and scikit-learn's guide says the goal is a better result than any single model gives.

The difference is how the models are made. Bagging makes each model on its own, from a random sample, and averages them. Boosting makes them in order, and each one is aimed at the mistakes of the one before.

Think of a bank's credit committee. In bagging, five analysts each read a different random pile of past loan files, and their scores are averaged. In boosting, one reviewer marks the files the previous reviewer got wrong, and the next reviewer gives those files extra weight. A card company's fraud team faces the same choice with its payment data.

- **Ensemble:** Many models whose answers are combined into one.
- **Weak learner:** A simple model only a bit better than guessing, such as a shallow tree.
- **Bootstrap sample:** A random copy of the training rows, drawn with repeats allowed.
- **Variance:** How much a model's answers swing when the training data changes.

## How does bagging work?

Bagging is short for bootstrap aggregating. Each model is a tree trained on a random sample of the rows, drawn with repeats, so some rows appear twice and some not at all. Google's course gives a typical figure of 67% of the examples for each tree.

> "In ensemble algorithms, bagging methods form a class of algorithms which build several instances of a black-box estimator on random subsets of the original training set and then aggregate their individual predictions to form a final prediction." (scikit-learn)

*Diagram, Bagging: trees trained side by side: Three trees each see a different random sample of past payments, and none waits for another. Their fraud scores are averaged, so one tree's odd call is diluted by the others.*
1. Tree 1 learns from its own random sample of the past payments.
2. Tree 2 learns from a different random sample.
3. Tree 3 learns from a third sample. None of the trees waits for another.
4. Tree 1 gives its fraud score, 0.82 in this made-up case.
5. Tree 2 gives its fraud score, 0.40.
6. Tree 3 gives its fraud score, 0.91. The average of the three is 0.71.

The trees do not depend on each other, so their building can run in parallel. scikit-learn's ensemble module supports the parallel construction of the trees.

Why does averaging help? Each tree makes its own errors, and those errors do not all point the same way, so many of them cancel out. scikit-learn calls this a way to reduce the variance of a single tree.

> **Tip:** Worked example, made-up numbers: a card company's fraud model has three trees. They score one payment 0.82, 0.40 and 0.91. The average is 0.71, so with a made-up review line of 0.6 the payment goes to review, and no single tree decided it alone.

## How does boosting work?

Boosting starts with a weak model and adds more models in order. Each new one is trained to fix what the models so far still get wrong. Google's course describes gradient boosting as combining many weak models, usually decision trees, one step at a time.

> "The core principle of AdaBoost is to fit a sequence of weak learners (i.e., models that are only slightly better than random guessing, such as small decision trees) on repeatedly modified versions of the data." (scikit-learn)
>
> "Gradient boosting creates a strong predictive model by iteratively combining multiple weak models, typically decision trees." (Google for Developers)

*Diagram, Boosting: each round fixes the last one's misses: Each round gives extra weight to the rows the last round got wrong. The rounds are then added into one model, so the hard cases end up with the most attention.*
1. Round 1 starts from all the past payments.
2. Round 1 misses some frauds, so those rows get more weight next round.
3. Round 2 still misses some frauds, so those rows get more weight for round 3.
4. All the rounds are added into one model that is stronger than any single round.

The classic version, AdaBoost, does this by changing the weights of the training rows. scikit-learn's guide says the rows predicted wrongly get their weights increased, and the rows predicted correctly get them decreased, so the hard cases gain influence as the rounds go on.

Gradient boosting uses a different detail. Each new tree is trained to predict the errors of the current model, and then it is added with a small step. Both methods chain their trees, but the details differ.

1. **Round 1:** A tree learns from every past payment. In a made-up set of 1,000 frauds, it misses 300.
2. **Round 2:** The 300 misses get extra weight. The next tree catches 200 of them, so 100 remain missed.
3. **Round 3:** The tree focuses on those 100. It catches 70, so 30 remain missed in this made-up example.

## Which one should you pick, and why?

- **Bagging: Strong trees, averaged.** Deep trees, each trained alone, then averaged. Steadier on new data, and the trees can be built in parallel.
- **Boosting: Weak trees, chained.** Shallow trees, trained in order. Can reach high accuracy, but it can overfit if rounds are not controlled.
- **Trade-off: Steadiness or reach.** Bagging is the safer first try. Boosting needs a learning rate and early stopping, and a test on new data.

A random forest is bagging of trees with one extra trick. At each split, only a random subset of the features is considered, which makes the trees differ more from each other. scikit-learn notes that random forests use bootstrap samples by default.

Boosting can overfit if rounds are not controlled. The learning rate is how big a step each new tree may take (shrinkage is the same idea), and early stopping means ending training once the score on rows kept back from training stops improving. scikit-learn says the learning rate controls overfitting through shrinkage.

> **Tip:** Avoid saying boosting is always better. Name the fit instead: bagging when a single model swings too much, boosting when the simple model is too weak and a test on new data confirms the gain.

### How would you choose for a messy claims dataset?

An insurer's claims team has labels typed by busy adjusters, and in a made-up audit about 5% of the fraud flags are wrong. The model must also work next quarter, on new claims.

I would start with bagged deep trees. Averaging dilutes the effect of an odd wrong label, though it does not remove it. Then I would try boosting with a small learning rate and early stopping, measured on rows kept back from training. I would keep boosting only if it wins on the new claims, not on the training data.

## What does the interviewer want to hear?

**A strong answer covers**

- [ ] Bagging: separate models on random samples, averaged to cut variance.
- [ ] Boosting: models in order, each one fixing the last one's errors.
- [ ] Why bagging suits strong trees and boosting suits weak ones.
- [ ] Random forests are bagging of trees, plus random features at each split.
- [ ] Boosting's risk: overfitting when the rounds are not controlled.
- [ ] A choice backed by a test on new data, for the data at hand.

**Common traps**

- Trap: "Boosting is always more accurate." Better: It often scores higher, but it can overfit if rounds are not controlled. Test it against bagging on new data.
- Trap: "Bagging and boosting both train in order." Better: Bagging trains each model alone, so the models don't depend on each other. Boosting is the ordered one.
- Trap: "A random forest is just boosting with more trees." Better: A random forest is bagging of trees, with random features at each split. Boosting chains its models.

## Can you spot the right setup?

**Spot the bagging:** A colleague on the fraud team wrote one of these four setups. Which one is bagging, not boosting?

A. AdaBoostClassifier(DecisionTreeClassifier(max_depth=2), n_estimators=50)
B. BaggingClassifier(DecisionTreeClassifier(), n_estimators=50, bootstrap=True)
C. GradientBoostingClassifier(n_estimators=50, learning_rate=0.1)
D. HistGradientBoostingClassifier(max_iter=50)

**Answer:** B. Bagging builds deep trees on random samples, each alone, then averages them. The other three are boosting: each round or stage learns from the errors of the ones before.

**Quick check:** A claims model's trees swing a lot each time the data is refreshed. Which change targets that problem?

A. Add more boosting rounds of very shallow trees
B. Train many deep trees on random samples and average them
C. Raise the learning rate to 1.0
D. Drop the validation set so training uses all the rows

**Answer:** B. That is high variance. Averaging trees trained on random samples cancels out their swings. Boosting works by fixing errors round by round, so it does not target swings the same way.

## Can you answer these from memory?

**Flash cards**

- **Bagging in one line?** Many models trained separately on random samples, then averaged.
- **Boosting in one line?** Models trained in order, each one fixing the errors of the last.
- **What does bagging mainly reduce?** Variance: how much the answers swing when the data changes.
- **What does boosting mainly risk?** Overfitting if rounds are not controlled. Use a learning rate and early stopping.
- **Are random forests bagging or boosting?** Bagging of trees, plus a random subset of features at each split.
- **Can bagging build its trees in parallel?** Yes. Each tree is built alone, so the trees do not wait for each other.

## Likely follow-up questions

- Q412 Bias vs variance? https://designervenkat.online/interview/ai/bias-vs-variance
- Q413 Overfitting vs underfitting? https://designervenkat.online/interview/ai/overfitting-vs-underfitting
- Q431 Decision trees and random forests? https://designervenkat.online/interview/ai/decision-trees-and-random-forests
- Q429 What is early stopping? https://designervenkat.online/interview/ai/what-is-early-stopping
- Q436 How do you tune hyperparameters? https://designervenkat.online/interview/ai/how-do-you-tune-hyperparameters
- Q423 What is cross-validation? https://designervenkat.online/interview/ai/what-is-cross-validation

## Learn it in depth

- [Decision trees and random forests](https://designervenkat.online/learn/machine-learning/decision-trees-random-forest)
- [Gradient boosting: fixing mistakes](https://designervenkat.online/learn/machine-learning/gradient-boosting)
