[python] 교차 검증을 위해 데이터 세트를 훈련 및 테스트 데이터 세트로 분할 / 분할하는 방법은 무엇입니까?

NumPy 배열을 훈련 및 테스트 / 검증 데이터 세트로 무작위로 분할하는 좋은 방법은 무엇입니까? Matlab 의 cvpartition또는 crossvalind함수 와 유사한 것 .



답변

데이터 세트를 두 개로 한 번 분할하려면을 사용 numpy.random.shuffle하거나 numpy.random.permutation인덱스를 추적해야하는 경우 :

import numpy
# x is your dataset
x = numpy.random.rand(100, 5)
numpy.random.shuffle(x)
training, test = x[:80,:], x[80:,:]

또는

import numpy
# x is your dataset
x = numpy.random.rand(100, 5)
indices = numpy.random.permutation(x.shape[0])
training_idx, test_idx = indices[:80], indices[80:]
training, test = x[training_idx,:], x[test_idx,:]

교차 검증을 위해 동일한 데이터 세트반복적으로 분할하는 방법에는 여러 가지가 있습니다 . 한 가지 전략은 반복하여 데이터 세트에서 리샘플링하는 것입니다.

import numpy
# x is your dataset
x = numpy.random.rand(100, 5)
training_idx = numpy.random.randint(x.shape[0], size=80)
test_idx = numpy.random.randint(x.shape[0], size=20)
training, test = x[training_idx,:], x[test_idx,:]

마지막으로 sklearn 에는 여러 교차 검증 방법 (k-fold, leave-n-out, …)이 포함되어 있습니다. 또한 학습 및 테스트 세트에 동일한 비율의 긍정 및 부정 예제가 있는지 확인하기 위해 일부 기능과 관련하여 균형 잡힌 데이터 분할을 생성하는 고급 “층화 된 샘플링” 방법 도 포함 됩니다.


답변

scikit-learn을 사용하는 또 다른 옵션이 있습니다. 으로 scikit의 위키 설명 , 당신은 다음과 같은 지침을 사용할 수 있습니다 :

from sklearn.model_selection import train_test_split

data, labels = np.arange(10).reshape((5, 2)), range(5)

data_train, data_test, labels_train, labels_test = train_test_split(data, labels, test_size=0.20, random_state=42)

이렇게하면 학습 및 테스트로 분할하려는 데이터의 레이블을 동기화 상태로 유지할 수 있습니다.


답변

메모입니다. 학습, 테스트 및 유효성 검사 세트를 원하는 경우 다음을 수행 할 수 있습니다.

from sklearn.cross_validation import train_test_split

X = get_my_X()
y = get_my_y()
x_train, x_test, y_train, y_test = train_test_split(X, y, test_size=0.3)
x_test, x_val, y_test, y_val = train_test_split(x_test, y_test, test_size=0.5)

이러한 매개 변수는 훈련에 70 %를 제공하고 테스트 및 값 세트에 각각 15 %를 제공합니다. 도움이 되었기를 바랍니다.


답변

으로 sklearn.cross_validation모듈이 사용되지 않습니다, 당신은 사용할 수 있습니다 :

import numpy as np
from sklearn.model_selection import train_test_split
X, y = np.arange(10).reshape((5, 2)), range(5)

X_trn, X_tst, y_trn, y_tst = train_test_split(X, y, test_size=0.2, random_state=42)


답변

훈련 및 테스트 세트로 계층화 된 분할을 고려할 수도 있습니다. Startified Division은 훈련 및 테스트 세트를 무작위로 생성하지만 원래 클래스 비율이 유지되는 방식으로 생성됩니다. 이렇게하면 학습 및 테스트 세트가 원본 데이터 세트의 속성을 더 잘 반영합니다.

import numpy as np

def get_train_test_inds(y,train_proportion=0.7):
    '''Generates indices, making random stratified split into training set and testing sets
    with proportions train_proportion and (1-train_proportion) of initial sample.
    y is any iterable indicating classes of each observation in the sample.
    Initial proportions of classes inside training and
    testing sets are preserved (stratified sampling).
    '''

    y=np.array(y)
    train_inds = np.zeros(len(y),dtype=bool)
    test_inds = np.zeros(len(y),dtype=bool)
    values = np.unique(y)
    for value in values:
        value_inds = np.nonzero(y==value)[0]
        np.random.shuffle(value_inds)
        n = int(train_proportion*len(value_inds))

        train_inds[value_inds[:n]]=True
        test_inds[value_inds[n:]]=True

    return train_inds,test_inds

y = np.array([1,1,2,2,3,3])
train_inds,test_inds = get_train_test_inds(y,train_proportion=0.5)
print y[train_inds]
print y[test_inds]

이 코드는 다음을 출력합니다.

[1 2 3]
[1 2 3]


답변

이 작업을 수행하기 위해 내 프로젝트에 대한 함수를 작성했습니다 (하지만 numpy를 사용하지 않음).

def partition(seq, chunks):
    """Splits the sequence into equal sized chunks and them as a list"""
    result = []
    for i in range(chunks):
        chunk = []
        for element in seq[i:len(seq):chunks]:
            chunk.append(element)
        result.append(chunk)
    return result

청크를 무작위로 만들려면 목록을 전달하기 전에 섞으십시오.


답변

다음은 계층화 된 방식으로 데이터를 n = 5 접기로 분할하는 코드입니다.

% X = data array
% y = Class_label
from sklearn.cross_validation import StratifiedKFold
skf = StratifiedKFold(y, n_folds=5)
for train_index, test_index in skf:
    print("TRAIN:", train_index, "TEST:", test_index)
    X_train, X_test = X[train_index], X[test_index]
    y_train, y_test = y[train_index], y[test_index]