Python으로 살펴보는 표본추출(Sampling)
James Chapman
Curriculum Manager, DataCamp

인구조사는 각 가구에 거주 인원을 묻습니다.

인구조사는 비용이 매우 큽니다!

소수의 가구만 조사하고 통계로 전체를 추정하는 것이 더 저렴합니다
전체에서 일부만 다루는 것을 표본추출이라 합니다
모집단은 전체 데이터셋입니다
표본은 계산에 사용하는 데이터의 부분집합입니다
| total_cup_points | variety | country_of_origin | aroma | flavor | aftertaste | body | balance |
|---|---|---|---|---|---|---|---|
| 90.58 | NA | Ethiopia | 8.67 | 8.83 | 8.67 | 8.50 | 8.42 |
| 89.92 | Other | Ethiopia | 8.75 | 8.67 | 8.50 | 8.42 | 8.42 |
| ... | ... | ... | ... | ... | ... | ... | ... |
| 73.75 | NA | Vietnam | 6.75 | 6.67 | 6.5 | 6.92 | 6.83 |
pts_vs_flavor_pop = coffee_ratings[["total_cup_points", "flavor"]]
total_cup_points flavor
0 90.58 8.83
1 89.92 8.67
2 89.75 8.50
3 89.00 8.58
4 88.83 8.50
... ... ...
1333 78.75 7.58
1334 78.08 7.67
1335 77.17 7.33
1336 75.08 6.83
1337 73.75 6.67
[1338 rows x 2 columns]
pts_vs_flavor_samp = pts_vs_flavor_pop.sample(n=10)
total_cup_points flavor
1088 80.33 7.17
1157 79.67 7.42
1267 76.17 7.33
506 83.00 7.67
659 82.50 7.42
817 81.92 7.50
1050 80.67 7.42
685 82.42 7.50
1027 80.92 7.25
62 85.58 8.17
[10 rows x 2 columns]
pandas DataFrame과 Series에는 .sample()을 사용합니다cup_points_samp = coffee_ratings['total_cup_points'].sample(n=10)
1088 80.33
1157 79.67
1267 76.17
... ...
685 82.42
1027 80.92
62 85.58
Name: total_cup_points, dtype: float64
모집단 모수는 모집단 데이터셋으로 계산한 값입니다
import numpy as np
np.mean(pts_vs_flavor_pop['total_cup_points'])
82.15120328849028
점추정(또는 표본 통계량)은 표본 데이터셋으로 계산한 값입니다
np.mean(cup_points_samp)
81.31800000000001
pts_vs_flavor_pop['flavor'].mean()
7.526046337817639
pts_vs_flavor_samp['flavor'].mean()
7.485000000000001
Python으로 살펴보는 표본추출(Sampling)