부트스트랩 소개

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

James Chapman

Curriculum Manager, DataCamp

복원 vs 비복원

비복원 단순무작위추출:

카지노 테이블 위의 트럼프.

복원추출(“재표본추출”):

구르는 주사위 네 개.

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

비복원 단순무작위추출

모집단:

행과 열로 정렬된 커피콩.

표본:

행과 열로 정렬된 커피콩. 대부분은 회색 처리됨.

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

복원 단순무작위추출

모집단:

행과 열로 정렬된 커피콩.

재표본:

무작위로 뽑은 커피콩 일부는 중복됨.

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

왜 복원추출을 할까요?

  • coffee_ratings: 전체 커피 집단의 표본
  • 표본의 각 커피는 여러 가상의 모집단 커피를 대표합니다
  • 복원추출은 그 대체 수단입니다
Python으로 살펴보는 표본추출(Sampling)

커피 데이터 준비

coffee_focus = coffee_ratings[["variety", "country_of_origin", "flavor"]]
coffee_focus = coffee_focus.reset_index()
      index  variety country_of_origin  flavor
0         0     None          Ethiopia    8.83
1         1    Other          Ethiopia    8.67
2         2  Bourbon         Guatemala    8.50
3         3     None          Ethiopia    8.58
4         4    Other          Ethiopia    8.50
...     ...      ...               ...     ...
1333   1333     None           Ecuador    7.58
1334   1334     None           Ecuador    7.67
1335   1335     None     United States    7.33
1336   1336     None             India    6.83
1337   1337     None           Vietnam    6.67

[1338 rows x 4 columns]
Python으로 살펴보는 표본추출(Sampling)

.sample()로 복원추출

coffee_resamp = coffee_focus.sample(frac=1, replace=True)
      index  variety country_of_origin  flavor
1140   1140  Bourbon         Guatemala    7.25
57       57  Bourbon         Guatemala    8.00
1152   1152  Bourbon            Mexico    7.08
621     621  Caturra          Thailand    7.50
44       44     SL28             Kenya    8.08
...     ...      ...               ...     ...
996     996   Typica            Mexico    7.33
1090   1090  Bourbon         Guatemala    7.33
918     918    Other         Guatemala    7.42
249     249  Caturra          Colombia    7.67
467     467  Caturra          Colombia    7.50

[1338 rows x 4 columns]
Python으로 살펴보는 표본추출(Sampling)

중복된 커피

coffee_resamp["index"].value_counts()
658     5
167     4
363     4
357     4
1047    4
       ..
771     1
770     1
766     1
764     1
0       1
Name: index, Length: 868, dtype: int64
Python으로 살펴보는 표본추출(Sampling)

누락된 커피

num_unique_coffees = len(coffee_resamp.drop_duplicates(subset="index"))
868
len(coffee_ratings) - num_unique_coffees
470
Python으로 살펴보는 표본추출(Sampling)

부트스트랩

모집단에서 표본추출의 반대

표본추출: 모집단에서 더 작은 표본으로

부트스트랩: 표본에서 이론적 모집단을 구성

부트스트랩 활용:

  • 하나의 표본로 표집 변동성을 이해

카우보이 부츠.

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

부트스트랩 과정

  1. 원표본과 같은 크기로 재표본을 만듭니다
  2. 이 부트스트랩 표본의 관심 통계를 계산
  3. 1–2를 여러 번 반복

얻은 통계는 부트스트랩 통계이며, 이는 부트스트랩 분포를 이룹니다

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

커피 평균 풍미 부트스트랩

import numpy as np

mean_flavors_1000 = []
for i in range(1000):
mean_flavors_1000.append(
np.mean(coffee_sample.sample(frac=1, replace=True)['flavor'])
)
Python으로 살펴보는 표본추출(Sampling)

부트스트랩 분포 히스토그램

import matplotlib.pyplot as plt
plt.hist(mean_flavors_1000)
plt.show()

평균 풍미의 부트스트랩 분포

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

실습해 봅시다!

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

Preparing Video For Download...