Task01 赛题理解
总数据量超过 120w,包含47列变量信息,其中15列为匿名变量。从80万条作为训练集,20 万条作为测试集A,20万条作为测试集B,同时会对employmentTitle、purpose、postCode和title等信息进行脱敏。
一、 预测指标 AUC
1. 混淆矩阵(Confuse Matrix)
TP、FN、FP、TN
(1)若一个实例是正类,并且被预测为正类,即为真正类TP(True Positive)
(2)若一个实例是正类,但是被预测为负类,即为假负类FN(False Negative)
(3)若一个实例是负类,但是被预测为正类,即为假正类FP(False Positive)
(4)若一个实例是负类,并且被预测为负类,即为真负类TN(True Negative)
2. 准确率(Accuracy)
Accuracy=(TP+TN)/(TP+TN+FP+FN)
3. 精确率(Precision)
又称查准率,正确预测为正样本(TP)占预测为正样本(TP+FP)的百分比。
4. 召回率(Recall)
又称为查全率,正确预测为正样本(TP)占正样本(TP+FN)的百分比。
5. F1 Score
F1 Score=2/(1/Precision+1/Recall)
6. P-R曲线(Precision-Recall Curve)
横轴为Recall,纵轴为Precision
7. ROC(Receiver Operating Characteristic)
假正例率(FPR)定义为 X 轴,真正例率(TPR)定义为 Y 轴.
TPR:在所有实际为正例的样本中,被正确地判断为正例之比率.
FPR:在所有实际为负例的样本中,被错误地判断为正例之比率.
8. AUC(Area Under Curve)
被定义为 ROC曲线下与坐标轴围成的面积.
在0.5-1之间, AUC越接近1.0, 检测方法真实性越高.
9. KS(Kolmogorov-Smirnov)
K-S曲线将真正例率和假正例率都作为纵轴,横轴则由选定的阈值来充当.
KS = max(TPR - FPR)
KS值[0.3,0.5],模型具有较强的区分能力.
二、赛题流程![在这里插入图片描述](https://img-blog.csdnimg.cn/20200915121329561.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2hvb2thaGg=,size_16,color_FFFFFF,t_70#pic_center)
三、代码示例
1. pandas数据读取
import pandas as pd
train = pd.read_csv('train.csv')
testA = pd.read_csv('testA.csv')
2. 分类指标评价计算实例
a. 混淆矩阵
import numpy as np
from sklearn.metrics import confusion_matrix
y_pred = [0, 1, 0, 1]
y_true = [0, 1, 1, 0]
print('混淆矩阵:\n',confusion_matrix(y_true, y_pred))
混淆矩阵: [[1 1] [1 1]]
b. accuracy
from sklearn.metrics import accuracy_score y_pred = [0, 1, 0, 1]
y_true = [0, 1, 1, 0] print('ACC:',accuracy_score(y_true, y_pred))
ACC: 0.5
c. Precision,Recall,F1-score
from sklearn import metrics
y_pred = [0, 1, 0, 1]
y_true = [0, 1, 1, 0] print('Precision',metrics.precision_score(y_true, y_pred)) print('Recall',metrics.recall_score(y_true, y_pred)) print('F1-score:',metrics.f1_score(y_true, y_pred))
Precision 0.5
Recall 0.5
F1-score: 0.5
d. P-R曲线
import matplotlib.pyplot as plt
from sklearn.metrics import precision_recall_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]
precision, recall, thresholds = precision_recall_curve(y_true, y_pred) plt.plot(precision, recall)
e. 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.ylabel('TPR')
plt.xlabel('FPR')
f. AUC
import numpy as np
from sklearn.metrics import roc_auc_score
y_true = np.array([0, 0, 1, 1])
y_scores = np.array([0.1, 0.4, 0.35, 0.8]) print('AUC socre:',roc_auc_score(y_true, y_scores))
AUC socre: 0.75
g. KS值
# 在实际操作时往往使用ROC曲线配合求出KS值 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, 1, 1] FPR,TPR,thresholds=roc_curve(y_true, y_pred) KS=abs(FPR-TPR).max()
print('KS值:',KS)
KS值: 0.5238095238095237
四、评分卡
评分卡是一张拥有分数刻度会让相应阈值的表. 信用评分卡是用于用户信用的一张刻度表. 评分卡是金融风控中常用的一种对于用户信用进行刻画的手段.
五、补充
- id 为贷款清单分配的唯一信用证标识
- loanAmnt 贷款金额
-
- term贷款期限(year)
- interestRate 贷款利率
- installment 分期付款金额
- grade 贷款等级
- subGrade贷款等级之子级
- employmentTitle就业职称
- employmentLength就业年限(年)
- homeOwnership 借款人在登记时提供的房屋所有权状况
- annualIncome 年收入
- verificationStatus 验证状态
- issueDate 贷款发放的月份
- purpose 借款人在贷款申请时的贷款用途类别
- postCode 借款人在贷款申请中提供的邮政编码的前3位数字
- regionCode 地区编码
- dti 债务收入比
- delinquency_2years 借款人过去2年信用档案中逾期30天以上的违约事件数
- ficoRangeLow 借款人在贷款发放时的fico所属的下限范围
- ficoRangeHigh 借款人在贷款发放时的fico所属的上限范围
- openAcc 借款人信用档案中未结信用额度的数量
- pubRec 贬损公共记录的数量
- pubRecBankruptcies 公开记录清除的数量
- revolBal 信贷周转余额合计
- revolUtil 循环额度利用率,或借款人使用的相对于所有可用循环信贷的信贷金额
- totalAcc 借款人信用档案中当前的信用额度总数
- initialListStatus 贷款的初始列表状态
- applicationType 表明贷款是个人申请还是与两个共同借款人的联合申请
- earliesCreditLine 借款人最早报告的信用额度开立的月份
- title 借款人提供的贷款名称
- policyCode 公开可用的策略 =1 代码=2
- n系列匿名特征 匿名特征n0-n14,为一些贷款人行为计数特征的处理