티스토리 뷰

반응형

Stack Overflow에 자주 검색, 등록되는 문제들과 제가 개발 중 찾아 본 문제들 중에서 나중에도 찾아 볼 것 같은 문제들을 정리하고 있습니다.

Stack Overflow에서 가장 먼저 확인하게 되는 가장 높은 점수를 받은 Solution과 현 시점에 도움이 될 수 있는 가장 최근에 업데이트(최소 점수 확보)된 Solution을 각각 정리하였습니다.

 

아래 word cloud를 통해 이번 포스팅의 주요 키워드를 미리 확인하세요.

How do I create test and train samples from one dataframe with pandas?

판다스를 이용해 데이터프레임에서 테스트와 트레인 샘플을 어떻게 생성할 수 있는지 궁금합니다.

 문제 내용 

I have a fairly large dataset in the form of a dataframe and I was wondering how I would be able to split the dataframe into two random samples (80% and 20%) for training and testing.

저는 데이터프레임 형태로 된 상당히 큰 데이터셋이 있고, 이를 80%와 20%의 랜덤한 샘플로 나눠서 훈련과 테스트에 사용하려고 합니다. 이를 어떻게 할 수 있는지 궁금합니다.

 

Thanks!

감사합니다!

 

 

 

 높은 점수를 받은 Solution 

Scikit Learn's train_test_split is a good one. It will split both numpy arrays and dataframes.

Scikit Learn의 train_test_split은 좋은 함수입니다. 이 함수는 numpy 배열과 데이터프레임을 모두 나눌 수 있습니다.
from sklearn.model_selection import train_test_split

train, test = train_test_split(df, test_size=0.2)

 

 

 가장 최근 달린 Solution 

shuffle = np.random.permutation(len(df))
test_size = int(len(df) * 0.2)
test_aux = shuffle[:test_size]
train_aux = shuffle[test_size:]
TRAIN_DF =df.iloc[train_aux]
TEST_DF = df.iloc[test_aux]

 

 

출처 : https://stackoverflow.com/questions/24147278/how-do-i-create-test-and-train-samples-from-one-dataframe-with-pandas

반응형
댓글
공지사항
최근에 올라온 글