Menu
CoddyTech

Box Plots by Group

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

Two groups differ: is it real, or luck? This chapter ends with the picture that answers it:

A histogram of 1000 differences that shuffling made by chance, piled around 0, with a red line for the real difference far out at the left edge

It starts with a look. A pottery studio timed its two kilns. Give ax.boxplot a list of groups, and their names:

gas = np.array([6.5, 7, 7.5, 8, 7, 6.5, 8.5, 7.5])
electric = np.array([9, 10.5, 11, 9.5, 12, 10, 10.5, 15])
fig, ax = plt.subplots()
ax.boxplot([gas, electric], tick_labels=["Gas", "Electric"])
ax.set_ylabel("Hours")
plt.show()

Two box plots on one axis: Gas low, around a median of 7.25 hours, and Electric higher, around 10.5 hours, with one outlier dot at 15. The boxes do not overlap

The boxes do not even overlap: gas fires faster. For a model, a feature whose boxes barely overlap helps tell the groups apart.

challenge icon

Challenge

Medium

A ski resort timed 36 rides on its three lifts. lifts.csv has lift (Red, Green or Blue) and minutes; the starter loads it and sets up the chart.

  1. Loop over names and collect each lift's minutes in a list.
  2. Draw one box per lift, named with tick_labels=.
  3. Show the chart.

It prints nothing; Coddy describes each box. Two other lifts look like this:

A chart titled Lift Times with two box plots named Orange and Purple: Orange low around 18 minutes, Purple higher around 27.5 minutes with one outlier dot at 44

Try it yourself

import pandas as pd
import matplotlib.pyplot as plt

lifts = pd.read_csv("lifts.csv")
names = ["Red", "Green", "Blue"]
# Don't change the lines below
fig, ax = plt.subplots()
ax.set_title("Lift Times")
ax.set_xlabel("Lift")
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