1.賽題了解:賽題以金融風控中個人信貸為背景,根據貸款申請人的資料資訊預測其是否有違約的可能,進而判斷是否發放給此人貸款。這是一個典型的資料分類問題。
2.資料了解:資料總量超過120W,包括47列變量資訊,15列資訊是匿名的。其中訓練集有80W條,測試集A有20W條,測試集B有20W條。對于賽題資料每一列名的含義,官網已經給出。
在jupyter中讀入資料
import pandas as pd
train = pd.read_csv('train.csv')
testA = pd.read_csv('testA.csv')
print('Train data shape:',train.shape)
print('TestA data shape:',testA.shape)
Train data shape: (800000, 47)
TestA data shape: (200000, 48)

3.評估名額:賽題中采用的評估名額是AUC,即:ROC曲線與X坐标軸圍成的面積。
(1) 常用的分類名額
混淆矩陣: 四個元素是真正類(TP)、假負類(FN)、假正類(FP)和真負類(TN)。
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]]
準确率(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
精确率(Precision): 真正類與真正類和假正類和的比列
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))
Precision 0.5
召回率(Recall):真正類與真正類和假負類和的比例
Recall 0.5
F1 Score:兼顧精确率和召回率的名額
F1-score: 0.5
P-R曲線:描述精确率和召回率變化的曲線
import matplotlib.pyplot as plt

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)
ROC:ROC空間将假正例率(FPR)定義為 X 軸,真正例率(TPR)定義為 Y 軸。
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')
AUC(Area Under Curve):被定義為 ROC曲線 下與坐标軸圍成的面積,
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
KS:K-S曲線與ROC曲線類似,K-S曲線将真正例率和假正例率都作為縱軸,橫軸則由標明的門檻值來充當。KS = max(TPR - FPR)
KS值<0.2,一般認為模型沒有區分能力。
KS值[0.2,0.3],模型具有一定區分能力,勉強可以接受
KS值[0.3,0.5],模型具有較強的區分能力。
KS值大于0.75,往往表示模型有異常。
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
4.總結:比賽中了解賽題是第一步,對後期的特種工程和模型選擇尤為重要。