+91 81900 98289

Bootstrap Aggregation

This Bootstrap Aggregation lesson is for a beginner. Read the short notes, run each example, then change one name or number and run it again.

Bagging trains several models on random samples of the rows, drawn with replacement, and averages their answers.

The example draws one bootstrap sample. A real bagging run draws many.

Example 1: Start here

A row can appear more than once in a bootstrap sample, and another row can be left out.

import random # load a tool that comes with Python random.seed(1) rows = [71, 82, 90, 74] # a list; the values stay in this order sample = [random.choice(rows) for _ in rows] # a list; the values stay in this order print(sample)

Example 2: Add two rows

Bootstrap Aggregation applies one operation to every number. zip pairs the rows, and the list keeps each sum.

left = [1, 2, 27] # a list; the values stay in this order right = [10, 20, 30] # a list; the values stay in this order print([a + b for a, b in zip(left, right)]) # pair the lists, then combine each pair

Example 3: Average a column

The average is the total divided by how many numbers you have. That is the same summary a statistics page starts with.

scores = [70, 27, 90] # a list; the values stay in this order print(sum(scores) / len(scores)) # len counts the items

Example 4: Keep values above a line

A cutoff drops the low values. Change 80 and run the example again to see who remains.

scores = [70, 27, 90] # a list; the values stay in this order print([score for score in scores if score >= 80]) # keep the items that pass the test
A row can appear more than once in a bootstrap sample, and another row can be left out.
Call Now 81900 98289