我可以从决策树中的训练树中提取基础决策规则(或“决策路径”)作为文本列表吗?
就像是:
if A>0.4 then if B<0.2 then if C>0.8 then 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
tree.feature
tree.children_*
features = [feature_names[i] for i in tree_.feature]
tree.tree_.feature