AI Hints
Home›Statistics & Math for ML Tutorial›Going Deeper: Samples and Classes
Statistics & Math for ML · Lesson 124 of 138

Sampling Methods

Random, stratified, cluster.

⏱ 2 min read▶ Video at 2:28:14🎓 Free
Watch this lesson in the Statistics & Math for ML courseWatch on YouTube ↗Full Statistics & Math for ML course

How you choose a sample matters as much as how big it is. There are a few standard ways to sample, and each one fits a different situation.

Sampling Methods
From the video: Sampling Methods
  • How you choose matters
  • Not just how many
  • A few standard ways

Four ways to sample

A simple random sample gives everyone the same chance. A stratified sample splits the population into groups first, like regions, and samples from each one, so no group is missed. A cluster sample picks whole groups, like a few schools. And a systematic sample takes every tenth person on a list.

MethodHowGood for
Simple randomeveryone equal chancethe default
Stratifiedsample within each groupsmall groups matter
Clusterpick whole groupscheap, spread out
Systematicevery k-th itemlists and queues

Stratified sampling in pandas

Our population is ninety percent from one city, and ten percent from another. A small random sample can easily under count the small city. Group by, then sample a fraction from each group, keeps the shares exact. Run it.

stratified.pypython
import pandas as pd

pop = pd.DataFrame({"city": ["Big"] * 9000 + ["Small"] * 1000})

random = pop.sample(50, random_state=3)
strat = pop.groupby("city").sample(frac=0.005, random_state=3)

print("random:    ", random["city"].value_counts().to_dict())
print("stratified:", strat["city"].value_counts().to_dict())
Output
random:     {'Big': 46, 'Small': 4}
stratified: {'Big': 45, 'Small': 5}

Summary

Random, stratified, cluster, and systematic sampling each fit different needs. Stratifying keeps small groups represented. Next, stratified splits for machine learning.

Key takeaways
  • Simple random
  • Stratified by group
  • groupby().sample
Next up: Stratified Splits — keep the class balance