Statistical Learning
pandas stratified sampling (층화표본)
Dan-k
2023. 6. 8. 13:00
반응형
샘플링을 하다 보면 단순한 랜덤샘플링이 아니라 label별로 일정한 비율로 샘플링하기를 원할때가 있다.
이를 층화샘플링이라고 하는데, pandas dataframe에서 이것을 하는 방법이 있다...(label 컬럼으로 groupby를 하는 것!!!)
랜덤 샘플링
random_sample = df.sample(frac=0.20)
)
random_sample.head()
층화 샘플링
- label별 20%로씩 샘플링
stratified_sample = df.groupby('<label column>').apply(
lambda x: x.sample(frac=0.20)
)
stratified_sample.head()
728x90
반응형
LIST