【Python机器学习】多分类问题的不确定度

decision_function和predict_proba也适用于多分类问题。还是以鸢尾花数据集为例:

python 复制代码
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.datasets import make_circles,load_iris
import numpy as np
from sklearn.model_selection import train_test_split

iris=load_iris()
X_train,X_test,y_train,y_test=train_test_split(
    iris.data,iris.target,random_state=0
)

gbrt=GradientBoostingClassifier(learning_rate=0.01,random_state=0)
gbrt.fit(X_train,y_train)
print('决策函数结果的形状:{}'.format(gbrt.decision_function(X_test).shape))
print('决策函数结果:{}'.format(gbrt.decision_function(X_test)[:6,:]))

可以看到,对于多分类的情况, decision_function的形状为(n_samples,n_classes),每一列对应每个类别的"确定度分数",分数较高的类别可能性更大,得分较低的分类可能性小,可以找到每个数据点的最大元素,也就是预测结果:

python 复制代码
print('决策函数得分最高的分类:{}'.format(np.argmax(gbrt.decision_function(X_test),axis=1)))
print('模型预测结果:{}'.format(gbrt.predict(X_test)))

predict_proba输出的形状相同,也是(n_samples,n_classes),每个数据点的所有可能类别的概率相加为1:

python 复制代码
print('预测每个分类的概率:{}'.format(gbrt.predict_proba(X_test)[:6]))

总之,decision_function和predict_proba的形状始终相同,都是(n_samples,n_classes)

相关推荐
m0_74117333几秒前
Golang Gin如何做Swagger文档_Golang Gin Swagger教程【速学】
jvm·数据库·python
AI_大白2 分钟前
让 Cursor 帮你搞定美股 4 个时段:AI Agent 的时段感知实战
python·架构
Uncertainty!!2 分钟前
claude code在pycharm中的安装使用
ide·python·pycharm·claude code
覆东流5 分钟前
第7天:Python小项目
开发语言·后端·python
a1117768 分钟前
Boxer 论文复刻(需要下载的文件都已放到压缩包)
python·开源·cv
不吃肥肉的傲寒9 分钟前
Graphify安装与结合claude code使用指南
java·python·ai编程·图搜索
geneculture10 分钟前
本真信息观:基于序位守恒的融智学理论框架——人类认知第二次大飞跃的基础
人工智能·算法·机器学习·数据挖掘·融智学的重要应用·哲学与科学统一性·融智时代(杂志)
djjdjdjdjjdj15 分钟前
golang如何编写SSL证书到期检测工具_golang SSL证书到期检测工具编写总结
jvm·数据库·python
axinawang19 分钟前
第2课: 与世界打招呼(输出)
python
2301_8135995520 分钟前
HTML5中Canvas局部刷新区域重绘的算法优化
jvm·数据库·python