如何用 predict_proba 获得每个样本的所有 类 的独立概率?

How to get independent probabilities of all classes for each sample with predict_proba?

在我的工作中,有一个完全由布尔数据组成的特征集,并且有 classes 属于特征。 类 是字符串。

     feature set              class (String)
[True False True   ...]        "A"
[True True  True   ...]        "B"
[True True  False   ...]       "C"

当我用随机森林算法训练这些数据时,

factor = pd.factorize(classes)
classes = factor[0]

classifier = RandomForestClassifier(n_estimators=100, criterion="entropy", random_state=0)
classifier.fit(x_train, classes)

classifier 可以正确检测到 97% 的 classes。当我这样做时

classifier.predict_proba(sample1_feature_set)

它给出了样本 1 每个 class 的相对概率。例如;喜欢

 [0.80    0.05    0.15]
   ↓        ↓        ↓
  Prob.    Prob.    Prob.
   of       of       of
  "A"      "B"      "C" 
  for      for      for
sample1   sample1  sample1

所以当我把list(0.80 + 0.05 + 0.15)的值相加时,结果总是1。这说明其实是做相对评价,就是1的概率class影响概率另一个 class.

我想得到样本 1 的所有 classes 的独立概率,比如

 [0.95    0.69    0.87]
   ↓        ↓        ↓
  Prob.    Prob.    Prob.
   of       of       of
  "A"      "B"      "C" 
  for      for      for
sample1   sample1  sample1

样本 1 是“A”的 %95、“B”的 %69 和“C”的 %87 class。你知道我该怎么做吗?

predict_prob 计算每个 class 的 个样本 的概率。 [0.95 0.05] 表示在模型决策树的 95% 中,这些 unique sample 的输出是 class 0; 5% 是 class 1。所以你正在一个一个地评估每个样本。

当你这样做时:

classifier.predict_proba(example_feature_set)[0]

您正在获取 example_feature_set 的第一个样本每个 class 的概率。

我想你想要的是每个 class 的准确率或召回率。 (如果你不熟悉,请检查这些分数的含义)。

为了计算这些,我推荐下面的代码:

from sklearn.metrics import classification_report
y_pred=classifier.predict(example_feature_set) #I'm assuming you have more than one sample to predict
print(classification_report(y_test,y_pred))

那么您将获得一些可以帮助您的措施。

随机森林是 ensemble method。基本上,它使用不同的数据子集(称为装袋)构建单独的决策树,并对所有树的预测进行平均,从而为您提供概率。帮助页面实际上是一个很好的起点:

In averaging methods, the driving principle is to build several estimators independently and then to average their predictions. On average, the combined estimator is usually better than any of the single base estimator because its variance is reduced.

Examples: Bagging methods, Forests of randomized trees, …

因此概率总和为 1。以下是您如何访问每棵树的单独预测的示例:

from sklearn.datasets import load_iris
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, test_size=0.33, random_state=42)

from sklearn.ensemble import RandomForestClassifier
model = RandomForestClassifier(n_estimators=10)
model.fit(X_train, y_train)

pred = model.predict_proba(X_test)
pred[:5,:]

array([[0. , 1. , 0. ],
       [1. , 0. , 0. ],
       [0. , 0. , 1. ],
       [0. , 0.9, 0.1],
       [0. , 0.9, 0.1]])

这是对第一棵树的预测:

model.estimators_[0].predict(X_test)
Out[42]: 
array([1., 0., 2., 2., 1., 0., 1., 2., 2., 1., 2., 0., 0., 0., 0., 2., 2.,
       1., 1., 2., 0., 2., 0., 2., 2., 2., 2., 2., 0., 0., 0., 0., 1., 0.,
       0., 2., 1., 0., 0., 0., 2., 2., 1., 0., 0., 1., 1., 2., 1., 2.])

我们计算所有树:

result = np.zeros((len(X_test),3))
for i in range(len(model.estimators_)):
    p = model.estimators_[i].predict(X_test).astype(int)
    result[range(len(X_test)),p] += 1

result[:5,:]
Out[63]: 
array([[ 0., 10.,  0.],
       [10.,  0.,  0.],
       [ 0.,  0., 10.],
       [ 0.,  9.,  1.],
       [ 0.,  9.,  1.]])

用这个除以树的数量得到你之前得到的概率:

result/10
Out[65]: 
array([[0. , 1. , 0. ],
       [1. , 0. , 0. ],
       [0. , 0. , 1. ],
       [0. , 0.9, 0.1],
       [0. , 0.9, 0.1],