import pandas as pd
import numpy as np

X_train = pd.read_csv('../input/hr-data-predict-change-jobscompetition-form/X_train.csv')
X_test = pd.read_csv('../input/hr-data-predict-change-jobscompetition-form/X_test.csv')
y_train = pd.read_csv('../input/hr-data-predict-change-jobscompetition-form/y_train.csv')
X_train.info()
X_train.shape
"""
결측치가 상당히 많이 존재하는데 이를 어떻게 처리할지 하나씩 살펴봄
"""
X_train.isnull().sum()
X_train
X_train.gender.unique() # 성별 null??
X_train.enrolled_university.unique() # null값은 no_enrollment로 
X_train.major_discipline.unique() # null 값을 non_graduate
X_train.experience.unique() # nan은 0으로 채워야겠다
X_train.company_size.unique() # nan은 ''<10'
X_train.company_type.unique() # Others로 채우자
X_train.last_new_job.unique() # 'never'로 가야할듯?
X_train.education_level.unique() # 초등학교로 채워야할듯
X_train.gender = X_train.gender.fillna('Other')
X_train.enrolled_university = X_train.enrolled_university.fillna('no_enrollment')
X_train.major_discipline = X_train.major_discipline.fillna('No Major')
X_train.experience = X_train.experience.fillna('0')
X_train.company_size = X_train.company_size.fillna('<10')
X_train.company_type = X_train.company_type.fillna('Other')
X_train.last_new_job = X_train.last_new_job.fillna('never')
X_train.education_level = X_train.education_level.fillna('Primary School')
y_train = y_train[['target']]
"""
적절히 잘 채워진것을 볼 수 있음. 맞게 채운지는 모름
"""
"""
범주형 변수들을 연속형 변수로 바꾸어보자
"""
X_train.drop('enrollee_id',axis=1, inplace=True)
X_train.info()
X_train.gender = X_train.gender.astype('category').cat.codes
X_train.enrolled_university = X_train.enrolled_university.astype('category').cat.codes
X_train.major_discipline = X_train.major_discipline.astype('category').cat.codes
X_train.experience = X_train.experience.astype('category').cat.codes
X_train.company_size = X_train.company_size.astype('category').cat.codes
X_train.company_type = X_train.company_type.astype('category').cat.codes
X_train.last_new_job = X_train.last_new_job.astype('category').cat.codes
X_train.education_level = X_train.education_level.astype('category').cat.codes
X_train.relevent_experience = X_train.relevent_experience.astype('category').cat.codes
X_train.city = X_train.city.astype('category').cat.codes
"""
훈련데이터, 검증데이터 홀드아웃/
분류이므로 stratify=y_train 층화추출/
random_state 고정
"""
from sklearn.model_selection import train_test_split
X_train_val, X_test_val, y_train_val, y_test_val = train_test_split(X_train, y_train, stratify=y_train, random_state=42)
"""
XGBClassifier 적용
"""
from xgboost import XGBClassifier
model = XGBClassifier(n_estimators = 400, learning_rate = 0.1, max_depth = 3, min_samples_leaf =3, min_samples_split =4)
model.fit(X_train_val, y_train_val)
pred_test = model.predict(X_test_val)
model.score(X_test_val, y_test_val)
"""
roc_auc_score 확인
"""
"""
roc_auc_score(y_test(검증용데이터셋), pred_proba(검증용 데이터셋으로 예측한 확률))
"""
pred_proba = model.predict_proba(X_test_val)[:,1]
from sklearn.metrics import roc_auc_score
roc_auc_score(y_test_val, pred_proba)
"""
roc_auc_score = 0.81
"""
X_test.gender = X_test.gender.fillna('Other')
X_test.enrolled_university = X_test.enrolled_university.fillna('no_enrollment')
X_test.major_discipline = X_test.major_discipline.fillna('No Major')
X_test.experience = X_test.experience.fillna('0')
X_test.company_size = X_test.company_size.fillna('<10')
X_test.company_type = X_test.company_type.fillna('Other')
X_test.last_new_job = X_test.last_new_job.fillna('never')
X_test.education_level = X_test.education_level.fillna('Primary School')

X_test.gender = X_test.gender.astype('category').cat.codes
X_test.enrolled_university = X_test.enrolled_university.astype('category').cat.codes
X_test.major_discipline = X_test.major_discipline.astype('category').cat.codes
X_test.experience = X_test.experience.astype('category').cat.codes
X_test.company_size = X_test.company_size.astype('category').cat.codes
X_test.company_type = X_test.company_type.astype('category').cat.codes
X_test.last_new_job = X_test.last_new_job.astype('category').cat.codes
X_test.education_level = X_test.education_level.astype('category').cat.codes
X_test.relevent_experience = X_test.relevent_experience.astype('category').cat.codes
X_test.city = X_test.city.astype('category').cat.codes
X_test.drop('enrollee_id',axis=1, inplace=True)
"""
X_test도 동일하게 전처리 적용
"""
"""
X_test로 예측하기 및 데이터 프레임으로 변경 후 X_test와 합쳐서 submission으로 만들기
"""
pred_test_test = model.predict(X_test)
pred_real = pd.DataFrame(pred_test_test, columns=['pred'])
submission = pd.concat([X_test, pred_real],axis=1)
submission
"""
제출용 코드 작성
"""
submission.to_csv('20211122.csv',index=False)
"""
올바르게 제출 되었는지 확인
"""
df = pd.read_csv('20211122.csv')
df.head()