• 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逻辑回归
    【EI会议征稿】2024年第四届人工智能、自动化与高性能计算国际会议(AIAHPC 2024)
    3D Instances as 1D Kernels
    shell编程必备100题
    PTA 7-199 水仙花求和
    2022牛客多校联赛第四场 题解
    顺序表应用7:最大子段和之分治递归法
    大橙子vfed 5.0去授权完美破解主题模版源码 | 苹果CMS
    Mybatis-Plus多种批量插入方案对比
    win10怎么录屏?windows自带录屏功能怎么用
  • 原文地址:https://blog.csdn.net/weixin_37707670/article/details/127121024