python predict_proba_python – 如何从scikit-learn中的predict_proba中使用cross_val_predict获取类标签...

我需要使用3倍交叉验证训练

Random Forest classifier.对于每个样本,我需要在它恰好位于测试集中时检索预测概率.

我正在使用scikit-learn版本0.18.dev0.

此新版本添加了使用方法cross_val_predict()和附加参数方法来定义估计器需要哪种预测的功能.

在我的情况下,我想使用predict_proba()方法,它返回多类方案中每个类的概率.

但是,当我运行该方法时,我得到预测概率矩阵,其中每行代表一个样本,每列代表特定类的预测概率.

问题是该方法没有指出哪个类对应于每列.

我需要的值与属性classes_中返回的相同(在我的情况下使用RandomForestClassifier)定义为:

classes_ : array of shape = [n_classes] or a list of such arrays

The classes labels (single output problem), or a list of arrays of class labels (multi-output problem).

这是predict_proba()所需要的,因为在其文档中写道:

The order of the classes corresponds to that in the attribute classes_.

最小的例子如下:

import numpy as np

from sklearn.ensemble import RandomForestClassifier

from sklearn.model_selection import cross_val_predict

clf = RandomForestClassifier()

X = np.random.randn(10, 10)

y = y = np.array([1] * 4 + [0] * 3 + [2] * 3)

# how to get classes from here?

proba = cross_val_predict(estimator=clf, X=X, y=y, method="predict_proba")

# using the classifier without cross-validation

# it is possible to get the classes in this way:

clf.fit(X, y)

proba = clf.predict_proba(X)

classes = clf.classes_

最佳答案 是的,它们将按顺序排列;这是因为DecisionTreeClassifier(它是RandomForestClassifier的默认base_estimator)

uses np.unique to construct the classes_ attribute,它返回输入数组的排序唯一值.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值