python - 如何使用 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。你知道我该怎么做吗?
解决方案
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))
然后,您将获得一些可以帮助您的措施。
推荐阅读
- mysql - LEFT JOIN 返回错误的总数
- javascript - 将附加信息传递给 EventListener 回调
- php - AltoRouter 发送 Mime 类型
- java - 如何使用 spring/java 在独立服务器中运行记录的 selenium 测试用例
- java - 验证文件的清晰方法
- html - 为什么输入 url 参数时 http-server 上的 Node.js 会冻结
- python - 项目中自定义第三方python包的正确方法
- java - 如何在java中使用本机android代码返回当前位置
- sql - 在 web2py 和 SQLFORM.grid 中使用两个条件查询
- css - 我可以以更简单的方式将 css 类指定为一种标签吗?