1. 程式人生 > >python機器學習-sklearn挖掘乳腺癌細胞(五)

python機器學習-sklearn挖掘乳腺癌細胞(五)

糾正 plot 不錯 方法 eid right ref nump cores

python機器學習-sklearn挖掘乳腺癌細胞( 博主親自錄制)

網易雲觀看地址

https://study.163.com/course/introduction.htm?courseId=1005269003&utm_campaign=commission&utm_source=cp-400000000398149&utm_medium=share

技術分享圖片

模型驗證

分類器好壞驗證,模型建立好後,不是萬事大吉,需要進行crossvalidation, AUC,GINi,KS,GainTable檢驗

KS可以檢測模型區分好壞客戶能力,如果有一個分數段區分能力強,KS會大於0.2

AUC檢測模型分類器效果,分類器敏感度越高,AUC越大,一般AUC大於0.7,分類器準確性就不錯。

Gain Table可以檢測模型收益情況和排序能力

模型驗證中數據要拆分為train(訓練),test(測試),oot(跨時間)

train和test是同一個時間段,一般三七開,train占百分之70,test占百分之30

oot的時間段在train,test後面,用於測試未來數據

技術分享圖片

下圖是模型驗證的可視化:

包括ROC,提升圖,KS,PSI四個指標

技術分享圖片

由於時間關系,我們只詳細說明一下ROC/AUC檢驗

auc分數有兩種計算方式,第一種是根據目標變量y_true,預測分數/預測概率y_socres,通過roc_auc_score(y_true, y_scores)計算AUC

第二種方法是通過fpr,tpr,通過auc(fpr,tpr)來計算AUC

技術分享圖片

excel 繪圖ROC

技術分享圖片

技術分享圖片

ROC的前置條件是分數越高,陽性率越高,但風控模型中,有的分數越低,壞客戶概率越高,例如蜜罐分數,因此ROC繪制出來是反的,需要對陽性標簽反轉pos_label=0

技術分享圖片

由於分數越低,壞客戶概率越高,畫出來的ROC曲線是反轉的,需要糾正

技術分享圖片

AUC/ROC檢驗代碼

# -*- coding: utf-8 -*-
"""
Created on Thu Apr 12 22:31:31 2018
 
@author: [email protected]
"""
import numpy as np
from sklearn import metrics
from sklearn.metrics import roc_curve, auc,roc_auc_score  ###計算roc和auc
 
import pandas as pd
from sklearn.datasets import load_breast_cancer
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
import mglearn
import matplotlib.pyplot as plt
 
cancer=load_breast_cancer()
 
#mglearn.plots.plot_knn_classification(n_neighbors=3)
X_train,x_test,y_train,y_test=train_test_split(cancer.data,cancer.target,stratify=cancer.target,random_state=42)
 
knn=KNeighborsClassifier()
knn.fit(X_train,y_train)
print("accuracy on the training subset:{:.3f}".format(knn.score(X_train,y_train)))
print("accuracy on the test subset:{:.3f}".format(knn.score(x_test,y_test)))
 
#Auc驗證,數據采用測試集數據
#癌癥的概率
proba_cancer=knn.predict_proba(x_test)
y_scores=pd.DataFrame(proba_cancer)[1]
y_scores=np.array(y_scores)
y_true=y_test
#auc分數
#auc分數有兩種計算方式,第一種是根據目標變量y_true,預測分數/預測概率y_socres,通過roc_auc_score(y_true, y_scores)計算AUC
AUC=roc_auc_score(y_true, y_scores)
print("AUC:",AUC)
#auc第二種方法是通過fpr,tpr,通過auc(fpr,tpr)來計算AUC
fpr, tpr, thresholds = metrics.roc_curve(y_true, y_scores, pos_label=1)
AUC1 = auc(fpr,tpr) ###計算auc的值 
 
#print("fpr:",fpr)
#print("tpr:",tpr)
#print("thresholds:",thresholds)
print("AUC1:",AUC1)
 
if AUC >=0.7:
    print("good classifier")
if 0.7>AUC>0.6:
    print("not very good classifier")
if 0.6>=AUC>0.5:
    print("useless classifier")
if 0.5>=AUC:
    print("bad classifier,with sorting problems")
     
 
#繪制ROC曲線
#畫對角線 
plt.plot([0, 1], [0, 1], ‘--‘, color=(0.6, 0.6, 0.6), label=‘Diagonal line‘) 
plt.plot(fpr,tpr,label=‘ROC curve (area = %0.2f)‘ % AUC) 
plt.title(‘ROC curve‘)  
plt.legend(loc="lower right")   

  

掃二維碼,關註博主主頁,學習更多Python知識

技術分享圖片

https://m.study.163.com/user/1135726305.htm?utm_campaign=share&utm_medium=iphoneShare&utm_source=weixin&utm_u=1015941113

python機器學習-sklearn挖掘乳腺癌細胞(五)