xgboost 최근 버전에서 생긴 오류

Theo Kim·2022년 12월 16일
0

xgboost 지도학습 중 생긴 오류

start_time = time.time()
xgb = XGBClassifier(n_estimators=400, learning_rate=0.1, max_depth=3)
xgb.fit(X_train.values, y_train)

print('Fit time : ', time.time() - start_time)

That happens because the class column has to start from 0 (as required since version 1.3.2). An easy way to solve that is using LabelEncoder from sklearn.preprocssing library.

해결 방법

from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()
y_train = le.fit_transform(y_train)

코드를 실행하여 y_train의 label을 새로 encoding 한 뒤 다시 학습 코드를 실행하니 해결되었다!

confusion_matrix 및 accuracy_score 조회 시 주의 사항

다시 le.inverser_transform으로 변환 후 실행해줘야 한다!!

from sklearn.metrics import confusion_matrix, accuracy_score
y_pred = xgb.predict(X_test.values)
y_pred = le.inverse_transform(y_pred)
cm = confusion_matrix(y_test, y_pred)
print(cm)
accuracy_score(y_test, y_pred)

출처: https://stackoverflow.com/questions/71996617/invalid-classes-inferred-from-unique-values-of-y-expected-0-1-2-3-4-5-got

profile
THEO's velog

0개의 댓글