Density Histograms
Part of the Statistics for ML section of Coddy's Machine Learning journey. Lesson 7 of 64.
This chapter is about the shape of data. By its end, you will fit a bell curve to a histogram and spot the odd one out:
First, a fix. Counts let a big group dwarf a small one, like 500 adults' reaction times against 50 gamers':
density=True scales each group so its bar areas add up to 1:
ax.hist(adults, bins=16, density=True, alpha=0.5, label="adults")
ax.hist(gamers, bins=16, density=True, alpha=0.5, label="gamers")Each height is count / (n * bin width), so a bar's area is its share of the group. Groups of any size now compare fairly.
Challenge
EasyA city hospital weighed 400 newborns, a rural one 60. The starter holds the weights in city and rural and sets up the titled, labelled chart.
- Draw a density histogram of each group with
bins=10,alpha=0.5and the labelcityorrural. - Add a legend and show the chart.
Coddy describes your chart. It should look like this one, from two tomato growers:
Try it yourself
import numpy as np
import matplotlib.pyplot as plt
# Don't change the lines below
rng = np.random.default_rng(23)
city = np.round(rng.normal(3.4, 0.5, size=400), 2)
rural = np.round(rng.normal(3.2, 0.45, size=60), 2)
fig, ax = plt.subplots()
ax.set_title("Birth Weights")
ax.set_xlabel("Weight (kg)")
ax.set_ylabel("Density")
# 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