2017-09-06 51 views
0

我已经拟合了DecisionTreeClassifier的一个实例,并且我试图为每个节点提取预测概率。我需要这个以创建类似于下面所示的自定义决策树可视化。来自训练过的sklearn中树节点的预测分类概率DecisionTreeClassifier

我可以导出每个节点的功能和阈值。

dtc.tree_.feature 
Out[72]: array([93, 36, 92, 51, 84, -2, 20, -2, -2, -2, -2, -2, 6, -2, -2]) 

dtc.tree_.threshold 
Out[73]: 
array([ 50.5  , 0.5  , 85.50991821, 0.5  , 
     5.5  , -2.  , 0.5  , -2.  , 
     -2.  , -2.  , -2.  , -2.  , 
     0.5  , -2.  , -2.  ]) 

理想情况下,我会使用类似于此的每个节点导出预测概率。

dtc.tree_.probability 
Out[xx]: 
array([0.50, 0.42, 0.21, 0.45, 0.62, ....]) 

这可能吗?

enter image description here

+0

也许这可以帮助:这样,我可以使用下面的计算它http://scikit-learn.org/stable/auto_examples/tree/plot_unveil_tree_structure.html#sphx-glr-auto-examples-tree- plot-unveil-tree-structure-py –

+0

这个例子帮助我找到了特征和阈值,但是我没有看到一种方法来为每个节点提取概率,而不是基于每个决策路径人工创建一个样本。这只会让我终端节点:/ – Selah

回答

0

我发现,值是落在每个类和我的“预测概率”样本的数量可以认为落在特定类别的样本比例。

samples = dtc.tree_.n_node_samples 
class1_positives = dtc.tree_.value[:,0,1] 
probs = (class1_positives/samples).tolist() 
相关问题