예시문제 작업형 2유형 질문있습니다
import pandas as pd
train = pd.read_csv("data/customer_train.csv")
test = pd.read_csv("data/customer_test.csv")
pd.set_option('display.max_columns',None)
# EDA
# print(train.shape, test.shape)
# print(train.describe(include='O'))
# print(test.describe(include='O'))
# print(train.isnull().sum()) # 결측치: 환불금액
# print(train.isnull().sum())
# print(train.info())
# print(train.describe(include = 'O'))
# print(sorted(list(train['주구매상품'].unique())))
# print(sorted(list(test['주구매상품'].unique())))
a = set(train['주구매상품'].unique())
b = set(test['주구매상품'].unique())
print(a - b)
print(b - a)
# 데이터 전처리
train['환불금액'] = train['환불금액'].fillna(train['환불금액'].mean())
test['환불금액'] = test['환불금액'].fillna(test['환불금액'].mean())
# print(train.isnull().sum())
# print(test.isnull().sum())
# 원핫인코딩
df = pd.concat([train,test])
df = pd.get_dummies(df)
train = df.iloc[:len(train)]
test = df.iloc[len(train):]
print(train.shape, test.shape)
# 검증데이터 분리
from sklearn.model_selection import train_test_split
x_tr,x_val,y_tr, y_val = train_test_split(train.drop('성별',axis =1), train['성별'], test_size=0.2, random_state=2022)
# print(x_tr.shape,x_val.shape,y_tr.shape, y_val.shape)
# 모델 및 평가
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import roc_auc_score
model = RandomForestClassifier(random_state = 2022, max_depth=4, n_estimators=500)
model.fit(x_tr, y_tr)
pred_proba = model.predict_proba(x_val)
print(roc_auc_score(y_val, pred_proba[:,1]))
# # 0.6759
# # print(test.shape)
# # print(test.isnull().sum())
# 예측 및 제출
pred_proba = model.predict_proba(test)
train이 test를 포함할 경우 train + test 를 합쳐서 원핫인코딩을 진행했습니다.
모델 및 평가까지는 에러 없이 진행하였는 데 test 데이터를 예측하는 pred_proba = model.predict_proba(test) 부분에서 아래와 같은 에러가 발생했습니다. 어떤게 문제였는지 궁금하여 질문글 올립니다.

답변 1
0
NaN 값 그러니깐 결측치를 포함한 데이터를 모델에 넣었어요
데이터를 합치고 분리하고나면 샘플을 확인해보세요
train에는 성별이 있고, test는 성별이 없어요!
합치면 성별이 없는 데이터에는 NaN이 들어가고 다시 분리 시켜도 성별 컬럼이 남아 있습니다.
test 데이터에서 target(성별) 컬럼 제거(drop)가 필요해요
작업형2 모의고사3 자료 업데이트 요청
0
1
1
수강 연장가능여부 문의드립니다!
0
17
1
수강기간 연장 문의드립니다!
0
23
1
최빈값
0
21
2
수강 가이드라인
0
20
2
수강 연장 가능 여부 문의
0
40
2
강의 구매했는데 교재 필수일까요
0
27
2
수강기간 연장 가부 문의드립니다.
0
25
2
수강기간 연장이 가능한지 양해를 구하며 문의를 드립니다.
0
32
2
작업형2 모의문제2에서 drop이랑 인코딩
0
41
3
인코딩을 다 꼭 해봐야하는지
0
43
2
안녕하세요? 강사님 질문있습니다.(인코딩관련)
0
58
2
이진분류에서 양성 클래스가 0일 때 평가 방법
0
46
2
원핫인코딩 전 train과 test 병합여부
0
51
2
수강기간 연장
0
69
2
29강 작업2형의 이상치 처리
0
67
2
수강기간 연장 문의드립니다!
0
65
2
9월에 실기출제기준 개정내역에 신설된부분
0
56
1
작업형2 모의문제2 결과값 관련 문의드립니다.
0
67
2
12회차 시험 실패 후 13회 도전 예정입니다.
0
57
1
random_state 값
0
73
2
강의 연장 문의 드립니다.
0
68
2
강의 연장 문의
0
61
2
오징어게임
0
71
1





