我正在创建一个决策树。我的数据类型如下:
X1 |X2 |X3|.....X50|Y_____________________________________1 |5 |7 |.....0 |11.5|34 |81|.....0 |14 |21 |21|.... 1 |065 |34 |23|.....1 |1
我正在尝试执行以下代码:
X_train = data.iloc[:,0:51]Y_train = data.iloc[:,51]clf = DecisionTreeClassifier(criterion = "entropy", random_state = 100, max_depth=8, min_samples_leaf=15)clf.fit(X_train, y_train)
我想要的是预测特定类(在本例中是“0”)的决策规则。例如,
当 X1 > 4 且 X5 > 78 且 X50 = 100 时,Y = 0(概率 = 84%)当 X4 = 56 且 X39 < 100 时,Y = 0(概率 = 93%)...
所以基本上我想要所有叶节点及其关联的决策规则,以及预测Y=0的概率,这些规则预测类Y=“0”。我还希望以上述指定格式打印这些决策规则。
我不关心预测(Y=1)的决策规则
谢谢,任何帮助都将不胜感激
回答:
基于 http://scikit-learn.org/stable/auto_examples/tree/plot_unveil_tree_structure.html
假设每个节点中类别的比例等于概率,例如,如果叶节点包含68个类别0的实例和15个类别1的实例(即tree_
中的value
是[68,15]),那么概率是[0.81927711, 0.18072289]
。
生成一个简单的树,4个特征,2个类别:
import numpy as npfrom sklearn.tree import DecisionTreeClassifierfrom sklearn.datasets import make_classificationfrom sklearn.cross_validation import train_test_splitfrom sklearn.tree import _treeX, y = make_classification(n_informative=3, n_features=4, n_samples=200, n_redundant=1, random_state=42, n_classes=2)feature_names = ['X0','X1','X2','X3']Xtrain, Xtest, ytrain, ytest = train_test_split(X,y, random_state=42)clf = DecisionTreeClassifier(max_depth=2)clf.fit(Xtrain, ytrain)
可视化它:
from sklearn.externals.six import StringIO from sklearn import treeimport pydot dot_data = StringIO() tree.export_graphviz(clf, out_file=dot_data) graph = pydot.graph_from_dot_data(dot_data.getvalue()) [0]graph.write_jpeg('1.jpeg')
创建一个函数,用于打印一个实例的条件:
node_indicator = clf.decision_path(Xtrain)n_nodes = clf.tree_.node_countfeature = clf.tree_.featurethreshold = clf.tree_.thresholdleave_id = clf.apply(Xtrain)def value2prob(value): return value / value.sum(axis=1).reshape(-1, 1)def print_condition(sample_id): print("当", end=' ') node_index = node_indicator.indices[node_indicator.indptr[sample_id]: node_indicator.indptr[sample_id + 1]] for n, node_id in enumerate(node_index): if leave_id[sample_id] == node_id: values = clf.tree_.value[node_id] probs = value2prob(values) print('则 Y={} (概率={}) (值={})'.format( probs.argmax(), probs.max(), values)) continue if n > 0: print('且 ', end='') if (Xtrain[sample_id, feature[node_id]] <= threshold[node_id]): threshold_sign = "<=" else: threshold_sign = ">" if feature[node_id] != _tree.TREE_UNDEFINED: print( "%s %s %s" % ( feature_names[feature[node_id]], #Xtrain[sample_id,feature[node_id]] # 实际值 threshold_sign, threshold[node_id]), end=' ')
在第一行上调用它:
>>> print_condition(0)当 X1 > -0.2662498950958252 且 X0 > -1.1966443061828613 则 Y=1 (概率=0.9672131147540983) (值=[[ 2. 59.]])
在所有预测值为零的行上调用它:
[print_condition(i) for i in (clf.predict(Xtrain) == 0).nonzero()[0]]