Sklearn - 如何预测所有目标标签的概率 [英] Sklearn - How to predict probability for all target labels

查看:68
本文介绍了Sklearn - 如何预测所有目标标签的概率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据集,其中的目标变量可以有 7 个不同的标签.我的训练集中的每个样本只有一个目标变量的标签.

I have a data set with a target variable that can have 7 different labels. Each sample in my training set has only one label for the target variable.

对于每个样本,我想计算每个目标标签的概率.所以我的预测将包含每行 7 个概率.

For each sample, I want to calculate the probability for each of the target labels. So my prediction would consist of 7 probabilities for each row.

在 sklearn 网站上我阅读了关于多标签分类的内容,但这似乎不是我想要的.

On the sklearn website I read about multi-label classification, but this doesn't seem to be what I want.

我尝试了以下代码,但这仅为每个样本提供了一个分类.

I tried the following code, but this only gives me one classification per sample.

from sklearn.multiclass import OneVsRestClassifier
clf = OneVsRestClassifier(DecisionTreeClassifier())
clf.fit(X_train, y_train)
pred = clf.predict(X_test)

有人对此有什么建议吗?谢谢!

Does anyone have some advice on this? Thanks!

推荐答案

您只需删除 OneVsRestClassifer 并使用 predict_proba DecisionTreeClassifier.您可以执行以下操作:

You can do that by simply removing the OneVsRestClassifer and using predict_proba method of the DecisionTreeClassifier. You can do the following:

clf = DecisionTreeClassifier()
clf.fit(X_train, y_train)
pred = clf.predict_proba(X_test)

这将为您提供 7 个可能类别中的每一个的概率.

This will give you a probability for each of your 7 possible classes.

希望有帮助!

这篇关于Sklearn - 如何预测所有目标标签的概率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆