기계 학습을위한 Python에서 데이터 전처리가 처음입니다. 일부 데이터를 사전 처리하려고합니다. 데이터는 빈 항목과 NaN 항목이있는 많은 범주 형 변수로 구성됩니다. ffil 메서드를 사용하여 빈 또는 NaN 공간을 채우는 중 sklearn LabelEncoder를 사용하여 레이블 인코딩을 한 후 나중에 한 번만 인코딩하면됩니다. 두 개의 다른 코드 세그먼트가 있습니다. 라벨 인코딩을하는 것은 오류를 제공하지 않습니다하지만 여기서 첫 번째는 간단 조금 나는 위에서 언급 한 오류가 발생하고 몇 addintional 프로세스가 두 번째 경우 : 첫 번째 코드 섹션TypeError : 'str'과 'float'인스턴스간에 추가 작업이 추가되면 '>'이 (가) 지원되지 않습니다.
encoder = LabelEncoder()
for cols in train.keys():
if(cols.startswith('cat')):
train[cols].fillna(method='ffill', inplace=True)
train[cols].fillna(method='bfill', inplace=True)
if train[cols].dtype == 'object':
train[cols] = encoder.fit_transform(train[cols])
train = pd.get_dummies(data=train, columns=[cols])
두 번째 코드 섹션 :
를encoder = LabelEncoder()
best_fit = SelectKBest(score_func=chi2, k=10)
for cols in train.keys():
if(cols.startswith('cat')):
train[cols].fillna(method='ffill', inplace=True)
train[cols].fillna(method='bfill', inplace=True)
if train[cols].dtype == 'object':
train[cols] = encoder.fit_transform(train[cols])
train_temp = pd.get_dummies(data=train, columns=[cols])
temp_df = train_temp[list(set(train_temp.keys())-set(train.keys()))]
fit_temp = best_fit.fit(temp_df, target)
features_temp = fit_temp.transform(temp_df)
train = train.drop([cols], axis=1)
train = pd.concat([train, pd.DataFrame(features_temp)], axis = 1, join='outer')
역 추적 로그 :
Traceback (most recent call last):
File "<ipython-input-1-4efe4593ba69>", line 37, in <module>
train[cols] = encoder.fit_transform(train[cols])
File "C:\Continuum\anaconda3\lib\site-packages\sklearn\preprocessing\label.py", line 112, in fit_transform
self.classes_, y = np.unique(y, return_inverse=True)
File "C:\Continuum\anaconda3\lib\site-packages\numpy\lib\arraysetops.py", line 211, in unique
perm = ar.argsort(kind='mergesort' if return_index else 'quicksort')
TypeError: '>' not supported between instances of 'str' and 'float'
문제가 해결 : 은 내부 에러처럼 보인다. 기계를 다시 시작하고 스크립트를 다시 실행하면 문제가 해결됩니다.
질문 제목에 넣은 오류 메시지에 대해 질문하는 경우 질문에 전체 추적을 포함하고 게시 한 소스 행을 나타냅니다. – Duncan
@Duncan이 로그를 추가했습니다. 시정 해줘서 고마워. –