【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)

相关推荐
数据智能老司机6 小时前
精通 Python 设计模式——分布式系统模式
python·设计模式·架构
数据智能老司机7 小时前
精通 Python 设计模式——并发与异步模式
python·设计模式·编程语言
数据智能老司机7 小时前
精通 Python 设计模式——测试模式
python·设计模式·架构
数据智能老司机7 小时前
精通 Python 设计模式——性能模式
python·设计模式·架构
c8i7 小时前
drf初步梳理
python·django
每日AI新事件7 小时前
python的异步函数
python
这里有鱼汤9 小时前
miniQMT下载历史行情数据太慢怎么办?一招提速10倍!
前端·python
databook18 小时前
Manim实现脉冲闪烁特效
后端·python·动效
程序设计实验室18 小时前
2025年了,在 Django 之外,Python Web 框架还能怎么选?
python
倔强青铜三20 小时前
苦练Python第46天:文件写入与上下文管理器
人工智能·python·面试