比赛链接:金融风控-贷款违约预测
因为这是一个金融风控专题的数据挖掘实战,在开始之前先引入一些预备知识。
1.预备知识
1.1预测指标
本次竞赛用AUC作为评价指标,AUC为ROC曲线下与坐标轴围成的面积大小。
分类算法常见的评估指标
-
混淆矩阵(confused matrix)
1.若一个实例为正类,预测也为正类,则为真正类TP(True Positive) 2.若一个实例为正类,预测也为负类,则为假负类FN(False Negative) 3.若一个实例为负类,预测也为正类,则为假正类FP(False Positive) 4.若一个实例为负类,预测也为负类,则为真负类TN(True Negative)
-
准确率(accuracy):不适用于样本不均衡的情况 a c c u r a c y = T P + T N T P + T N + F P + F N accuracy=\frac{TP+TN}{TP+TN+FP+FN} accuracy=TP+TN+FP+FNTP+TN
-
精确率(Precision):也被称为查准率, P r e c i s i o n = T P T P + F P Precision = \frac{TP}{TP+FP} Precision=TP+FPTP
-
召回率(Recall): R e c a l l = T P T P + F N Recall=\frac{TP}{TP+FN} Recall=TP+FNTP
-
F1-Score:精确率和召回率是相互影响的两个指标,如果精确率上升(下降)则召回率下降(上升)。若要兼顾二者的话,引入了 F 1 − S c o r e = 2 1 P r e c i s i o n + 1 R e c a l l F1-Score = \frac{2}{\frac{1}{Precision} + \frac{1}{Recall}} F1−Score=Precision1+Recall12
-
P-R曲线(Precision-Recall Curve):描述精确率和召回率变化的曲线
-
ROC(Receiver Operating characteristic)
引入假正率 F P R = F P F P + T N FPR=\frac{FP}{FP+TN} FPR=FP+TNFP和真正率 T P R = T P T P + F N TPR=\frac{TP}{TP+FN} TPR=TP+FNTP,ROC曲线就是以FPR为x轴,TPR为y轴的曲线。 -
AUC(Area under Curve):ROC曲线下与坐标轴围成的面积。
金融风控预测类常见的评估指标如下:
-
K-S曲线(Kolmogorov-Smirnov),K-S曲线与ROC曲线类似,不同在于:
ROC曲线将真正例率和假正例率作为横纵轴,而K-S曲线将真正例率和假正例率都作为纵轴,横轴则由选定的阈值来充当。
公式如下:
K
S
=
m
a
x
(
T
P
R
−
F
P
R
)
KS=max(TPR-FPR)
KS=max(TPR−FPR)
KS不同代表的不同情况,一般情况KS值越大,模型的区分能力越强,但是也不是越大模型效果就越好,如果KS过大,模型可能存在异常,所以当KS值过高可能需要检查模型是否过拟合。以下为KS值对应的模型情况,但此对应不是唯一的,只代表大致趋势。
KS | 模型能力 |
---|---|
[0,0.2) | 模型差,不具备区分的能力 |
[0.2,0.3) | 模型有一定的区分能力,勉强能用 |
[0.3,0.5) | 模型好,有很强的区分能力 |
>0.75 | 模型可能有异常(考虑是否过拟合) |
1.2代码示例
这一部分主要是对数据的提取和之前提到的一些评价指标用代码来做一些示例,方便大家来理解这些指标。
1.2.1利用pandas包进行数据提取
import pandas as pd
train_data = pd.read_csv("train.csv")
testA_data = pd.read_csv("testA.csv")
print("train_data's shape:",train_data.shape)
print("testA_data's shape:",testA_data.shape)
# train_data's shape: (800000, 47)
# testA_data's shape: (200000, 48)
train_data.head(10)
# 10 rows × 47 columns
1.2.2分类指标评价计算的示例
- 混淆矩阵
import numpy as np
from sklearn.metrics import confusion_matrix
#y的预测值
y_pred=[1,1,0,1]
#y的真实值
y_true=[1,1,0,0]
print("混淆矩阵:\n",confusion_matrix(y_true,y_pred))
#混淆矩阵:
# [[1 1]
# [0 2]]
- accuracy,Precision,Recall,F1-Score
from sklearn import metrics
y_pred=[1,1,0,1]
y_true=[1,1,0,0]
print("accuray:",metrics.accuracy_score(y_true,y_pred)) #accuracy
print("Precision:",metrics.precision_score(y_true,y_pred)) #Precision
print("Recall:",metrics.recall_score(y_true,y_pred)) #Recall
print("F1-Score:",metrics.f1_score(y_true,y_pred)) #F1-score
#accuray: 0.75
#Precision: 0.66
#Recall: 1.0
#F1-Score: 0.8
- P-R曲线
import matplotlib.pyplot as plt
from sklearn.metrics import precision_recall_curve
%matplotlib inline
y_pred = [0, 1, 1, 0, 1, 1, 0, 1, 1, 1]
y_true = [0, 1, 1, 0, 1, 0, 1, 1, 0, 1]
precison,recall,thresholds = precision_recall_curve(y_true,y_pred)
plt.plot(precison,recall)
# [<matplotlib.lines.Line2D at 0x7fe0a58dc390>]
4. ROC曲线,AUC及K-S
# Roc曲线
from sklearn.metrics import roc_curve
y_pred = [0, 1, 1, 0, 1, 1, 0, 1, 1, 1]
y_true = [0, 1, 1, 0, 1, 0, 1, 1, 0, 1]
FPR,TPR,thresholds = roc_curve(y_true,y_pred)
plt.title('Roc')
plt.plot(FPR,TPR,'b')
plt.plot([0,1],[0,1],'r--')
plt.xlabel("TPR")
plt.ylabel('FPR')
# Text(0, 0.5, 'FPR')
# Auc
from sklearn.metrics import roc_auc_score
y_true = [0,0,1,1]
y_scores = [0.1,0.4,0.35,0.8]
Auc = roc_auc_score(y_true,y_scores)
print('Auc:',Auc)
# Auc:0.75
#K-S max(FPR-TPR)
y_pred = [0, 1, 1, 0, 1, 1, 0, 1, 1, 1]
y_true = [0, 1, 1, 0, 1, 0, 1, 1, 1, 1]
FPR,TPR,thresholds = roc_curve(y_true,y_pred)
KS = abs(FPR-TPR).max()
print('KS:',KS)
# KS: 0.5238095238095237