본문 바로가기
Python

[python] pandas를 이용하여DataFrame 무작위 추출하기,Random sampling

by Chandler.j 2021. 1. 26.
반응형

fig1. title

machine learning에서

데이터 구조가 imbalance할 때 down sampling 기법 중 가장 간단한 방법

무작위 추출 : random sampling 

random state를 꼭 설정해주어야 reproducible 함. 


#1. DataFrame.sample

원하는 개수만큼 parameter : n= 원하는 개수

전체 dataframe의 길이의 비율 ; parameter : frac= 원하는 비율(0~1) ; replace=true 해줘야함

df=pd.read_csv("C:/Users/comcom/knhanes_eGFR/ua_full.v1.csv")
abnormal = df.query('eGFR_ab==1')
normal_sample = df.query('eGFR_ab==0').sample(n=len(abnormal), random_state=SEED)
print(abnormal.shape)
print(normal_sample.shape)

df_sample = pd.concat([normal_sample, abnormal])
print(df_sample.info())

참고 : pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.sample.html

 

pandas.DataFrame.sample — pandas 1.2.1 documentation

Default ‘None’ results in equal probability weighting. If passed a Series, will align with target object on index. Index values in weights not found in sampled object will be ignored and index values in sampled object not in weights will be assigned we

pandas.pydata.org

 


TOP

Designed by 티스토리