当前位置:首页 > 技术 > 正文内容

分类器置信度评估:从决策函数到概率输出的完整实现

访客 技术 2026年7月6日 1

不确定性量化的核心价值

在机器学习实践中,仅获取分类结果往往不够充分——理解模型对其预测的置信程度同样关键。这种不确定性信息在多个领域发挥重要作用:医疗诊断中辅助医生判断可疑病例、金融风控中识别异常交易、主动学习中选择最有价值的样本进行标注,以及提升模型可解释性。

决策函数与概率输出机制

分类器通常通过两种方式表达预测可信度:决策函数输出实数值,反映样本到分类边界的距离;概率输出将数值映射到0-1区间,更直观地表示类别归属可能性。对于线性模型,二者可通过sigmoid函数相互转换。

数学表达上,假设线性分类器输出为 \( f(x) = w^T x + b \),则决策函数直接取符号,而概率形式为 \( p(x) = \frac{1}{1+e^{-f(x)}} \)。

主流分类器实现方案

逻辑回归

该模型天然输出概率值,其决策边界由sigmoid函数定义:\( p(y=1|x) = \frac{1}{1+e^{-(w^T x+b)}} \)。

支持向量机

SVM原始输出为决策函数值 \( f(x)=w^T x+b \),可通过Platt校准转换为概率:\( p(y=1|x) = \frac{1}{1+e^{A f(x)+B}} \),其中参数A、B通过交叉验证拟合。

随机森林

集成模型通过投票机制获得概率:\( p(y=c|x)=\frac{1}{N}\sum_{i=1}^N \mathbb{I}(T_i(x)=c) \),即各决策树预测类别c的比例。

代码实现实战

数据准备

import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler

dataset = pd.read_csv("iris.csv")
features = dataset[['sepal_length', 'sepal_width', 'petal_length', 'petal_width']]
labels = dataset['species']

X_train, X_test, y_train, y_test = train_test_split(features, labels, test_size=0.2, random_state=42)
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)

逻辑回归概率预测

from sklearn.linear_model import LogisticRegression

log_reg = LogisticRegression()
log_reg.fit(X_train_scaled, y_train)
proba_log = log_reg.predict_proba(X_test_scaled)
print("逻辑回归前5个样本概率:", proba_log[:5])

SVM概率校准

from sklearn.svm import SVC
from sklearn.calibration import CalibratedClassifierCV

svm_base = SVC(probability=True)
svm_calibrated = CalibratedClassifierCV(svm_base, cv=5)
svm_calibrated.fit(X_train_scaled, y_train)
proba_svm = svm_calibrated.predict_proba(X_test_scaled)
print("SVM前5个样本概率:", proba_svm[:5])

随机森林概率输出

from sklearn.ensemble import RandomForestClassifier

rf_model = RandomForestClassifier(n_estimators=100, random_state=42)
rf_model.fit(X_train_scaled, y_train)
proba_rf = rf_model.predict_proba(X_test_scaled)
print("随机森林前5个样本概率:", proba_rf[:5])

不确定性计算函数

import numpy as np

def compute_uncertainty(proba_array):
    return np.std(proba_array, axis=1)

uncertainty_scores = compute_uncertainty(proba_rf)
print("前5个样本不确定性:", uncertainty_scores[:5])

不确定性分布可视化

import matplotlib.pyplot as plt

plt.hist(uncertainty_scores, bins=20, edgecolor='black')
plt.xlabel("Uncertainty Score")
plt.ylabel("Frequency")
plt.title("Uncertainty Distribution of Random Forest")
plt.show()

高不确定性样本筛选

top_uncertain_idx = np.argsort(uncertainty_scores)[-5:]
high_uncertain_samples = X_test_scaled[top_uncertain_idx]
print("最不确定的5个样本特征:", high_uncertain_samples)

交叉验证概率评估

from sklearn.model_selection import cross_val_predict

cv_proba = cross_val_predict(rf_model, features, labels, method='predict_proba')
cv_uncertainty = compute_uncertainty(cv_proba)
print("交叉验证前5个不确定性值:", cv_uncertainty[:5])

主动学习样本选择

active_learn_idx = np.argsort(cv_uncertainty)[-5:]
active_samples = features.iloc[active_learn_idx]
print("需要标注的样本索引:", active_samples.index.tolist())

异常检测应用

anomaly_threshold = 0.2
anomaly_mask = uncertainty_scores > anomaly_threshold
anomalies = X_test_scaled[anomaly_mask]
print(f"检测到{len(anomalies)}个异常样本")

模型可解释性分析

import shap

shap_explainer = shap.TreeExplainer(rf_model)
shap_values = shap_explainer.shap_values(X_test_scaled)
shap.summary_plot(shap_values, X_test_scaled, plot_type="bar")

端到端工作流

# 完整流程合并
full_pipeline = [
    ("load", lambda: pd.read_csv("iris.csv")),
    ("split", lambda df: train_test_split(df[['sepal_length', 'sepal_width', 'petal_length', 'petal_width']], df['species'], test_size=0.2, random_state=42)),
    ("scale", lambda X: StandardScaler().fit_transform(X)),
    ("train", lambda X, y: RandomForestClassifier(n_estimators=100, random_state=42).fit(X, y)),
    ("predict", lambda model, X: model.predict_proba(X)),
    ("evaluate", lambda proba: (proba, np.std(proba, axis=1)))
]

data = full_pipeline[0][1]()
X_tr, X_te, y_tr, y_te = full_pipeline[1][1](data)
X_tr_s = full_pipeline[2][1](X_tr)
X_te_s = full_pipeline[2][1](X_te)
model = full_pipeline[3][1](X_tr_s, y_tr)
proba, unc = full_pipeline[4][1](model, X_te_s)
print("高不确定性样本特征:", X_te[np.argsort(unc)[-5:]])

相关文章

Linux crontab 详解

1) crontab 是什么cron 是 Linux 的定时任务守护进程;crontab 是用来编辑/查看“按时间周期执行命令”的表(cron table)。常见两类:用户 crontab:每个用户一份(crontab -e 编辑)系统级 crontab / cron.d:可指定执行用户(/etc/crontab、/etc/cron.d/*)2) crontab 时间...

富文本里可以允许的 HTML 属性

一、所有标签默认允许的安全属性(极少)class        (可选)id           (通常建议禁用)title️ 注意:id 容易被滥用做锚点注入,很多系统直接禁用class 允许的话最好只允许固定前缀(如 editor-*)二、a 标签允许属性<a href="" t...

Mac 安装 Node.js 指南

方法一:通过官网安装包(最简单,适合初学者)如果你只是想快速安装并开始使用,这是最直接的方法。访问 Node.js 官网。页面会显示两个版本:LTS (Recommended For Most Users):长期支持版,最稳定。建议选这个。Current:最新特性版,包含最新功能但可能不够稳定。下载 .pkg 安装包并运行。按照安装向导点击“下一步”即可完成。方法二:使用 Homebrew 安装(...

Dom\HTML_NO_DEFAULT_NS 的副作用:自动加闭合标签

在使用Dom\HTMLDocument时,Dom\HTML_NO_DEFAULT_NS 将禁止在解析过程中设置元素的命名空间, 此设置是为了与DOMDocument向后兼容而存在的。当使用它时,已知的一个副作用就是:自动加闭合标签例如 </img> 为什么会这样?当你使用:Dom\HTML_NO_DEFAULT_NS文档会变成 无命名空间模式,此时内部更接近 XML...

Laravel 事件和监听器创建

在 Laravel 中,使用 Artisan 命令创建 Events(事件) 和 Listeners(监听器) 是非常高效的。你可以通过以下几种方式来实现:1. 手动创建单个 Event如果你只想创建一个事件类,可以使用 make:event 命令:Bashphp artisan make:event UserRegistered执行后,文件将生成在 app/Even...

自定义域名解析神器 dnsmasq

什么是 dnsmasq?dnsmasq 是一个轻量级、功能强大的网络服务工具,专为小型和中等规模网络设计。它是一个综合的网络基础设施解决方案[1]。dnsmasq 能做什么?功能说明应用场景DNS 转发与缓存将 DNS 查询转发到上游服务器(ISP、Google DNS 等),并在本地缓存结果加快 DNS 查询速度,减少外部 DNS 流量本地 DNS解析本地网络设备的主机名,无需编辑&n...

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。