기계학습 (Machine Learning) : 컴퓨터가 데이터간의 관계를 학습하여, 새로운 수식(Model)을 도출하는 작업
기계학습의 핵심 Point :
기계학습의 3요소:
import pandas as pd
import matplotlib as mpl
import plotly.express as px
import scipy.stats as stats
import numpy as np
mpl.rc('font', family = 'AppleGothic')
df1 = pd.read_csv('01_Data.csv')
1. 지도 학습(Supervised Learning) : 목표변수(Y)와 설명변수(X)의 관계를 학습해서, 새로운 X가 들어올 때, Y를 예측/분류
- 회귀 (Regression - Y 연속형) : 특정 값을 최대한 가깝게 예측하는 것이 목적
- 분류 (Classification - Y 범주형) : 특정 항목인지 아닌지를 정확하게 분류하는 것이 목적(평가 지표들이 많다!)
- Ex) 고객 해약 예측 (정상 / 해악) / 스팸메세지 분류기 (스팸/정상) / 주가 예측 (회사의 정보 -> 주가) ...
2. 비지도학습(Unsupervised Learning) : 설명변수(X) 데이터 간의 수학적 거리/ 유사성 / 상관성 등을 이용하여, 비슷한 유형의 데이터를 묶거나 연관있는 데이터를 찾거나, 데이터의 차원을 줄이는 등의 학습 기법
- 군집분석 (Clustering) : 비슷한 위치 또는 가가운 위치의 (특성이 비슷한) 데이터를 묶어주는 학습 기법
- 차원축소 : 특정 데이터의 항목 수 (Columns)를 줄이는 기법
- 연관분석 : 특정 데이터의 유사한 항목을 찾아주는 기법
Ex) 장바구니 분석 / 추천 시스템 ...
3. 강화학습(Reinforcement Learning) : 컴퓨터 시뮬레이션을 통해 사용자가 설정한 환경에 대해, 적절한 보상이 주어지는 방향으로 학습을 수행
- 데이터가 없이도 학습이 가능
- 게임 AI (알파고)
1. 데이터 핸들링 (데이터를 병합 / 파생 변수 생성 / 데이터 선택 / 이상치 제거 /...)
2. 학습의 목표변수(Y)와 설명변수(X)를 설정
- 유의 사항 : 사용되는 X 설명변수는 새로 들어올 데이터에 대한 값으로 설정
3. 학습 데이터(Train Set)와 검증 데이터(Test Set)를 분할
- 검증 데이터(Test Set)는 절대로 학습에 참여하지 않는다!
- (교차검증 기법에서 Validation Set은 학습에 참여함 <-> Test Set과 별개!)
4. 학습 수행
- 특성공학 (Feature Engineering) : 학습에 맞게 데이터를 처리
- 스케일링 / 인코딩 / 교차검증 / 변수선택법 ...
- 학습
5. 학습 된 모델의 성능을 평가
- 학습 능력
- 일반화 능력
6. 적용(새로운 데이터 입력 / 파일형태로 저장)
✔︎ 'State' 컬럼의 unique 값 확인하기
df1['State'].unique()
>> array(['계약확정', '기간만료', '해약확정', '해약진행중'], dtype=object)
✔︎ 'State'컬럼을 매핑하여 '해약여부' 파생변수 생성
df1['해약여부'] = df1['State'].replace({'계약확정':0, '기간만료':1,
'해약확정':1, '해약진행중':1})
df1['해약여부'].value_counts()
>> 해약여부
0 50620
1 681
Name: count, dtype: int64
✔︎ 결측치 제거 (Y값에는 결측치가 있으면 안됨)
df2 = df1.dropna()
Y = df2['해약여부']
X = df2[['Age', 'Credit_Rank', 'Amount_Month', 'Term']]
from sklearn.model_selection import train_test_split
X_train, X_test, Y_train, Y_test = train_test_split(X,Y,
random_state = 1234)
from sklearn.tree import DecisionTreeClassifier
model = DecisionTreeClassifier()
model.fit(X_train, Y_train)
>> DecisionTreeClassifier()
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
Y_train_pred = model.predict(X_train)
Y_test_pred = model.predict(X_test)
from sklearn.metrics import accuracy_score
✔︎ 학습 능력 평가
accuracy_score(Y_train, Y_train_pred)
✔︎ 일반화 능력 평가
accuracy_score(Y_test, Y_test_pred)
x1 = input('고객 연령을 입력하시오 : ')
x2 = input('고객 신용 등급을 입력하시오 : ')
x3 = input('계약할 월 랜탈비용을 입력하시오 : ')
x4 = input('계약할 기간을 입력하시오 : ')
>> 고객 연령을 입력하시오 : 29
고객 신용 등급을 입력하시오 : 3
계약할 월 랜탈비용을 입력하시오 : 100000
계약할 기간을 입력하시오 : 12
✔︎ 해약 : 1 / 정상 : 0
input_data = pd.DataFrame([[x1,x2,x3,x4]], columns=X.columns)
model.predict(input_data)
✔︎ 생성된 모델을 파일 형태로 저장
import pickle
pickle.dump(model, open('model.sav', 'wb'))
from sklearn.metrics import precision_score, recall_score
print('학습 데이터의 Precision : ', precision_score(Y_train, Y_train_pred))
print('검증 데이터의 Precision : ', precision_score(Y_test, Y_test_pred))
>> 학습 데이터의 Precision : 0.9787234042553191
검증 데이터의 Precision : 0.0
print('학습 데이터의 Recall : ', recall_score(Y_train, Y_train_pred))
print('검증 데이터의 Recall : ', recall_score(Y_test, Y_test_pred))
>> 학습 데이터의 Recall : 0.11219512195121951
검증 데이터의 Recall : 0.0
from sklearn.metrics import classification_report
print(classification_report(Y_train, Y_train_pred))
>> precision recall f1-score support
0 0.99 1.00 0.99 30075
1 0.98 0.11 0.20 410
accuracy 0.99 30485
macro avg 0.98 0.56 0.60 30485
weighted avg 0.99 0.99 0.98 30485
pd.DataFrame(model.predict_proba(X_test))
>>
0 1
0 0.980392 0.019608
1 1.000000 0.000000
2 1.000000 0.000000
3 1.000000 0.000000
4 1.000000 0.000000
... ... ...
10157 0.909091 0.090909
10158 0.976378 0.023622
10159 0.500000 0.500000
10160 1.000000 0.000000
10161 1.000000 0.000000
10162 rows × 2 columns
from sklearn.preprocessing import Binarizer
customer_threshold = 0.15
Binarizer(threshold = customer_threshold)
>> Binarizer(threshold=0.15)
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
predict_proba_col = model.predict_proba(X_test)[:, 1].reshape(-1,1)
custom_bin = Binarizer(threshold=customer_threshold).fit(predict_proba_col)
Y_test_pred2 = custom_bin.transform(predict_proba_col) #15%를 기준으로 해약을 분류
print(classification_report(Y_test, Y_test_pred2))
>> precision recall f1-score support
0 0.99 0.98 0.98 10029
1 0.07 0.12 0.09 133
accuracy 0.97 10162
macro avg 0.53 0.55 0.54 10162
weighted avg 0.98 0.97 0.97 10162
from sklearn.metrics import precision_recall_curve
Y_test_proba1 = model.predict_proba(X_test)[:, 1]
precision1, recall1, threshold1 = precision_recall_curve(Y_test, Y_test_proba1)
threshold1
>> array([0. , 0.00719424, 0.00735294, 0.00869565, 0.00961538,
0.01204819, 0.01219512, 0.01265823, 0.01282051, 0.01298701,
0.01315789, 0.01333333, 0.01351351, 0.01388889, 0.01428571,
0.01694915, 0.01724138, 0.01818182, 0.01851852, 0.01923077,
0.01960784, 0.02 , 0.02040816, 0.0212766 , 0.02173913,
0.02222222, 0.02362205, 0.02380952, 0.02439024, 0.025 ,
0.02542373, 0.02564103, 0.02631579, 0.02702703, 0.02777778,
0.02941176, 0.03030303, 0.03125 , 0.03225806, 0.03333333,
0.03389831, 0.03448276, 0.0375 , 0.03846154, 0.04 ,
0.04761905, 0.05 , 0.05084746, 0.05128205, 0.05263158,
0.05405405, 0.05555556, 0.05714286, 0.05882353, 0.06060606,
0.0625 , 0.06666667, 0.07692308, 0.08333333, 0.09090909,
0.09375 , 0.1 , 0.10526316, 0.11111111, 0.125 ,
0.14285714, 0.15384615, 0.16666667, 0.18181818, 0.2 ,
0.21428571, 0.22222222, 0.25 , 0.28571429, 0.3 ,
0.30769231, 0.33333333, 0.375 , 0.4 , 0.5 ,
0.66666667, 1. ])
df_cm_curve = pd.DataFrame()
df_cm_curve['threshold'] = threshold1
df_cm_curve['precision'] = precision1[:-1]
df_cm_curve['recall']= recall1[:-1]
p1 = df_cm_curve.melt(id_vars=['threshold'])
px.line(p1, x='threshold', y='value', color='variable')

from sklearn.metrics import roc_curve
Y_test_proba1
>> array([0.01960784, 0. , 0. , ..., 0.5 , 0. ,
0. ])
fprs, tprs, threshold2 = roc_curve(Y_test, Y_test_proba1)
df_roc = pd.DataFrame()
df_roc['FPR'] = fprs
df_roc['TPR'] = tprs
df_roc['Threshold'] = threshold2
import plotly.graph_objects as go
fig1 = px.line(df_roc, x='FPR', y='TPR')
fig1.add_trace(go.Scatter(x=[0,1], y=[0,1], mode='lines'))

AUC (Area Under the Curve)
from sklearn.metrics import roc_auc_score
print('학습 AUC : ', roc_auc_score(Y_train, Y_train_pred))
print('검증 AUC : ', roc_auc_score(Y_test, Y_test_pred))
>> 학습 AUC : 0.5536660238485673
검증 AUC : 0.510552641896278
R^2 (결정계수) : 변동을 기반으로 예측성능을 평가하는 지표/ 회귀 모델이 데이터를 얼마나 잘 설명하고 있는가 (0 ~ 1)
MSE (Mean Squared Error) : 실제값과 예측값의 차이를 제곱해 평균을 계산
MAE (Mean Absolute Error) : 실제값과 예측값의 차이에 절댓값의 평균을 계산