首页 > 编程语言 >python 决策曲线 DCA

python 决策曲线 DCA

时间:2024-02-04 16:45:09浏览次数:30  
标签:25 python 曲线 DCA benefit thresh ax net label

import numpy as np
import matplotlib.pyplot as plt
from sklearn.metrics import confusion_matrix


def calculate_net_benefit_model(thresh_group, y_pred_score, y_label):
    net_benefit_model = np.array([])
    for thresh in thresh_group:
        y_pred_label = y_pred_score > thresh
        tn, fp, fn, tp = confusion_matrix(y_label, y_pred_label).ravel()
        n = len(y_label)
        net_benefit = (tp / n) - (fp / n) * (thresh / (1 - thresh))
        net_benefit_model = np.append(net_benefit_model, net_benefit)
    return net_benefit_model


def calculate_net_benefit_all(thresh_group, y_label):
    net_benefit_all = np.array([])
    tn, fp, fn, tp = confusion_matrix(y_label, y_label).ravel()
    total = tp + tn
    for thresh in thresh_group:
        net_benefit = (tp / total) - (tn / total) * (thresh / (1 - thresh))
        net_benefit_all = np.append(net_benefit_all, net_benefit)
    return net_benefit_all


def plot_DCA(ax, thresh_group, net_benefit_model, net_benefit_all):
    #Plot
    ax.plot(thresh_group, net_benefit_model, color = 'crimson', label = 'Model')
    ax.plot(thresh_group, net_benefit_all, color = 'black',label = 'Treat all')
    ax.plot((0, 1), (0, 0), color = 'black', linestyle = ':', label = 'Treat none')

    #Fill,显示出模型较于treat all和treat none好的部分
    y2 = np.maximum(net_benefit_all, 0)
    y1 = np.maximum(net_benefit_model, y2)
    ax.fill_between(thresh_group, y1, y2, color = 'crimson', alpha = 0.2)

    #Figure Configuration, 美化一下细节
    ax.set_xlim(0,1)
    ax.set_ylim(net_benefit_model.min() - 0.15, net_benefit_model.max() + 0.15)#adjustify the y axis limitation
    ax.set_xlabel(
        xlabel = 'Threshold Probability', 
        fontdict= {'family': 'Times New Roman', 'fontsize': 15}
        )
    ax.set_ylabel(
        ylabel = 'Net Benefit', 
        fontdict= {'family': 'Times New Roman', 'fontsize': 15}
        )
    ax.grid('major')
    ax.spines['right'].set_color((0.8, 0.8, 0.8))
    ax.spines['top'].set_color((0.8, 0.8, 0.8))
    ax.legend(loc = 'upper right')

    return ax


if __name__ == '__main__':
    #构造一个分类效果不是很好的模型
    y_pred_score = np.arange(0, 1, 0.001)
    y_label = np.array([1]*25 + [0]*25 + [0]*450 + [1]*25 + [0]*25+ [1]*25 + [0]*25 + [1]*25 + [0]*25+ [1]*25 + [0]*25 + [1]*25 + [0]*25 + [1]*25 + [0]*25 + [1]*25 + [0]*50 + [1]*125)

    thresh_group = np.arange(0,1,0.01)
    net_benefit_model = calculate_net_benefit_model(thresh_group, y_pred_score, y_label)
    net_benefit_all = calculate_net_benefit_all(thresh_group, y_label)
    fig, ax = plt.subplots()
    ax = plot_DCA(ax, thresh_group, net_benefit_model, net_benefit_all)
    # fig.savefig('fig1.png', dpi = 300)
    plt.show()

参考链接 https://huaweicloud.csdn.net/63808352dacf622b8df89473.html

标签:25,python,曲线,DCA,benefit,thresh,ax,net,label
From: https://www.cnblogs.com/cupleo/p/18006485

相关文章

  • Python实现给视频添加字幕
    主要思路:1.用moviepy库处理视频文件;用pysrt库处理字幕。2.由于moviepy依赖名为ImageMagick免费开源图片编辑软件,所以要先安装ImageMagick开始:1.安装ImageMagick到官网 https://www.imagemagick.org/script/download.php#windows下载我这里选择ImageMagick-7.1.1-27-Q16-......
  • 11 - 初步了解Python
    初步了解Python参考资料:菜鸟教程:Python3基础语法PEP8:StyleGuideforPythonCodePythonDocs:SourceCodeEncoding菜鸟教程:Python3命令行参数PythonDocs:ExecutablePythonScripts知乎:#!/usr/bin/envpython有什么用?编程规范:PEP8在没有额外编程规范的前提下,建议翻阅并......
  • Yield Keyword, classmethod and static method, and Property Method in Python
    ReferenceWhatisYieldKeywordinPythonPython'syieldkeywordislikeanotheroneweusetoreturnanexpressionorobject,typicallyinfunctions,calledreturn.Thereisasmallamountoffluctuation,though.Theyieldstatementofafunctionre......
  • python3 模型日记
    说明作为一种python框架模型的记录吧,用于个人总结,不定时更新。正文1.主进程退出后,子进程也跟着退出之前遇到过一种情况,用flet写了一个页面,然后又同时开了一个tcpserver的子线程,flet页面点击关闭后,tcpserver却没有退出。在linux中按Ctrl+c可以强制结束,但是如......
  • Python 矩阵运算
    #coding=utf8fromrequests.sessionsimportsessionimportpubimportnumpyasnpimportdatetimeimportosfromapscheduler.schedulers.blockingimportBlockingSchedulerdefget_default_conn():  conn=(host="127.0.0.1",  port="3306&......
  • 【Python】conda常用命令
    ✨conda下载AnacondaFreeDownload|AnacondaMinicondaMiniconda—minicondadocumentation个人推荐安装Miniconda即可反正我从来没有使用过Anaconda的GUI✨conda基本命令新建环境condacreate-n${envName}python=3.8-n新建环境名称python=3.8可选指定Py......
  • 很好用的python游戏环境(续):强化学习算法走迷宫游戏环境(导航问题 navigation):分享一个pyt
    前文分享了一个python下的maze游戏环境,本文再给出一个不错的实现项目,这个项目的实现更加的简单,并且可视化界面做的很好看,是用tkinter框架做的可视化:相关:迷宫游戏python实现Github地址:https://github.com/wonanut/Maze-Game/tree/Maze-game-v1.0.7......
  • 很好用的python游戏环境:强化学习算法走迷宫游戏环境(导航问题 navigation):分享一个pyth
    项目的GitHub地址(作者:莫凡):https://github.com/MorvanZhou/mmaze运行的示例代码:importmmazestart=(0,0)end=(10,10)m=mmaze.generate(width=11,height=11,symmetry="horizontal")solutions=m.solve(start=start,end=end)m.plot(solution=solutions[0],star......
  • python语言下的迷宫游戏的实现猜想
    由于本人是研究AI的,尤其是AI的强化学习方向,有时候就会对一些小游戏环境的实现有几分兴趣,因为刚看了有关reinforcementlearning解决maze游戏的论文,于是就突发奇想的对这个maze迷宫的实现有些想了解了。迷宫其实就是在一个表格上画线,然后构成的从某点到另一个点的一个通路的环境:具体......
  • pyqt报错、python报错:src/pyaudio/device_api.c:9:10: fatal error: portaudio.h: 没
    报错信息:-DNDEBUG-fwrapv-O2-Wall-fPIC-O2-isystem/home/devil/anaconda3/envs/91/include-fPIC-O2-isystem/home/devil/anaconda3/envs/91/include-fPIC-I/usr/local/include-I/usr/include-I/home/devil/anaconda3/envs/91/include/python3.10-csrc/pyaudio/d......