2014-04-08 2 views
1

나는 가느 다란 배열이 있습니다. 이 배열의 무작위 샘플을 교차 검증을위한 테스트 및 교육 세트로 선택하고 싶습니다. 교육 세트로서, 나는 항목 idx를 선택하여 조각을 사용합니다. 이 항목의 칭찬을 선택하는 방법이 있습니까? 즉 idx에없는 모든 항목.울프 슬라이스 :이 것들을 제외한 모든 항목

# N: size of numpy array a. 
idx = random.sample(np.arange(N),N/10) # select random sample 
train(a[idx]) # train on this random sample 
test(a[ NOT idx]) # test on the rest. 

마지막 줄에 대해 압축 방식으로 나머지 항목을 호출하는 방법은 무엇입니까? 감사합니다. .

+0

'시험 ([np.setdiff1d (np.arange (N), IDX는)])'도 작동 – askewchan

답변

3

당신이 idx 부울 배열을 만드는 경우에, 당신은 ~idx와 보수를 선택할 수 있습니다

import numpy as np 

N = len(a) 
idx = np.zeros(N, dtype='bool') 
idx[np.random.choice(np.arange(N), size=N/10, replace=False)] = True 
train(a[idx]) # train on this random sample 
test(a[~idx]) # test on the rest. 
관련 문제