본문 바로가기
Python notes/Python Errors

ValueError 발생) Found input variables with inconsistent numbers of samples: [501, 500] 해결하기

by 성실한 나무 2022. 1. 10.

#1. cesium 패키지의 데이터로 시계열 데이터 분석 중에 에러 발생!

 cesium 패키지의 eeg 데이터로 feature를 추출해서 시계열 데이터를 분석하려던 참이었다. 그런데 그동안 한번도 에러가 나지 않았던 scikit learn의 train_test_split 함수에서 에러가 발생하는 것이 아닌가.

 

ValueError) Found input variables with inconsistent numbers of samples: [501, 500]

--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-140-e0783aea8e3b> in <module> 1 from sklearn.model_selection import train_test_split ----> 2 X_train, X_test, y_train, y_test = train_test_split(fset.values, eeg['classes']) /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/sklearn/model_selection/_split.py in train_test_split(test_size, train_size, random_state, shuffle, stratify, *arrays) 2170 raise ValueError("At least one array required as input") 2171 -> 2172 arrays = indexable(*arrays) 2173 2174 n_samples = _num_samples(arrays[0]) /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/sklearn/utils/validation.py in indexable(*iterables) 354 """ 355 result = [_make_indexable(X) for X in iterables] --> 356 check_consistent_length(*result) 357 return result 358 /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/sklearn/utils/validation.py in check_consistent_length(*arrays) 317 uniques = np.unique(lengths) 318 if len(uniques) > 1: --> 319 raise ValueError("Found input variables with inconsistent numbers of" 320 " samples: %r" % [int(l) for l in lengths]) 321 ValueError: Found input variables with inconsistent numbers of samples: [501, 500]

내가 작성했던 코드는 아래의 간단하디 간단한 train_test_split인데 뭐 이리도 긴 ValueError가 나타났을까?

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(fset.values, eeg['classes'])

 

 

#2. 알고보니...

 결과적으로 이 에러의 원인은 X, y의 데이터셋 shape의 불일치가 문제였다.

fset.values와 eeg['classes']의 shape을 print로 확인해 보니 과연 데이터의 개수가 맞지 않았다. fset.values를 csv 파일로 불러오는 과정에서 필요없는 데이터 한줄이 삭제되지 않고 딸려들어온 것이 이번 에러의 원인이었다.

 과연 필요없는 데이터 한줄을 지우고 X, y의 shape을 일치시킨 후, train_test_split을 하니 아주 잘 데이터가 나누어졌다.

 

ValueError) Found input variables with inconsistent numbers of samples: [501, 500]

 

이 에러의 의미는 X데이터 셋의 shape은 501이고, y 데이터셋의 shape은 500이니 둘의 데이터 개수(행의 개수)를 맞추어라 라는 뜻이었다. 오늘의 에러 노트 끝.

 

댓글