Programming

데이터를 3 세트 (트레인, 검증 및 테스트)로 나누는 방법은 무엇입니까?

procodes 2020. 8. 3. 20:43
반응형

데이터를 3 세트 (트레인, 검증 및 테스트)로 나누는 방법은 무엇입니까?


팬더 데이터 프레임이 있으며 3 개의 별도 세트로 나누고 싶습니다. train_test_split from 을 사용 sklearn.cross_validation하면 데이터를 두 세트 (트레인 및 테스트)로 나눌 수 있습니다. 그러나 데이터를 세 세트로 나누는 방법에 대한 해결책을 찾지 못했습니다. 바람직하게는 원본 데이터의 색인을 갖고 싶습니다.

해결 방법은 train_test_split두 번 사용 하고 어떻게 든 색인을 조정하는 것입니다. 그러나 데이터를 2 대신 3 세트로 나누는 더 표준적인 방법이 있습니까?


너피 솔루션. 데이터 세트를 다음 부분으로 분할합니다.

  • 60 %-열차 세트,
  • 20 %-검증 세트
  • 20 %-테스트 세트

In [305]: train, validate, test = np.split(df.sample(frac=1), [int(.6*len(df)), int(.8*len(df))])

In [306]: train
Out[306]:
          A         B         C         D         E
0  0.046919  0.792216  0.206294  0.440346  0.038960
2  0.301010  0.625697  0.604724  0.936968  0.870064
1  0.642237  0.690403  0.813658  0.525379  0.396053
9  0.488484  0.389640  0.599637  0.122919  0.106505
8  0.842717  0.793315  0.554084  0.100361  0.367465
7  0.185214  0.603661  0.217677  0.281780  0.938540

In [307]: validate
Out[307]:
          A         B         C         D         E
5  0.806176  0.008896  0.362878  0.058903  0.026328
6  0.145777  0.485765  0.589272  0.806329  0.703479

In [308]: test
Out[308]:
          A         B         C         D         E
4  0.521640  0.332210  0.370177  0.859169  0.401087
3  0.333348  0.964011  0.083498  0.670386  0.169619

[int(.6*len(df)), int(.8*len(df))]-numpy.split ()indices_or_sections배열입니다 .

다음은 np.split()사용법에 대한 간단한 데모입니다. 20 개 요소 배열을 80 %, 10 %, 10 %로 나눕니다.

In [45]: a = np.arange(1, 21)

In [46]: a
Out[46]: array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20])

In [47]: np.split(a, [int(.8 * len(a)), int(.9 * len(a))])
Out[47]:
[array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16]),
 array([17, 18]),
 array([19, 20])]

노트 :

무작위 세트 생성 시딩을 처리하기 위해 함수가 작성되었습니다. 세트를 무작위 화하지 않는 세트 분할에 의존해서는 안됩니다.

import numpy as np
import pandas as pd

def train_validate_test_split(df, train_percent=.6, validate_percent=.2, seed=None):
    np.random.seed(seed)
    perm = np.random.permutation(df.index)
    m = len(df.index)
    train_end = int(train_percent * m)
    validate_end = int(validate_percent * m) + train_end
    train = df.ix[perm[:train_end]]
    validate = df.ix[perm[train_end:validate_end]]
    test = df.ix[perm[validate_end:]]
    return train, validate, test

데모

np.random.seed([3,1415])
df = pd.DataFrame(np.random.rand(10, 5), columns=list('ABCDE'))
df

enter image description here

train, validate, test = train_validate_test_split(df)

train

enter image description here

validate

enter image description here

test

enter image description here


그러나, 데이터 세트로 분할하는 한 가지 방법 train, test, cv로는 0.6, 0.2, 0.2을 사용하는 것 train_test_split두 방법.

from sklearn.model_selection import train_test_split

x, x_test, y, y_test = train_test_split(xtrain,labels,test_size=0.2,train_size=0.8)
x_train, x_cv, y_train, y_cv = train_test_split(x,y,test_size = 0.25,train_size =0.75)

한 가지 방법은 train_test_split 함수를 두 번 사용하는 것입니다.

from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test 
= train_test_split(X, y, test_size=0.2, random_state=1)

X_train, X_val, y_train, y_val 
= train_test_split(X_train, y_train, test_size=0.25, random_state=1)

train_test_split여러 세트로 나누고 일부 추가 코드를 작성한 후 재 인덱싱을 수행하지 않고 사용 하는 것이 매우 편리 합니다. 위의 가장 좋은 대답은 train_test_split파티션 크기를 변경하지 않고 두 번 분리 해도 처음에 의도 한 파티션을 제공 하지 않는다는 것을 언급하지는 않습니다 .

x_train, x_remain = train_test_split(x, test_size=(val_size + test_size))

Then the portion of validation and test sets in the x_remain change and could be counted as

new_test_size = np.around(test_size / (val_size + test_size), 2)
# To preserve (new_test_size + new_val_size) = 1.0 
new_val_size = 1.0 - new_test_size

x_val, x_test = train_test_split(x_remain, test_size=new_test_size)

In this occasion all initial partitions are saved.

참고URL : https://stackoverflow.com/questions/38250710/how-to-split-data-into-3-sets-train-validation-and-test

반응형