python - 决策树: Probability of prediction inversely proportional in python

标签 python machine-learning scikit-learn classification decision-tree

我想创建与决策树中每个类别成反比的预测概率。类似于所描述的here第 9 页 4.1 中的公式。 我该如何引用我的代码来做到这一点:

import numpy as np
import pandas as pd
from sklearn.cross_validation import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score
from sklearn import tree
url="https://archive.ics.uci.edu/ml/machine-learning-databases/abalone/abalone.data"
c=pd.read_csv(url, header=None)
X = c.values[:,1:8]
Y = c.values[:,0]
X_train, X_test, y_train, y_test = train_test_split( X, Y, test_size = 0.3, random_state = 100)
clf_entropy = DecisionTreeClassifier(criterion = "entropy", random_state = 100,
 max_depth=3, min_samples_leaf=5)
clf_entropy.fit(X_train, y_train)
probs = clf_entropy.predict_proba(X_test)
probs

目标是将零概率替换为 小的非零值并对概率进行归一化以使其成为分布。 然后选择标签,使得选择的概率成反比 与当前树的预测成比例。 enter image description here

最佳答案

上述方程可以通过以下代码片段实现。

def inverse_prob(model_probs):
    model_probs[model_probs == 0 ] = 1e-5
    inverse = 1/model_probs
    return inverse/inverse.sum(axis=0)

只要给定的概率分布中有零值,就添加一个小值 1e-5。

关于python - 决策树: Probability of prediction inversely proportional in python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53936310/

相关文章:

python - 如何将不规则张量与 tf.data 和 TFRecords 一起使用?

Python MysqlDB 使用 cursor.rowcount 和 SSDictCursor 返回错误计数

python - 寻找数字编码分类变量之间的相关性?

python-3.x - 何时使用 min-max-scaler 和 standardscaler

scikit-learn - Scikit HDBSCAN *树* 标记(不是单切片标记)

python - 为什么调整图像大小会导致 channel 数增加?

python - 分布式训练产生的神经网络是否是每个分布式节点中训练的神经网络的平均值?

python - 训练CNN时出错: "RuntimeError: The size of tensor a (10) must match the size of tensor b (64) at non-singleton dimension 1"

machine-learning - 集成模型和平均模型有什么区别?

python - 使用 UnbalancedDataset 包对数据集进行过采样时发生 KeyError(在 pandas.index.IndexEngine.get_loc 中)