반응형
In [14]:
from IPython.core.display import display, HTML
display(HTML("<style>.container { width:1200px !important; }</style>"))
산탄데르 고객 만족 예측¶
데이터 링크 https://www.kaggle.com/c/santander-customer-satisfaction/data
data preprocessing¶
In [ ]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib
cust_df = pd.read_csv("./santander-customer-satisfaction/train_santander.csv")
print('dataset shape : ', cust_df.shape)
cust_df.head(3)
In [ ]:
cust_df.info()
이진분류의 경우 target의 분포가 균일한지 확인할 필요가 있음.¶
In [ ]:
print(cust_df['TARGET'].value_counts())
unsatisfied_cnt = cust_df[cust_df['TARGET'] ==1]['TARGET'].count()
total_cnt = cust_df['TARGET'].count()
print(f'unsatisfied 비율은 {unsatisfied_cnt/total_cnt:.2f}')
In [ ]:
cust_df.describe()
value -999999를 가장 많이 있는 2로 replace¶
In [ ]:
print(cust_df['var3'].value_counts()[:10])
In [ ]:
# var3 피처 값 대체 및 ID 피처 드롭
cust_df['var3'].replace(-999999,2,inplace=True)
cust_df.drop('ID', axis = 1, inplace = True)
#피처 세트와 레이블 세트 분리. 레이블 컬럼은 DataFrame의 맨 마지막에 위치해 컬럼 위치 -1로 분리
X_features = cust_df.iloc[:, :-1]
y_labels = cust_df.iloc[:, -1]
print(f'피처 데이터 shape:{X_features.shape}')
불균형한 데이터는 학습 /테스트에 균일하게 들어가줘야한다.¶
- train_test_split의 stratify=y_labels 추가.
In [ ]:
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X_features, y_labels,
test_size = 0.2, random_state = 0, stratify = y_labels)
train_cnt = y_train.count()
test_cnt = y_test.count()
print(f'학습 세트 Shape: { X_train.shape}, 테스트 세트 Shape:{X_test.shape}')
print('학습 세트 레이블 값 분포 비율')
print(y_train.value_counts() / train_cnt)
print('\n 테스트 세트 레이블 값 분포 비율')
print(y_test.value_counts()/test_cnt)
In [ ]:
from xgboost import XGBClassifier
from sklearn.metrics import roc_auc_score
# n_estimators는 500으로, random state는 예제 수행 시마다 동일 예측 결과를 위해 설정.
xgb_clf = XGBClassifier(n_estimators = 500, random_state =156)
#성능 평가 지표를 auc로, 조기 중단 파라미터는 100으로 설정하고 학습 수행.
xgb_clf.fit(X_train, y_train, early_stopping_rounds = 100,
eval_metric ="auc", eval_set = [(X_train, y_train), (X_test, y_test)])
xgb_roc_score = roc_auc_score(y_test, xgb_clf.predict_proba(X_test)[:,1],average='macro')
print(f'ROC AUC : {xgb_roc_score}')
하이퍼파라미타 튜닝을 한다고해서 성능이 드라마틱하게 상승하진 않음.¶
In [ ]:
from sklearn.model_selection import GridSearchCV
#하이퍼 파라미터 테스트의 수행 속도를 향상시키기 위해 n_estimators를 100으로 감소
xgb_clf = XGBClassifier(n_estimators=100)
params = {
'max_depth' : [5,7],
'min_child_weight' :[1,3],
'colsample_bytree' : [0.5, 0.75]
}
#하이퍼파라미터 테스트의 수행 속도를 향상 시키기 위해 cv를 시정하지 않음.
gridcv = GridSearchCV(xgb_clf, param_grid=params)
gridcv.fit(X_train, y_train, early_stopping_rounds = 30, eval_metric = 'auc',
eval_set = [(X_train,y_train), (X_test, y_test)])
print('GridSearchCV 최적 파라미터 : ', gridcv.best_params_)
xgb_roc_score = roc_auc_score(y_test, gridcv.predict_proba(X_test)[:,1], average='macro')
print(f'ROC AUC:{xgb_roc_score}')
In [ ]:
# n_estimators는 1000으로 증가시키고, learning_rate=0.02로 감소, reg_alpha=0.03으로 추가함.
xgb_clf = XGBClassifier(n_estimators=1000, random_state=156, learning_rate=0.02, max_depth=5,\
min_child_weight=1, colsample_bytree=0.75, reg_alpha=0.03)
# evaluation metric을 auc로, early stopping은 200 으로 설정하고 학습 수행.
xgb_clf.fit(X_train, y_train, early_stopping_rounds=200,
eval_metric="auc",eval_set=[(X_train, y_train), (X_test, y_test)])
xgb_roc_score = roc_auc_score(y_test, xgb_clf.predict_proba(X_test)[:,1],average='macro')
print('ROC AUC: {0:.4f}'.format(xgb_roc_score))
In [ ]:
from xgboost import plot_importance
import matplotlib.pyplot as plt
%matplotlib inline
fig, ax = plt.subplots(1,1,figsize=(10,8))
plot_importance(xgb_clf, ax=ax , max_num_features=20,height=0.4)
LightGBM 모델 학습과 하이퍼 파라미터 튜닝¶
In [ ]:
from lightgbm import LGBMClassifier
lgbm_clf = LGBMClassifier(n_estimators = 500)
evals = [(X_test, y_test)]
lgbm_clf.fit(X_train, y_train, early_stopping_rounds=100, eval_metric = 'auc', eval_set = evals, verbose =True)
lgbm_roc_score = roc_auc_score(y_test, lgbm_clf.predict_proba(X_test)[:,1], average='macro')
print(f'ROC AUC: {lgbm_roc_score:.4f}')
In [ ]:
from sklearn.model_selection import GridSearchCV
# 하이퍼 파라미터 테스트의 수행 속도를 향상시키기 위해 n_estimators를 100으로 감소
LGBM_clf = LGBMClassifier(n_estimators=200)
params = {'num_leaves': [32, 64 ],
'max_depth':[128, 160],
'min_child_samples':[60, 100],
'subsample':[0.8, 1]}
# 하이퍼 파라미터 테스트의 수행속도를 향상 시키기 위해 cv 를 지정하지 않습니다.
gridcv = GridSearchCV(lgbm_clf, param_grid=params)
gridcv.fit(X_train, y_train, early_stopping_rounds=30, eval_metric="auc",
eval_set=[(X_train, y_train), (X_test, y_test)])
print('GridSearchCV 최적 파라미터:', gridcv.best_params_)
lgbm_roc_score = roc_auc_score(y_test, gridcv.predict_proba(X_test)[:,1], average='macro')
print('ROC AUC: {0:.4f}'.format(lgbm_roc_score))
In [ ]:
lgbm_clf = LGBMClassifier(n_estimators=1000, num_leaves=32, sumbsample=0.8, min_child_samples=100,
max_depth=128)
evals = [(X_test, y_test)]
lgbm_clf.fit(X_train, y_train, early_stopping_rounds=100, eval_metric="auc", eval_set=evals,
verbose=True)
lgbm_roc_score = roc_auc_score(y_test, lgbm_clf.predict_proba(X_test)[:,1],average='macro')
print('ROC AUC: {0:.4f}'.format(lgbm_roc_score))
하드웨어가 좋으면 하이퍼파라미터 튜닝시간이 적게걸리니 해도 좋지만
그렇지 않다면 그리 추천하지 않음. 차라리 피쳐엔지니어링에 시간을 쏟을 것.¶
반응형
'인공지능 > Merchine_Learning' 카테고리의 다른 글
[파이썬 머신러닝 완벽가이드] 협업필터링 (0) | 2020.12.21 |
---|---|
[파이썬 머신러닝 완벽가이드] 추천시스템 - 콘텐츠기반 (1) | 2020.12.21 |
[파이썬 머신러닝 완벽가이드] Ch7 Clustering 내용 (0) | 2020.11.23 |
[파이썬 머신러닝 완벽가이드] Ch.5 Regression (0) | 2020.11.14 |
[파이썬 머신러닝 완벽가이드] Ch4. Classification (0) | 2020.11.10 |