Implementing Sampling to Python
Svep för att visa menyn
Simple Random Sampling
1234567import random N = 30 # population size n = 5 # sample size sample_srs = random.sample(range(1, N+1), n) print(f"Simple Random Sample: {sample_srs}")
random.sample(range(1, N+1), n)randomly selects n unique values from the population;- Works without replacement (no repeats);
- Every member of the population has an equal chance of being chosen.
Stratified Sampling
123456789N_males = 18 N_females = 12 N_total = N_males + N_females n_total = 10 n_males = round((N_males / N_total) * n_total) n_females = round((N_females / N_total) * n_total) print(f"Stratified Sample Size -> Males: {n_males}, Females: {n_females}")
- Population is divided into subgroups (strata);
- Sample is drawn proportionally from each subgroup;
- Ensures representation of key groups.
Cluster Sampling
1234567import random clusters = 5 students_per_cluster = 25 selected_cluster = random.randint(1, clusters) print(f"Selected cluster (classroom): {selected_cluster} containing {students_per_cluster} students")
- Population divided into clusters (e.g., classrooms);
- One or more clusters are selected randomly;
- Everyone in chosen cluster(s) is surveyed;
- Efficient when listing every individual is impractical.
Systematic Sampling
123456789101112import random N = 1000 n = 100 k = N // n # Sampling interval start = random.randint(1, k) # Random start sample_systematic = list(range(start, N+1, k)) print(f"Sampling interval k = {k}") print(f"Random start = {start}") print(f"First 10 samples: {sample_systematic[:10]}")
- Interval k=nN;
- Start point chosen randomly between 1 and k;
- Select every k-th element from ordered population.
Summary of Methods
- Simple Random: equal chance for all, no repeats;
- Stratified: ensures subgroup representation;
- Cluster: randomly selects whole groups;
- Systematic: selects at fixed intervals after random start.
Var allt tydligt?
Tack för dina kommentarer!
Avsnitt 1. Kapitel 45
Fråga AI
Fråga AI
Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal
Avsnitt 1. Kapitel 45