https://blog.csdn.net/huangqihao723/article/details/83347269
'''信贷风控模型评估指标'''
'----ks'
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
def createksData():
d=pd.DataFrame([
['100-',100,20],
['100-200',80,21],
['200-300',72,45],
['300-400',47,90],
['400-500',30,101],
['500-600',15,200],
['600-700',12,301],
['700+',10,220]
],columns=['score_range','bad_count','good_count'])
return d
d=createksData()
d['bad_count_cumsum_rate']=d['bad_count'].cumsum()/d['bad_count'].sum()
d['good_count_cumsum_rate']=d['good_count'].cumsum()/d['good_count'].sum()
ks=max(abs(d['good_count_cumsum_rate']-d['bad_count_cumsum_rate']))
'混淆矩阵'
def createconfuseData():
d=pd.DataFrame([
[10,20],
[30,40]
],columns=['true','false'],index=['pre_true','pre_false'])
return d
d=createconfuseData()
print(d)
'''
tp:第一个t表示是否正确预测,第二个为结果,所以tp表示,正确预测成positive的数量
tn:正确预测为negtive的数量
fp:错误预测为positive的数量
fn:错误预测为negtive的数量
参考https://en.wikipedia.org/wiki/Confusion_matrix
'''
tp,fp,fn,tn=[d.iloc[0,0],d.iloc[0,1],d.iloc[1,0],d.iloc[1,1]]
tpr=tp/(tp+fn)
fpr=fp/(fp+tn)
'roc'
def createrocData():
d=pd.DataFrame([
['p',.9,1,0,5,4],
['p',.8,2, 0, 5, 3],
['n',.7, 2, 1, 4, 3],
['p',.6, 3, 1, 4, 2],
['p',.55, 4, 1, 4, 1],
['n',.54, 4, 2, 3, 1],
['n',.53, 4, 3, 2, 1],
['n',.51, 4, 4, 1, 1],
['p',.5, 5, 4, 1, 0],
['n',.4, 5, 5, 0, 0],
],columns=['class','prob','tp','fp','tn','fn'])
return d
d=createrocData()
d['fpr']=d['fp']/(d['fp']+d['tn'])
d['tpr']=d['tp']/(d['tp']+d['fn'])
# 最优点为到y=x的距离最大的时候对应的概率,有距离公式 |(a*x+b*y+c)|/sqrt(a**2+b**2),这里y-x=0
d['dist']=np.abs(d['tpr']-d['fpr'])/np.sqrt(2)
point_line=d[d['dist']==max(d['dist'])]
plt.plot(d['fpr'],d['tpr'],label="roc")
plt.xlim(0,1)
plt.ylim(0,1)
x_sample=np.linspace(0,1,1000)
plt.plot(x_sample,x_sample,label='random') # 随机猜测的话,是这样一条曲线
plt.legend()
plt.annotate('optimum point', xy=(point_line['fpr'], point_line['tpr']), xytext=(point_line['fpr']+0.1, point_line['tpr']+0.1),
arrowprops=dict(facecolor='black', shrink=0.05),
)
plt.show()
'auc'
'gini'
'ar'