sklearn 二分类计算

需要特别指出本文参考如下链接修改得到
https://blog.csdn.net/itnerd/article/details/102105662

目标计算了AUC,F1_score,SEN,SPE等指标

其中SEN敏感性就是使用sklearn中的TPR来计算的
SPE特异性使用的是sklearn中的1-FPR来计算的

"""
https://blog.csdn.net/itnerd/article/details/102105662
"""

#%%
from numpy.random import random
import numpy as np


#%%
from sklearn.metrics import precision_recall_fscore_support
from sklearn.metrics import roc_auc_score
from sklearn.metrics import roc_curve


from sklearn.metrics import precision_recall_curve
from sklearn.metrics import accuracy_score,f1_score

from sklearn.metrics import confusion_matrix
from sklearn.metrics import classification_report


import matplotlib.pyplot as plt
# %matplotlib inline

#%%
N = 100
y_pred = [random() for i in range(N)]

y_true = [int(y_pred[i]>0.4) if random()>0.5 else 0 for i in range(N)]

y_pred= np.array(y_pred)
y_true = np.array(y_true)


plt.plot(y_pred,'.r',label='pred')
plt.plot(y_true,'.b',label='true')
plt.legend()
plt.show()


# %%
def return_best_thr(y_true, y_score):
    precs, recs, thrs = precision_recall_curve(y_true, y_score)
    
    plt.plot(recs,precs)
    plt.title('PR curve')
    plt.show()
    
    f1s = 2 * precs * recs / (precs + recs)
    f1s = f1s[:-1]
    thrs = thrs[~np.isnan(f1s)]
    f1s = f1s[~np.isnan(f1s)]
    best_thr = thrs[np.argmax(f1s)]
    return best_thr

print('best threshold: ',return_best_thr(y_true,y_pred))

threshold = return_best_thr(y_true, y_pred)

# %%

acc = accuracy_score(y_true=y_true,y_pred=y_pred>threshold)
f1_score_value = f1_score(y_true=y_true,y_pred=y_pred>threshold)
print("acc : {} \t  f1_score : {}".format(acc,f1_score_value))
# %%

conf = confusion_matrix(y_true=y_true, y_pred=y_pred>threshold)
fpr, tpr, thresholds = roc_curve(y_true, y_pred>threshold)

print("tpr :{} \t 1-fpr : {}".format(tpr,1-fpr))
# sensitive_score, specificity_score, 
# specificity = specificity_score(conf)
# sensitive = sensitive_score(conf)

# print("specificity:{}\t sensitive:{}".format(specificity,sensitive ))
# %%


你可能感兴趣的:(学术论文工具,sklearn,分类,python)