第100+24步 ChatGPT学习:概率校准 Beta Calibration

news2024/9/20 22:04:57

基于Python 3.9版本演示

一、写在前面

最近看了一篇在Lancet子刊《eClinicalMedicine》上发表的机器学习分类的文章:《Development of a novel dementia risk prediction model in the general population: A large, longitudinal, population-based machine-learning study》。

学到一种叫做“概率校准”的骚操作,顺手利用GPT系统学习学习。

文章中用的技术是:保序回归(Isotonic regression)。

为了体现举一反三,顺便问了GPT还有哪些方法也可以实现概率校准。它给我列举了很多,那么就一个一个学习吧。

这一期,介绍一个叫做 Beta Calibration 的方法。

二、Beta Calibration

Beta Calibration 是一种用于概率校准的方法,旨在调整机器学习分类模型输出的概率,使其更接近真实的后验概率。这种校准方法是通过对输出概率进行二项分布参数的转换来实现的。

(1)作用和优势

1)提高概率预测的可靠性:经过校准后的概率输出更能反映真实情况,从而提高模型在实际应用中的可靠性。例如,在医疗诊断中,准确的概率预测对于决策非常重要。

2)优化决策阈值:经过校准的概率输出可以帮助优化决策阈值,使得分类模型在不同的应用场景中更加灵活和有效。

3)改进不确定性评估:通过校准后的概率输出,模型能够更准确地评估其预测的不确定性,从而在风险管理和资源分配等方面提供更有价值的信息。

三、Beta Calibration代码实现

下面,我编一个1比3的不太平衡的数据进行测试,对照组使用不进行校准的SVM模型,实验组就是加入校准的SVM模型,看看性能能够提高多少?

(1)不进行校准的SVM模型(默认参数)

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVC
from sklearn.metrics import confusion_matrix, roc_auc_score, roc_curve

# 加载数据
dataset = pd.read_csv('8PSMjianmo.csv')
X = dataset.iloc[:, 1:20].values
Y = dataset.iloc[:, 0].values

# 分割数据集
X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.30, random_state=666)

# 标准化数据
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)

# 使用SVM分类器
classifier = SVC(kernel='linear', probability=True)
classifier.fit(X_train, y_train)

# 预测结果
y_pred = classifier.predict(X_test)
y_testprba = classifier.decision_function(X_test)

y_trainpred = classifier.predict(X_train)
y_trainprba = classifier.decision_function(X_train)

# 混淆矩阵
cm_test = confusion_matrix(y_test, y_pred)
cm_train = confusion_matrix(y_train, y_trainpred)
print(cm_train)
print(cm_test)

# 绘制测试集混淆矩阵
classes = list(set(y_test))
classes.sort()
plt.imshow(cm_test, cmap=plt.cm.Blues)
indices = range(len(cm_test))
plt.xticks(indices, classes)
plt.yticks(indices, classes)
plt.colorbar()
plt.xlabel('Predicted')
plt.ylabel('Actual')
for first_index in range(len(cm_test)):
    for second_index in range(len(cm_test[first_index])):
        plt.text(first_index, second_index, cm_test[first_index][second_index])

plt.show()

# 绘制训练集混淆矩阵
classes = list(set(y_train))
classes.sort()
plt.imshow(cm_train, cmap=plt.cm.Blues)
indices = range(len(cm_train))
plt.xticks(indices, classes)
plt.yticks(indices, classes)
plt.colorbar()
plt.xlabel('Predicted')
plt.ylabel('Actual')
for first_index in range(len(cm_train)):
    for second_index in range(len(cm_train[first_index])):
        plt.text(first_index, second_index, cm_train[first_index][second_index])

plt.show()

# 计算并打印性能参数
def calculate_metrics(cm, y_true, y_pred_prob):
    a = cm[0, 0]
    b = cm[0, 1]
    c = cm[1, 0]
    d = cm[1, 1]
    acc = (a + d) / (a + b + c + d)
    error_rate = 1 - acc
    sen = d / (d + c)
    sep = a / (a + b)
    precision = d / (b + d)
    F1 = (2 * precision * sen) / (precision + sen)
    MCC = (d * a - b * c) / (np.sqrt((d + b) * (d + c) * (a + b) * (a + c)))
    auc_score = roc_auc_score(y_true, y_pred_prob)
    
    metrics = {
        "Accuracy": acc,
        "Error Rate": error_rate,
        "Sensitivity": sen,
        "Specificity": sep,
        "Precision": precision,
        "F1 Score": F1,
        "MCC": MCC,
        "AUC": auc_score
    }
    return metrics

metrics_test = calculate_metrics(cm_test, y_test, y_testprba)
metrics_train = calculate_metrics(cm_train, y_train, y_trainprba)

print("Performance Metrics (Test):")
for key, value in metrics_test.items():
    print(f"{key}: {value:.4f}")

print("\nPerformance Metrics (Train):")
for key, value in metrics_train.items():
print(f"{key}: {value:.4f}")

结果输出:

记住这些个数字。

这个参数的SVM还没有LR好。

(2)进行校准的SVM模型(默认参数)

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVC
from sklearn.metrics import confusion_matrix, roc_auc_score, roc_curve
from scipy.stats import beta
from scipy.optimize import minimize

# 加载数据
dataset = pd.read_csv('8PSMjianmo.csv')
X = dataset.iloc[:, 1:20].values
Y = dataset.iloc[:, 0].values

# 分割数据集
X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.30, random_state=666)

# 标准化数据
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)

# 使用SVM分类器
classifier = SVC(kernel='rbf', C=0.1, probability=True)
classifier.fit(X_train, y_train)

# 获取未校准的概率预测
y_train_probs = classifier.predict_proba(X_train)[:, 1]
y_test_probs = classifier.predict_proba(X_test)[:, 1]

# Beta Calibration 函数
def beta_calibration(y_probs, y_true):
    def neg_log_likelihood(params):
        a, b = params
        return -np.sum(beta.logpdf(y_probs, a, b) * y_true + beta.logpdf(1 - y_probs, a, b) * (1 - y_true))
    
    result = minimize(neg_log_likelihood, [1, 1], bounds=[(1e-3, None), (1e-3, None)])
    a, b = result.x
    return beta.cdf(y_probs, a, b)

# 进行Beta校准
calibrated_train_probs = beta_calibration(y_train_probs, y_train)
calibrated_test_probs = beta_calibration(y_test_probs, y_test)

# 预测结果
y_train_pred = (calibrated_train_probs >= 0.5).astype(int)
y_test_pred = (calibrated_test_probs >= 0.5).astype(int)

# 混淆矩阵
cm_test = confusion_matrix(y_test, y_test_pred)
cm_train = confusion_matrix(y_train, y_train_pred)
print(cm_train)
print(cm_test)

# 绘制混淆矩阵函数
def plot_confusion_matrix(cm, classes, title='Confusion Matrix'):
    plt.imshow(cm, cmap=plt.cm.Blues)
    indices = range(len(cm))
    plt.xticks(indices, classes)
    plt.yticks(indices, classes)
    plt.colorbar()
    plt.xlabel('Predicted')
    plt.ylabel('Actual')
    for first_index in range(len(cm)):
        for second_index in range(len(cm[first_index])):
            plt.text(first_index, second_index, cm[first_index][second_index])
    plt.title(title)
    plt.show()

# 绘制测试集混淆矩阵
plot_confusion_matrix(cm_test, list(set(y_test)), 'Confusion Matrix (Test)')

# 绘制训练集混淆矩阵
plot_confusion_matrix(cm_train, list(set(y_train)), 'Confusion Matrix (Train)')

# 计算并打印性能参数
def calculate_metrics(cm, y_true, y_pred_prob):
    a = cm[0, 0]
    b = cm[0, 1]
    c = cm[1, 0]
    d = cm[1, 1]
    acc = (a + d) / (a + b + c + d)
    error_rate = 1 - acc
    sen = d / (d + c)
    sep = a / (a + b)
    precision = d / (b + d)
    F1 = (2 * precision * sen) / (precision + sen)
    MCC = (d * a - b * c) / (np.sqrt((d + b) * (d + c) * (a + b) * (a + c)))
    auc_score = roc_auc_score(y_true, y_pred_prob)
    
    metrics = {
        "Accuracy": acc,
        "Error Rate": error_rate,
        "Sensitivity": sen,
        "Specificity": sep,
        "Precision": precision,
        "F1 Score": F1,
        "MCC": MCC,
        "AUC": auc_score
    }
    return metrics

metrics_test = calculate_metrics(cm_test, y_test, calibrated_test_probs)
metrics_train = calculate_metrics(cm_train, y_train, calibrated_train_probs)

print("Performance Metrics (Test):")
for key, value in metrics_test.items():
    print(f"{key}: {value:.4f}")

print("\nPerformance Metrics (Train):")
for key, value in metrics_train.items():
    print(f"{key}: {value:.4f}")

看看结果:

同样的,仅仅训练集起作用了,验证集差强人意。

四、换个策略

参考那篇文章的策略:采用五折交叉验证来建立和评估模型,其中四折用于训练,一折用于评估,在训练集中,其中三折用于建立SVM模型,另一折采用Beta Calibration概率校正,在训练集内部采用交叉验证对超参数进行调参。

代码:

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.model_selection import train_test_split, GridSearchCV, KFold
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVC
from sklearn.metrics import confusion_matrix, roc_auc_score, roc_curve
from scipy.stats import beta
from scipy.optimize import minimize

# 加载数据
dataset = pd.read_csv('8PSMjianmo.csv')
X = dataset.iloc[:, 1:20].values
Y = dataset.iloc[:, 0].values

# 分割数据集
X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.30, random_state=666)

# 标准化数据
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)

# 超参数调优
param_grid = {'C': [0.01, 0.1, 1, 10, 100], 'kernel': ['rbf']}
svm = SVC(probability=True)
clf = GridSearchCV(svm, param_grid, cv=3, scoring='roc_auc')
clf.fit(X_train, y_train)
best_params = clf.best_params_

# 五折交叉验证
kf = KFold(n_splits=5)
calibrated_probs = []
true_labels = []

for train_index, test_index in kf.split(X_train):
    X_train_cv, X_val_cv = X_train[train_index], X_train[test_index]
    y_train_cv, y_val_cv = y_train[train_index], y_train[test_index]

    # 使用最佳参数训练SVM
    classifier = SVC(kernel=best_params['kernel'], C=best_params['C'], probability=True)
    classifier.fit(X_train_cv, y_train_cv)

    # 获取未校准的概率预测
    y_val_cv_probs = classifier.predict_proba(X_val_cv)[:, 1]

    # Beta Calibration
    def beta_calibration(y_probs, y_true):
        def neg_log_likelihood(params):
            a, b = params
            return -np.sum(beta.logpdf(y_probs, a, b) * y_true + beta.logpdf(1 - y_probs, a, b) * (1 - y_true))
        
        result = minimize(neg_log_likelihood, [1, 1], bounds=[(1e-3, None), (1e-3, None)])
        a, b = result.x
        return beta.cdf(y_probs, a, b)

    # 进行Beta校准
    calibrated_val_cv_probs = beta_calibration(y_val_cv_probs, y_val_cv)
    calibrated_probs.extend(calibrated_val_cv_probs)
    true_labels.extend(y_val_cv)

# 用于测试集的SVM模型训练和校准
classifier_final = SVC(kernel=best_params['kernel'], C=best_params['C'], probability=True)
classifier_final.fit(X_train, y_train)
y_test_probs = classifier_final.predict_proba(X_test)[:, 1]
calibrated_test_probs = beta_calibration(y_test_probs, y_test)

# 预测结果
y_train_pred = (np.array(calibrated_probs) >= 0.5).astype(int)
y_test_pred = (calibrated_test_probs >= 0.5).astype(int)

# 混淆矩阵
cm_test = confusion_matrix(y_test, y_test_pred)
cm_train = confusion_matrix(y_train, y_train_pred)
print("Training Confusion Matrix:\n", cm_train)
print("Testing Confusion Matrix:\n", cm_test)

# 绘制混淆矩阵函数
def plot_confusion_matrix(cm, classes, title='Confusion Matrix'):
    plt.imshow(cm, cmap=plt.cm.Blues)
    indices = range(len(cm))
    plt.xticks(indices, classes)
    plt.yticks(indices, classes)
    plt.colorbar()
    plt.xlabel('Predicted')
    plt.ylabel('Actual')
    for first_index in range(len(cm)):
        for second_index in range(len(cm[first_index])):
            plt.text(first_index, second_index, cm[first_index][second_index])
    plt.title(title)
    plt.show()

# 绘制测试集混淆矩阵
plot_confusion_matrix(cm_test, list(set(y_test)), 'Confusion Matrix (Test)')

# 绘制训练集混淆矩阵
plot_confusion_matrix(cm_train, list(set(y_train)), 'Confusion Matrix (Train)')

# 计算并打印性能参数
def calculate_metrics(cm, y_true, y_pred_prob):
    a = cm[0, 0]
    b = cm[0, 1]
    c = cm[1, 0]
    d = cm[1, 1]
    acc = (a + d) / (a + b + c + d)
    error_rate = 1 - acc
    sen = d / (d + c)
    sep = a / (a + b)
    precision = d / (b + d)
    F1 = (2 * precision * sen) / (precision + sen)
    MCC = (d * a - b * c) / (np.sqrt((d + b) * (d + c) * (a + b) * (a + c)))
    auc_score = roc_auc_score(y_true, y_pred_prob)
    
    metrics = {
        "Accuracy": acc,
        "Error Rate": error_rate,
        "Sensitivity": sen,
        "Specificity": sep,
        "Precision": precision,
        "F1 Score": F1,
        "MCC": MCC,
        "AUC": auc_score
    }
    return metrics

metrics_test = calculate_metrics(cm_test, y_test, calibrated_test_probs)
metrics_train = calculate_metrics(cm_train, y_train, np.array(calibrated_probs))

print("Performance Metrics (Test):")
for key, value in metrics_test.items():
    print(f"{key}: {value:.4f}")

print("\nPerformance Metrics (Train):")
for key, value in metrics_train.items():
    print(f"{key}: {value:.4f}")

输出:

哈哈哈,性能参数都飞回去了。

五、最后

各位可以去试一试在其他数据或者在其他机器学习分类模型中使用的效果。

数据不分享啦。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2118083.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

元宇宙的崛起:重塑2024年游戏行业的新趋势

最早可以追溯到1994年,当时出现了世界上第一个轴测图界面的多人互动社交游戏《Web World》。‌这个游戏允许用户实时聊天、旅游、改造场景,开启了游戏中的UGC模式,可以视为元宇宙游戏的雏形。 2021年Roblox元宇宙的概念股上市,Fac…

学生护眼台灯哪个品牌比较好?分享五款效果好的学生护眼台灯

现在孩子的很多兴趣班和课后辅导班都是在线上举行,通常对着手机电脑长时间。电子产品有大量蓝光和辐射,会伤害到孩子的眼睛。但为了学习,也是没办法。护眼台灯的出现可以让孩子们的眼睛得到保护,防止蓝光对眼睛的伤害。学生护眼台…

用命令行的方式启动.netcore webapi

用命令行的方式启动.netcore web项目 进入指定的项目文件夹,比如我发布后的代码放在下面文件夹中 在此地址栏中输入“cmd”,打开命令提示符,进入到发布代码目录 命令行启动.netcore项目的命令为: dotnet 项目启动文件.dll --urls"ht…

CSP-J基础之数学基础 计数原理与排列组合 一篇搞懂

文章目录 前言加法原理加法原理是什么使用场景 乘法原理举个例子总结 区别加法原理乘法原理总结 乘法原理的运用排列组合**排列****组合**总结 计算排列的可能种数举个例子数学定义数学公式应用公式例子应用 总结全排列举个例子数学定义数学公式作用 组合数学定义组合的公式举个…

系统安全设计规范(Word完整版)

1.1 总体设计 1.1.1 设计原则 1.2 物理层安全 1.2.1 机房建设安全 1.2.2 电气安全特性 1.2.3 设备安全 1.2.4 介质安全措施 1.3 网络层安全 1.3.1 网络结构安全 1.3.2 划分子网络 1.3.3 异常流量管理 1.3.4 网络安全审计 1.3.5 网络访问控制 1.3.6 完整性检查 1.…

天洑软件荣获国家级专精特新“小巨人”企业认定

近日,江苏省工业和信息化厅公布了第六批国家级专精特新"小巨人"企业名单,南京天洑软件有限公司(以下简称“天洑软件”)获得国家级专精特新“小巨人”企业认定。继2023年被评为江苏省“专精特新”中小企业后,…

【828华为云征文|华为云Flexus X实例:从选购到登录,一站式指南】

华为云Flexus X实例:从选购到登录,一站式指南 华为云Flexus X实例的优势大揭秘操作指南:一步步带你开通华为云Flexus X实例注册与登录华为云账号选择配置并购买选择Flexus X实例配置选择基础配置实例规格镜像存储网络弹性公网IP您可能需要&am…

佰朔资本:换手率是什么指标?换手率高股价为什么不涨呢?

换手率,也叫”周转率“,指的是在必定时间内商场中股票易手买卖的频率,是反映股票流通性强弱的指标之一。 换手率某段时期内成交量/发行总股数*100%。 通常而言,在股票商场上,换手次数多,筹码互动多&#…

Docker 部署 Kafka (图文并茂超详细)

部署 Kafka ( Docker ) Kafka对于zookeeper是强依赖,保存kafka相关的节点数据,所以安装Kafka之前必须先安装zookeeper [Step 1] : 部署 Zookeeper -> 拉取 Zookeeper 镜像 ➡️ 启动 Zookeeper 容器 docker pull zookeeper:3.4.14 docker run -d --…

Linux网络:网络协议栈协议

1.网络在体系结构的位置与网络协议栈的层状结构 2.协议栈各层的功能 协议栈分层设计达到了解耦目的,层与层之间只有接口之间的关系,提高了代码之间的可维护性与拓展性。同一层之间使用的协议相同,达到了跨设备的作用 3.协议 协议本质是一…

【详解】文件操作,Stream流

文件(File)操作——I/O流 Windows(大多数)进行文件操作的类File。 文件?文件夹?路径? 文件 能够使用工具打开操作的,文件是不能存储文件的。 一般文件具有后缀——.mp4 文件夹 存储文件的 路径问题——“/” 正右\ 反左/ ——统一朝左&am…

什么是点对点专线、SDH专线以及MSTP专线?

点对点专线(Point-to-Point Circuit)、SDH专线(Synchronous Digital Hierarchy)以及MSTP专线(Multi-Service Transport Platform)都是企业级通信服务中常见的网络连接类型,主要用于提供高带宽、…

SprinBoot+Vue停车场管理系统的设计与实现

目录 1 项目介绍2 项目截图3 核心代码3.1 Controller3.2 Service3.3 Dao3.4 application.yml3.5 SpringbootApplication3.5 Vue 4 数据库表设计5 文档参考6 计算机毕设选题推荐7 源码获取 1 项目介绍 博主个人介绍:CSDN认证博客专家,CSDN平台Java领域优质…

《语文新读写》是知网收录吗?语文新读写编辑部查询

《语文新读写》是知网收录吗?语文新读写编辑部查询 《语文新读写》是知网收录。 一、期刊简介 《语文新读写》是经国家新闻出版总署正式批准,由上海世纪出版(集团)有限公司主管,上海少年儿童出版社有限公司主办的综合…

超宽带uwb芯片模块测距,扫地机器人轨迹跟随,自动导航定位、测距避障

智能家居产品正以未有的速度改变着我们的生活。从智能音箱到智能门锁,再到今天我们要深入探讨的主角——搭载了超宽带(Ultra-Wideband, UWB)芯片模块的扫地机器人,它们正悄然开启一场家庭清洁的创新。想象一下,一个不仅…

Python | 练习作业 2

为学生登录系统新增搜索功能。 第二天作业的解题思路: # 1.创建一个空列表保存搜索结果 # 2.让用户输入要搜索的内容 # 3.遍历学生信息,检查学生的id name age gender score # 中的属性值 是否跟用户搜索的内容一致 # 4.如果有一致的属性 那么就将该学生…

全局共用一个线程池竟然发生死锁了

在我们进行代码开发时,我也见过很多全局注册一个自定义线程池(也有可能不是自定义的,直接使用更不推荐Executors 创建的线程池),也许是业务量不高、也许是其他原因,反正全局可这一个线程池使劲造。 先给大家…

深入FastAPI:掌握使用多个关联模型的高级用法[Union类型]

在FastAPI中,响应模型可以声明为Union类型,这允许你为同一个端点定义多种可能的响应模型。这种灵活性使得API可以根据不同的情况返回不同类型的数据结构。 例如,根据请求中的查询参数或数据库中的数据,一个API端点可能有时返回一…

零信任安全:重新思考数字世界的访问

目录 ​编辑 网络安全形势的演变 数字安全的变化 引入零信任安全 零信任的当今意义 了解零信任原则 零信任架构的核心概念 实施微分段 持续验证:积极主动的立场 与传统安全模型的对比 在现代企业中实施零信任 零信任实施基础知识 多重身份验证 (MFA) 的…

【数据结构】——二叉树(JAVA)

一、二叉树的定义二、二叉树的形态三、二叉树的性质四、二叉树的存储五、二叉树的创建与遍历(递归) 一、二叉树的定义 二叉树(Binary Tree) 是由n个结点构成的有限集(n≥0),n0时为空树,n>0时为非空树。对于非空树root&#xf…