The Sampling Distribution
Part of the Statistics for ML section of Coddy's Machine Learning journey. Lesson 21 of 64.
Video sessions are right-skewed: most last a few minutes, a few run two hours. Yet the means of 1000 samples of 40 sessions form a bell:
The histogram of many sample means is the sampling distribution. With enough values in each sample, it is a bell centered on the population mean, even for skewed data:
means = sample_means(sessions, 40, 1000) # your function from Sample Means Vary
ax.hist(means, bins=12)That is the central limit theorem. It makes averages predictable: a model's mean error over a test set is roughly normal too.
Challenge
MediumA cloud drive's 6000 file sizes in files (MB) are right-skewed: many documents, a few videos. The program reads a sample size; the starter has sample_means and two charts.
- Get 500 sample means of that size.
- Print mean minus median for the files, then the means (2 decimals).
- Histogram the files on
axs[0]and the means onaxs[1],bins=12, and show.
For another drive and 60:
Files: mean - median = 25.28 MB
Sample means: mean - median = 0.55 MBThen Coddy reports each chart. A gap near 0 means the skew is gone.
Try it yourself
import numpy as np
import matplotlib.pyplot as plt
size = int(input())
# Don't change the lines below: the size in MB of every file in the drive
rng = np.random.default_rng(21)
files = np.round(rng.random(6000) ** 4 * 300 + 5, 2)
# Your function from Sample Means Vary: the means of count samples
def sample_means(data, size, count):
means = []
for i in range(count):
sample = rng.choice(data, size=size, replace=False)
means.append(sample.mean())
return np.array(means)
fig, axs = plt.subplots(1, 2)
axs[0].set_title("All Files")
axs[0].set_xlabel("File size (MB)")
axs[1].set_title("Sample Means")
axs[1].set_xlabel("Mean file size (MB)")
# 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