2017-03-04 1 views
0

k- 접기 교차 유효성 검사 작업을 위해 접기를 만들기 위해 numpy 배열을 사용하고 싶습니다. 테스트 슬라이스를 추출하는 것은 쉽지만 테스트 슬라이스가 생략 된 나머지 배열을 반환하는 방법을 알 수는 없습니다. 이 작업을 수행하는 효율적인 방법이 있습니까?숫자 검증 배열을 사용하여 교차 유효성 검사를위한 폴드를 생성 할 수 있습니까?

examples = range(50) 
classes = range(50) 
data = np.array(zip(classes,examples)) 
test_slice = data[5:10] 
train_on_remainder = ?? 
+0

슬라이스를 양쪽에 연결하십시오. 결과는 사본이됩니다. – hpaulj

답변

1

당신은 너무처럼 설정할 수 있습니다 :

test_slice, remainder = np.split(data.copy(), [test_size], axis=0) 
# run test 
remainder[:test_size], test_slice = test_slice, remainder[:test_size].copy() 
# run test 
remainder[test_size:2*test_size], test_slice = test_slice, remainder[test_size:2*test_size].copy() 

# etc. 

난 당신이 훨씬 덜 복사와 함께 할 수 있다고 생각하지 않습니다.

작동 원리 :

.  full set:   | 0 | 1 | 2 | 3 | 4 | 5 | 
     split (full copy)  /\ 
     tst/rem   | 0 |  | 1 | 2 | 3 | 4 | 5 | 
     run trial 
          | 1 | 2 | 3 | 4 | 5 | 
     swap tst and   ^| 
     first segment:   | v 
     (partial copy)  | 0 | 

     tst/rem   | 1 |  | 0 | 2 | 3 | 4 | 5 | 
     run trial 
          | 0 | 2 | 3 | 4 | 5 | 
     swap tst and    ^| 
     second segment:   | v 
     (partial copy)   | 1 | 

     tst/rem   | 2 |  | 0 | 1 | 3 | 4 | 5 | 
     run trial 
          | 0 | 1 | 3 | 4 | 5 | 
     swap tst and     ^| 
     third segment:     | v 
     (partial copy)    | 2 | 

등이 거의 그대로 배를 이동하고 볼 수있다. 전체 사본을 많이 저장하는 중입니다.

1

일반적으로 sklearn의 train_test_split()을 사용할 수 있다면 이상한 질문이옵니다.

편집 : 또 다른 방법은 잘 모르겠어요

r = np.arange(len(data)) 
trainX = data[r < 5 | r > 10] 

효율적인 해결책이 될 수 있지만 은 지능형리스트를 사용하여 인덱서를 구축하려고 할 수 있습니다. 물론

def indx(n, test_slice): 
    return [x for x in range(n) if, x not in test_slice] 

test_slice = set(range(5, 10)) 
trainX = data[indx(len(data), test_slice))] 

당신에게 sklearn의 train_test_split() 사용 가능한 경우 같은 것을해야한다.

0
split = np.vsplit(data, np.array([5,10])) 

'''This will give you a list with 3 elements''' 

test_slice = split[1] 
train_slice = np.vstack((split[0],split[2])) 

[5 5] [6 6] [7 7] [8 8] [9]

[0 0] [1] [2] [3 3] [4] [10] [11 11] [12] [12] [15] [12] [13] [14] [15] [16] 48 (48)] [49 49]

0

두 가지 방식, 1 차원 어레이의 증명 :

In [64]: data = np.arange(20) 
In [65]: test = data[5:10] 
In [66]: rest = np.concatenate((data[:5],data[10:]),axis=0) 
In [67]: rest 
Out[67]: array([ 0, 1, 2, 3, 4, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]) 
In [68]: 
In [68]: mask = np.zeros(data.shape[0], dtype=bool) 
In [69]: mask[5:10] = True 
In [70]: test = data[mask] 
In [71]: rest = data[~mask] 
In [72]: rest 
Out[72]: array([ 0, 1, 2, 3, 4, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]) 
,691 363,210

In [75]: np.delete(data, np.arange(5,10)) 
Out[75]: array([ 0, 1, 2, 3, 4, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]) 

내부는 I 입증 두 가지 방법 중 하나를 사용하는 함수 np.delete있다.

관련 문제