๋ชฉํ
- Counfusion Matrix์ ๋ํ ์ดํด ๋ฐ ํด์
- ์ ๋ฐ๋, ์ฌํ์จ์ ์ดํดํ๊ณ ์ฌ์ฉ
- ROC Curve, AUC score์ ๋ํ ์ดํด
feature engineering ํ
def engineer(df):
"""ํน์ฑ์ ์์ง๋์ด๋ง ํ๋ ํจ์์
๋๋ค."""
# ์๋ก์ด ํน์ฑ์ ์์ฑํฉ๋๋ค.
behaviorals = [col for col in df.columns if 'behavioral' in col]
df['behaviorals'] = df[behaviorals].sum(axis=1) # 'behavioral'์ด๋ผ๋ ๋จ์ด๊ฐ ํฌํจ๋ column์ ๋ํด์ sumํ ์๋ก์ด ๋ณ์ ๋ง๋ค๊ธฐ
# ๊ณ์ ๋
๊ฐ(seas)์ ๋ํ ๋ชจ๋ธ์ ํ์ตํ ๊ฒ์ด๊ธฐ ๋๋ฌธ์ h1n1์ ๋ํ ํน์ฑ์ ์ ๊ฑฐํฉ๋๋ค.
dels = [col for col in df.columns if ('employment' in col or 'h1n1' in col)]
df.drop(columns=dels, inplace=True) # 'employment'์ 'h1n1'์ด ๋ค์ด๊ฐ column์ drop
return df
Confusion Matrix
- ๋ถ๋ฅ ๋ชจ๋ธ์ ์ฑ๋ฅ ํ๊ฐ ์งํ๋ฅผ ํ์ธํ ์ ์์ต๋๋ค.
from sklearn.metrics import plot_confusion_matrix
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
pcm = plot_confusion_matrix(pipe, X_val, y_val,
cmap=plt.cm.Blues,
ax=ax);
plt.title(f'Confusion matrix, n = {len(y_val)}', fontsize=15)
plt.show()
- ํํ ์ฐ๋ฆฌ๊ฐ ์๋ ๊ธ์ (True, 1)์ด ์ข์ธก ์๋จ์ ์กด์ฌํ๋ ๊ฒฝ์ฐ์ ํผ๋ํ์ง ์๋๋ก ํด์ผํฉ๋๋ค.(์ถ์ ์ ํ์ธํ ๊ฒ)
cm = pcm.confusion_matrix
cm # ์์ ์ฌ์ง์์ confusion matrix ํ
์ด๋ธ๋ง ๊ฐ์ ธ์ค๊ธฐ
TP = cm[1][1]
TN = cm[0][0]
FP = cm[0][1]
FN = cm[1][0]
correct_predictions = np.diag(cm).sum() # np.diag๋ ๋๊ฐ์์๋ค๋ก ํ๋ ฌ์ ๋ง๋๋ ๊ฒ
correct_predictions # ์ฆ, TP + TN ์
๋๋ค. (์์ธก๊ฐ๊ณผ ์ค์ ๊ฐ์ด ์ผ์นํ๋ ๊ฒฝ์ฐ์
๋๋ค.)
total_predictions = cm.sum()
total_predictions
classification accuracy = correct_predictions/total_predictions
# ๋ถ๋ฅ ์ ํ๋๋ accuracy_score(y_val, y_pred)์ ๊ฐ์ ๊ฐ์ ๊ฐ์ต๋๋ค.
Precision(์ ๋ฐ๋), Recall(์ฌํ์จ ; Sensitivity), F1 score
- ์ ๋ฐ๋(Precision) : Positive๋ก ์์ธกํ ๊ฒฝ์ฐ ์ค Positive๋ฅผ ๋ง์ถ ๋น์จ
$$Precision = \frac{TP}{TP + FP}$$
- ์ฌํ์จ(Recall, Sensitivity) : ์ค์ Positive์ธ ๊ฒฝ์ฐ ์ค Positive๋ฅผ ๋ง์ถ ๋น์จ
$$Recall(Sensitivity) = \frac{TP}{TP + FN}$$
- F1 ์ ์(F1 score) : ์ ๋ฐ๋์ ์ฌํ์จ์ ์กฐํํ๊ท (harmonic mean)
$$F1 score = 2\cdot\frac{precision\cdot recall}{precision + recall}$$
์ฐธ๊ณ
F beta score
$$β = \frac{Recall}{Precision}$$
๋ค๋ฃจ๋ ๋ฌธ์ ์ ๋ฐ๋ผ ์ด๋ค ํ๊ฐ์งํ๋ฅผ ์ฐ์ ์ ํด์ผํ๋์ง ํ๋จํด์ผํฉ๋๋ค.
- ๋ณ์์์ ์ด๊ธฐ ์์ง๋จ์ ํ๋ ๊ฒฝ์ฐ
- ์์ด ์๋ค๊ณ ์์ธกํ๋๋ฐ ์๋ ๊ฒฝ์ฐ(FP)๋ณด๋ค ์์ด ์๋ค๊ณ ์์ธกํ๋๋ฐ ์๋ ๊ฒฝ์ฐ(FN)์ด ๋ ์น๋ช ์ ์ด๋ฏ๋ก ์ฌํ์จ์ ํ๊ฐ์งํ๋ก ์ฌ์ฉํด์ผํ ๊ฒ์ ๋๋ค.
- ์คํธ ๋ฉ์ผ์ ๊ตฌ๋ถํ๋ ๊ฒฝ์ฐ
- ์คํธ ๋ฉ์ผ์ด ์๋ ๊ฒ์ผ๋ก ๋ถ๋ฅํ๋๋ฐ, ์คํธ ๋ฉ์ผ์ธ ๊ฒฝ์ฐ(FN)๋ณด๋ค ์คํธ ๋ฉ์ผ๋ก ๋ถ๋ฅํ๋๋ฐ, ์คํธ ๋ฉ์ผ์ด ์๋ ๊ฒฝ์ฐ(FP)๊ฐ ๋ ์น๋ช ์ ์ด๋ฏ๋ก ์ ๋ฐ๋๊ฐ ํ๊ฐ์งํ๋ก ๋ ์ ์ฉํ ๊ฒ์ ๋๋ค.
- ๋ทํ๋ฆญ์ค ์ํ ์ถ์ฒ์ ๊ฒฝ์ฐ
- ์ข์ํ์ง ์๋ ์ํ๋ผ๊ณ ์์ธกํ๋๋ฐ, ์ข์ํ๋ ์ํ์ธ ๊ฒฝ์ฐ(FN)(์ ํํ ์ด ๊ฒฝ์ฐ๋ ์ฐ๋ฆฌ๊ฐ ์์์ฑ ์ ์์ต๋๋ค. ์ข์ํ์ง ์๋ ์ํ๋ผ๊ณ ์์ธกํ๋ฉด ์ถ์ฒ์ ๋จ์ง ์์ํ ๋)๋ณด๋ค ์ข์ํ๋ ์ํ๋ผ๊ณ ์์ธกํ๋๋ฐ, ์ข์ํ๋ ์ํ๊ฐ ์๋ ๊ฒฝ์ฐ(FP)๊ฐ ์ถ์ฒ์์คํ ์ ๋ํ ์ ๋ขฐ๋์ ๋ ์ํฅ์ ์ค ๊ฒ์ ๋๋ค.
์ฌ๋ฌ ํ๊ฐ์งํ ํ๋ฒ์ ํ์ธํ๊ธฐ
from sklearn.metrics import classification_report
print(classification_report(y_val, y_pred))
Threshold(์๊ณ์น)
- ์๊ณ์น๋ฅผ ์ด๋ป๊ฒ ์ค์ ํ๋๋์ ๋ฐ๋ผ 0๊ณผ 1์ ๊ตฌ๋ถ์ง๋ ๊ธฐ์ค์ด ๋ฌ๋ผ์ง๊ฒ ๋ฉ๋๋ค.(๊ฐ์ ๋ง)
# ๋ชจ๋ธ์์ ๋๋ฆด ๋ ์์
threshold = 0.5
y_pred_proba = pipe.predict_proba(X_val)[:, 1]
y_pred = y_pred_proba > threshold
- ์ด๋ฐ ์๊ณ์น๋ฅผ ํ๋์ ๋ณด๊ณ ์ฐพ์์ ์ด์ฉํ ์ ์๋ ๋ฐฉ๋ฒ์ด ๋ฐ๋ก ROC curve๋ฅผ ๊ทธ๋ฆฌ๊ณ AUC score
ROC curve ๋ฐ AUC
- ROC curve : ์ฌ๋ฌ ์๊ณ๊ฐ์ ๋ฐ๋ฅธ TPR๊ณผ FPR์ ๊ทธ๋ํ๋ฅผ ๋ณด์ฌ์ค๋๋ค.
- ์ฌํ์จ์ ๋์ด๊ธฐ ์ํด์๋ Positive๋ก ํ๋จํ๋ ์๊ณ๊ฐ์ ๊ณ์ ๋ฎ์ถ์ด ๋ชจ๋ Positive๋ก ํ๋จํ๊ฒ ๋ง๋ค๋ฉด ๋ฉ๋๋ค. ๊ทธ๋ฌ๋ ์ด๋ฌ๋ฉด ๋์์ Negative์ด์ง๋ง Positive๋ก ํ๋จํ๋ ์์์ฑ๋ฅ (FPR)๋ ํจ๊ป ์ฆ๊ฐํฉ๋๋ค.
- ์ฌํ์จ์ ์ต๋ํํ๊ณ ์์์ฑ๋ฅ ์ ์ต์ํํ๋ ์๊ณ๊ฐ์ด ์ต์ ์ ์๊ณ๊ฐ
- AUC : Area Under the Curve ์ฆ, ROC curve์ ์๋ ๋ฉด์ ์ ๋งํฉ๋๋ค.
- ์ธ์ดํท๋ฐ์์ roc_curve ํ์ฉ ์์
from sklearn.metrics import roc_curve
# roc_curve(ํ๊ฒ๊ฐ, prob of 1)
fpr, tpr, thresholds = roc_curve(y_val, y_pred_proba)
roc = pd.DataFrame({
'FPR(Fall-out)': fpr,
'TPRate(Recall)': tpr,
'Threshold': thresholds
})
roc
plt.scatter(fpr, tpr)
plt.title('ROC curve')
plt.xlabel('FPR(Fall-out)')
plt.ylabel('TPR(Recall)');
# threshold ์ต๋๊ฐ์ ์ธ๋ฑ์ค, np.argmax()
optimal_idx = np.argmax(tpr - fpr)
optimal_threshold = thresholds[optimal_idx]
print('idx:', optimal_idx, ', threshold:', optimal_threshold)
idx: 256 , threshold: 0.4633333333333334
y_pred_optimal = y_pred_proba >= optimal_threshold # ์ด๋ ๊ฒ threshold๋ฅผ ์ ์ฉ
from sklearn.metrics import roc_auc_score # AUC_score ๊ตฌํ๋ ๋ฐฉ๋ฒ
auc_score = roc_auc_score(y_val, y_pred_proba)
auc_score # negative์ positive๊ฐ ๋ ์ ๊ตฌ๋ถ๋์ด์๋ค๋ ์๋ฏธ์ผํ
๋!
์ถ๊ฐ ํ
ROC curve๋ ์ด์ง๋ถ๋ฅ๋ฌธ์ ์์ ์ฌ์ฉํ ์ ์์ต๋๋ค. ๋ค์ค๋ถ๋ฅ๋ฌธ์ ์์๋ ๊ฐ ํด๋์ค๋ฅผ ์ด์งํด๋์ค ๋ถ๋ฅ๋ฌธ์ ๋ก ๋ณํ(One Vs All)ํ์ฌ ๊ตฌํ ์ ์์ต๋๋ค.
- 3-class(A, B, C) ๋ฌธ์ -> A vs (B,C), B vs (A,C), C vs (A,B) ๋ก ๋๋์ด ์ํ
๋ถ๋ฅ๋ฌธ์ ์์ ๋ชจ๋ธ์ ์ฌ๋ฐ๋ฅด๊ฒ ํ๊ฐํ๊ธฐ ์ํด์๋ ์ ํ๋ ์ธ์๋ ์ ๋ฐ๋, ์ฌํ์จ ์ ์ ์ดํดํ๊ณ ์ฌ์ฉํด์ผ ํ๋ค๋ ๊ฒ์ ์์์ต๋๋ค. ํนํ ๊ฐ ๋ฒ์ฃผ๋ฅผ ์์ธกํ๋ ๊ธฐ์ค์ด ๋๋ ์๊ณ๊ฐ์ ์์น์ ๋ฐ๋ผ ์ ๋ฐ๋๋ ์ฌํ์จ์ด ๋ฌ๋ผ์ง๊ธฐ ๋๋ฌธ์ ๋ฌธ์ ์ ์ํฉ์ ๋ฐ๋ผ ์ ์ ํ ์๊ณ๊ฐ์ ์ ํํ ํ์์ฑ์ด ์์ต๋๋ค. ์ด์ง ๋ถ๋ฅ๋ฌธ์ ์์๋ ROC curve์ AUC ์ ์๋ฅผ ์ ํ์ฉํ๋ฉด ์ข์ ๊ฒฐ๊ณผ๋ฅผ ๋ง๋ค์ด๋ผ ์ ์์ต๋๋ค.
'๐ฟ Data > ๋ถํธ์บ ํ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[TIL]32.Section2 Sprint2 Chall(Sprint2 ํค์๋ ์ค์ฌ ์ ๋ฆฌ) (0) | 2021.12.31 |
---|---|
[TIL]31.Model Selection(๋ชจ๋ธ ์ ํ) (0) | 2021.12.30 |
[TIL]29.RandomForest(๋๋คํฌ๋ ์คํธ) (0) | 2021.12.27 |
[TIL]28.Decision Tree(์์ฌ๊ฒฐ์ ๋๋ฌด) (0) | 2021.12.26 |
[TIL]27.Section2_sprint1 challenge (0) | 2021.12.24 |