데이터분석/전처리

train_test_split 데이터 나누기

씩씩한 IT블로그 2022. 1. 8. 18:03
반응형

train_test_split

from sklearn.model_selection import train_test_split

데이터셋을 나눠주는 train_test_split 에 대해서 알아본다.

 

옵션값

x_train, x_valid, y_train, y_valid = train_test_split(train_x, train_y, test_size=0.2, shuffle=True, stratify=train_y, random_state=42)

1. test_size : test 사이즈의 비율

2. shuffle : split하기 전 섞을것인지

3. stratify : class label의 비율을 맞춰서 split할것인지

4. random_state : 실행할 때 마다 똑같이 섞을것인지 다르게 섞을것인지

 

파라미터 별 결과

1. 섞지않고 위에서 부터 비율만큼 끊기

(shuffle = False)

위에서부터 상위 80%를 train, 나머지 20%를 test 으로 나누기

x_train, x_valid, y_train, y_valid = train_test_split(train_x, train_y, test_size=0.2, shuffle=False)

display(x_train.index)
display(x_valid.index)

 

2.  매번 새롭게 섞어서 뽑기

(shuffle = True)

매번 새롭게 섞어서 나누기

for i in range(2):
    print(i+1," 번째 섞기")
    x_train, x_valid, y_train, y_valid = train_test_split(train_df.문장, train_df.label, test_size=0.2, shuffle=True)
    display(x_train.index)
    display(x_valid.index)
 

 

3.  매번 똑같이 섞어서 뽑기

( shuffle=True, random_state=42(아무숫자) )

하이퍼파라미터 튜닝같은 것을 할 때, 같은 데이터로 비교해야 하는 경우 사용한다.

매번 섞어도 같은 순서로 나눈다.

for i in range(2):
    print(i+1," 번째 섞기")
    x_train, x_valid, y_train, y_valid = train_test_split(train_df.문장, train_df.label, test_size=0.2, shuffle=True, random_state=42)
    display(x_train.index)
    display(x_valid.index)

반응형