如何从scikit-learn决策树中提取决策规则?
作者:互联网
我可以从决策树中的受过训练的树中提取基础决策规则(或“决策路径”)作为文本列表吗?
就像是:
如果A> 0.4,那么如果B <0.2那么如果C> 0.8则那么class ='X' 谢谢你的帮助.
解决方法:
我相信这个答案比其他答案更正确:
from sklearn.tree import _tree
def tree_to_code(tree, feature_names):
tree_ = tree.tree_
feature_name = [
feature_names[i] if i != _tree.TREE_UNDEFINED else "undefined!"
for i in tree_.feature
]
print "def tree({}):".format(", ".join(feature_names))
def recurse(node, depth):
indent = " " * depth
if tree_.feature[node] != _tree.TREE_UNDEFINED:
name = feature_name[node]
threshold = tree_.threshold[node]
print "{}if {} <= {}:".format(indent, name, threshold)
recurse(tree_.children_left[node], depth + 1)
print "{}else: # if {} > {}".format(indent, name, threshold)
recurse(tree_.children_right[node], depth + 1)
else:
print "{}return {}".format(indent, tree_.value[node])
recurse(0, 1)
这将打印出有效的Python函数.以下是尝试返回其输入的树的示例输出,该数字介于0和10之间.
def tree(f0):
if f0 <= 6.0:
if f0 <= 1.5:
return [[ 0.]]
else: # if f0 > 1.5
if f0 <= 4.5:
if f0 <= 3.5:
return [[ 3.]]
else: # if f0 > 3.5
return [[ 4.]]
else: # if f0 > 4.5
return [[ 5.]]
else: # if f0 > 6.0
if f0 <= 8.5:
if f0 <= 7.5:
return [[ 7.]]
else: # if f0 > 7.5
return [[ 8.]]
else: # if f0 > 8.5
return [[ 9.]]
以下是我在其他答案中看到的一些绊脚石:
>使用tree_.threshold == -2来确定节点是否为叶子不是一个好主意.如果它是一个阈值为-2的真实决策节点怎么办?相反,您应该查看tree.feature或tree.children_ *.
> line feature = [feature_ames [i] for i in tree_.feature]与我的sklearn版本崩溃,因为tree.tree_.feature的某些值为-2(特别是对于叶节点).
>递归函数中不需要多个if语句,只需一个就可以了.
标签:decision-tree,random-forest,python,scikit-learn,machine-learning 来源: https://codeday.me/bug/20190915/1804838.html