Menu

Populations, Samples and How to Draw Them

Populations, Samples and How to Draw Them

A population is every item you care about, such as all an app's customers. A sample is the smaller group you actually observe. Measuring the whole population is usually too slow or expensive, so data scientists work with samples and use them to estimate the truth.

The most common estimate is the sample mean:

x̄ = (Σxᵢ) / n

  • x̄ (x-bar): the average of your sample, which serves as your best guess for the population’s average.
  • xᵢ: the i-th observed value in the sample, such as one customer’s delivery time.
  • Σ (sigma): an instruction to add up all those values.
  • n: how many observations are in the sample.
  • μ (mu): the true population average. You rarely see it, which is why x̄ exists.

Suppose five deliveries took 28, 35, 31, 40 and 26 minutes. The sum is 160, and 160 / 5 gives x̄ = 32 minutes.

Common ways to draw a sample

  • Simple random: every member has an equal chance of being picked.
  • Stratified: split the population into groups first, then sample from each group in proportion to its size.
  • Systematic: pick every k-th member from an ordered list.
  • Cluster: randomly choose whole groups (say, entire stores) and study everyone inside them.

Example: Take an app with 5,000 customers, 3,000 urban and 2,000 rural, and a target sample of 200. That is 4% of the population, so a proportional stratified sample takes 4% from each group: 120 urban and 80 rural customers. This keeps both groups represented as they are in reality.

import pandas as pd

df = pd.read_csv("customers.csv")

simple = df.sample(n=200, random_state=42)

stratified = df.groupby("region").sample(frac=0.04, random_state=42)

Code Explanation:

  • The customer data is loaded into a DataFrame called df.
  • df.sample(n=200) picks 200 rows completely at random, which is a simple random sample.
  • random_state=42 fixes the randomness so the same rows are picked every time you rerun it.
  • groupby("region") separates the rows by region, and sample(frac=0.04) then takes 4% from each region, giving the stratified sample.

Probability and Statistics for Data Science

AP

Abhishek Pati



Learn via Video Course

Probability and Statistics for Data Science Logo

Probability and Statistics for Data Science

4 hrs



Get in Touch For Details