2017-11-20 7 views
1

LOOCV (교차 유효성 검사 중 하나를 두십시오)을 사용하고있는 작은 데이터 세트를 가지고 있으므로 sklearn에 있습니다.sklearn loocv.split이 작은 테스트와 열 배열을 예상보다 많이 반환했습니다.

"Number of labels=41 does not match number of samples=42" : 내 분류를 실행하면

나는 다음과 같은 오류가 발생했습니다.

나는 다음과 같은 코드를 사용하여 테스트 및 훈련 세트를 생성 :

otu_trans = test_train.transpose() 
# transpose otu table 
merged = pd.concat([otu_trans, metadata[status]], axis=1, join='inner') 
# merge phenotype column from metadata file with transposed otu table 

X = merged.drop([status],axis=1) 

# drop status from X 
y = merged[status] 


encoder = LabelEncoder() 
y = pd.Series(encoder.fit_transform(y), 
index=y.index, name=y.name) 
# convert T and TF lables to 0 and 1 respectively 

loocv = LeaveOneOut() 
loocv.get_n_splits(X) 

for train_index, test_index in loocv.split(X): 
    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] 
    print(X_train, X_test, y_train, y_test) 

input data

내가 X_train 나는 그것이 있어야 생각대로 42,41보다는 41,257입니다 X_test의 모양을 확인할 때, 따라서 데이터가 잘못된 축을 따라 분할 된 것처럼 보입니다.

아무도 왜 이런 일이 일어 났는지 설명 할 수 있습니까?

당신에게 모든

+0

더 자세한 정보를 표시 할 수 있습니까? – pissall

+0

@adamsorbie in X 얼마나 많은 샘플과 변수가 있습니까?초기 X 행렬의 모양은 무엇입니까? y 행렬에 대한 동일한 질문 – sera

+0

42 샘플 및 257 피쳐가 있습니다. 즉 X.shape는 42,257을 반환하고 y.shape는 처음에 42를 반환합니다. 이 경우 X.shape를 분할 한 후 (41,257) 맞습니까? – adamsorbie

답변

0

먼저 감사, 초기 매트릭스 X는 전혀 영향을받지됩니다. 이것은 인덱스를 생성하고 데이터를 분할하는 데에만 사용됩니다.

초기 X의 모양은 항상 동일합니다.

import numpy as np 
from sklearn.model_selection import LeaveOneOut 

# I produce fake data with same dimensions as yours. 
#fake data 
X = np.random.rand(41,257) 
#fake labels 
y = np.random.rand(41) 

#Now check that the shapes are correct: 
X.shape 
y.shape 

이 당신에게 줄 것이다 : 지금

(41, 257) 
(41,) 

분할 :

loocv = LeaveOneOut() 
loocv.get_n_splits(X) 

for train_index, test_index in loocv.split(X): 
    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] 

    #classifier.fit(X_train, y_train) 
    #classifier.predict(X_test) 


X_train.shape 
X_test.shape 

이제

, 여기 LOOCV spliting를 사용하여 간단한 예입니다 이것은 인쇄 :

(40, 257) 
(1, 257) 

당신이 볼 수 있듯이, X_train40 샘플을 포함하고 X_test1 샘플이 포함되어 있습니다. 이것은 LOOCV 분할을 사용하기 때문에 정확합니다.

초기 X 행렬에는 총 42 개의 샘플이 있으므로 훈련을 위해 41 개를, 테스트를 위해 1 개를 사용합니다.

이 루프는 많은 X_trainX_test 매트릭스를 생성합니다. 구체적으로 말하자면, N 행렬을 생성 할 것입니다. N = number of samples (우리의 경우 : N = 41)입니다.

Nloocv.get_n_splits(X)과 동일합니다.

희망 하시겠습니까?

+0

감사합니다! 불행히도 X_train.shape을 실행할 때 (40,257)가 아니라 (42, 41)을 얻습니다. 이것을 실행하기 전에 데이터가 바뀌었을 가능성이 있습니까? 물론 – adamsorbie

+0

! 이게 문제 야 !! 조 변경하지 마십시오. X 행렬은 [샘플 수, 변수 수/특징]이어야합니다. – sera

+0

아, 그 다음 간단한 해결책이 있습니다. 고마워요! – adamsorbie