Menu
CoddyTech

Box Plots

Part of the Statistics for ML section of Coddy's Machine Learning journey. Lesson 24 of 64.

Every dataset hides odd values: a typo, a glitch, a record day. This chapter finds and tames them, starting with the chart that shows them, the box plot:

11 podcast episode lengths as dots, and beside them their box plot: a box from Q1, 39, to Q3, 47.5, split by the median line at 43, whiskers down to 32 and up to 52, and one lone dot at 75 marked outlier

ax.boxplot draws it:

episodes = np.array([41, 36, 49, 43, 32, 46, 75, 40, 52, 38, 44])
fig, ax = plt.subplots()
ax.boxplot(episodes)
plt.show()

The box holds the middle half of the values, Q1 to Q3, split by the median. Whiskers reach the last values not too far out.

Dots beyond them are outliers, values far from the rest: the ones to check before a model learns from them.

challenge icon

Challenge

Medium

A family logged two weeks of reading in reading.csv, one column of minutes per person. The program reads a name, and the starter sets up the chart.

  1. Draw a box plot of that person's column.
  2. Print their median and the box's edges, Q1 and Q3, each rounded to 2 decimals, as below.

For Sam, another reader, the program prints:

Typical day: 97.5 minutes
Middle half: 90.5 to 108.75 minutes

and draws this chart (Coddy then describes yours):

A box plot titled Reading: Sam with Minutes up the side: a box from about 90 to 109 with the median line near 98, whiskers from 87 to 128, and outlier dots at 61 and 205

Try it yourself

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

reading = pd.read_csv("reading.csv")
name = input()
# Don't change the lines below
fig, ax = plt.subplots()
ax.set_title(f"Reading: {name}")
ax.set_ylabel("Minutes")

# Write your code below
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Statistics for ML

Practice on your own: Online Python compiler