층화 및 가중 임의 추출

Python으로 살펴보는 표본추출(Sampling)

James Chapman

Curriculum Manager, DataCamp

국가별 커피 수

원산지별로 그룹화된 커피 원두.

top_counts = coffee_ratings['country_of_origin'].value_counts()
top_counts.head(6)
country_of_origin
Mexico                    236
Colombia                  183
Guatemala                 181
Brazil                    132
Taiwan                     75
United States (Hawaii)     73
dtype: int64
1 이 데이터셋은 편의를 위해 하와이와 타이완을 각각 커피 재배 지역으로서의 국가로 표기합니다.
Python으로 살펴보는 표본추출(Sampling)

6개 국가로 필터링

top_counted_countries = ["Mexico", "Colombia", "Guatemala",
  "Brazil", "Taiwan", "United States (Hawaii)"]


top_counted_subset = coffee_ratings['country_of_origin'].isin(top_counted_countries)
coffee_ratings_top = coffee_ratings[top_counted_subset]
Python으로 살펴보는 표본추출(Sampling)

단순 임의 추출의 비율

coffee_ratings_samp = coffee_ratings_top.sample(frac=0.1, random_state=2021)
coffee_ratings_samp['country_of_origin'].value_counts(normalize=True)
country_of_origin
Mexico                    0.250000
Guatemala                 0.204545
Colombia                  0.181818
Brazil                    0.181818
United States (Hawaii)    0.102273
Taiwan                    0.079545
dtype: float64
Python으로 살펴보는 표본추출(Sampling)

비율 비교

모집단:

Mexico                    0.268182
Colombia                  0.207955
Guatemala                 0.205682
Brazil                    0.150000
Taiwan                    0.085227
United States (Hawaii)    0.082955
Name: country_of_origin, dtype: float64

10% 단순 임의 표본:

Mexico                    0.250000
Guatemala                 0.204545
Colombia                  0.181818
Brazil                    0.181818
United States (Hawaii)    0.102273
Taiwan                    0.079545
Name: country_of_origin, dtype: float64
Python으로 살펴보는 표본추출(Sampling)

비율 기반 층화 추출

coffee_ratings_strat = coffee_ratings_top.groupby("country_of_origin")\

.sample(frac=0.1, random_state=2021)
coffee_ratings_strat['country_of_origin'].value_counts(normalize=True)
Mexico                    0.272727
Guatemala                 0.204545
Colombia                  0.204545
Brazil                    0.147727
Taiwan                    0.090909
United States (Hawaii)    0.079545
Name: country_of_origin, dtype: float64
Python으로 살펴보는 표본추출(Sampling)

동일 개수 층화 추출

coffee_ratings_eq = coffee_ratings_top.groupby("country_of_origin")\
    .sample(n=15, random_state=2021)
coffee_ratings_eq['country_of_origin'].value_counts(normalize=True)
Taiwan                    0.166667
Brazil                    0.166667
United States (Hawaii)    0.166667
Guatemala                 0.166667
Mexico                    0.166667
Colombia                  0.166667
Name: country_of_origin, dtype: float64
Python으로 살펴보는 표본추출(Sampling)

가중 임의 추출

  • 가중치를 지정하여 행이 추출될 상대 확률을 조정합니다.
import numpy as np
coffee_ratings_weight = coffee_ratings_top
condition = coffee_ratings_weight['country_of_origin'] == "Taiwan"
coffee_ratings_weight['weight'] = np.where(condition, 2, 1)
coffee_ratings_weight = coffee_ratings_weight.sample(frac=0.1, weights="weight")
Python으로 살펴보는 표본추출(Sampling)

가중 임의 추출 결과

가중치 적용 10% 표본:

coffee_ratings_weight['country_of_origin'].value_counts(normalize=True)  
Brazil                    0.261364
Mexico                    0.204545
Guatemala                 0.204545
Taiwan                    0.170455
Colombia                  0.090909
United States (Hawaii)    0.068182
Name: country_of_origin, dtype: float64
Python으로 살펴보는 표본추출(Sampling)

연습해 봅시다!

Python으로 살펴보는 표본추출(Sampling)

Preparing Video For Download...