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:
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
MediumA 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.
- Draw a box plot of that person's column.
- 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 minutesand draws this chart (Coddy then describes yours):
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
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Statistics for ML
1Center and Spread
Mean and MedianPercentiles and QuartilesVariance and StdSample or Population?The Most Common ValueRecap: Delivery Times4Samples and Populations
Population and SampleSample Means VaryThe Sampling DistributionBigger Samples, Less NoiseRecap: Polling a Town2Distributions
Density HistogramsShapes of DataThe Normal CurveThe 68-95-99.7 RuleZ-ScoresRecap: Battery Life3Probability by Simulation
Probability as FrequencyThe Law of Large NumbersSums of DiceExpected ValueConditional ProbabilityRecap: Dice Stall Odds6Relationships
Reading Scatter PlotsCorrelationCorrelation MatrixCorrelation HeatmapsCorrelation Is Not CauseRecap: Ice Cream and SunPractice on your own: Online Python compiler