首页 > 解决方案 > 如何使用 predict_proba 获得每个样本的所有类的独立概率?

问题描述

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

     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)

分类器可以正确检测 97% 的类别。当我做

classifier.predict_proba(sample1_feature_set)

它给出了 sample1 每个类的相对概率。例如; 喜欢

 [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。这说明它实际上是在进行相对评估,即一个类的概率影响另一类的概率。

我想获得sample1所有类的独立概率,比如

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

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

标签: pythonscikit-learnrandom-forestmulticlass-classification

解决方案


predict_prob计算每个类别的一个样本的概率。[0.95 0.05] 表示在模型的 95% 的决策树中,这些唯一样本的输出为0 类;5% 是第 1 类。因此,您正在逐个评估每个样本。

当你这样做时:

classifier.predict_proba(example_feature_set)[0]

您正在获得成为第一个样本的每个类的概率example_feature_set

我认为您想要的是每个班级的精确度或召回率。(如果您不熟悉,请检查这些分数的含义)。

要计算这些,我推荐以下代码:

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

然后,您将获得一些可以帮助您的措施。


推荐阅读