인프런 커뮤니티 질문&답변

jnood님의 프로필 이미지

작성한 질문수

[퇴근후딴짓] 빅데이터 분석기사 실기 (작업형1,2,3)

2회 기출유형(작업형2)

2회 기출유형 작업형 2 id 값 오류 질문 드립니다.

해결된 질문

23.06.18 16:40 작성

·

213

0

# 랜덤포레스트
from sklearn.ensemble import RandomForestClassifier
rf=RandomForestClassifier(random_state=2023)
rf.fit(X_tr,y_tr)
pred=rf.predict_proba(X_val)
pred=rf.predict_proba(X_test)

pd.DataFrame({'ID':X_test['ID'],'Reached.on.Time_Y.N':pred[:,1]}).to_csv('dddd.csv',Index=False)
위 코드로 실행 시 , 아래 ID값에 대한 오류가 나와 오류 해석 및 해결방법 문의드립니다,,
KeyError                                  Traceback (most recent call last)
/usr/local/lib/python3.10/dist-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
   3801             try:
-> 3802                 return self._engine.get_loc(casted_key)
   3803             except KeyError as err:


4 frames


pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: 'ID'
The above exception was the direct cause of the following exception:

KeyError                                  Traceback (most recent call last)
/usr/local/lib/python3.10/dist-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
   3802                 return self._engine.get_loc(casted_key)
   3803             except KeyError as err:
-> 3804                 raise KeyError(key) from err
   3805             except TypeError:
   3806                 # If we have a listlike key, _check_indexing_error will raise

KeyError: 'ID'

답변 1

0

퇴근후딴짓님의 프로필 이미지
퇴근후딴짓
지식공유자

2023. 06. 18. 17:44

X_test를 한번 출력해 보세요! ID 컬럼이 없는 것 같네요!

위에X_test id 관련해서 pop이나 drop을 사용한 적은 없나요?

jnood님의 프로필 이미지

작성한 질문수

질문하기