这个问题类似于(并且可能是一个简单的扩展)这里链接的问题:
来自上述链接的解决方案综合如下:
首先,让我们使用关于决策树结构的 scikit 文档来获取有关所构建树的信息:
n_nodes = clf.tree_.node_count children_left = clf.tree_.children_left children_right = clf.tree_.children_right feature = clf.tree_.feature threshold = clf.tree_.threshold然后我们定义两个递归函数。第一个将找到从树根开始的路径以创建特定节点(在我们的例子中是所有叶子)。第二个将编写用于使用其创建路径创建节点的特定规则:
def find_path(node_numb, path, x): path.append(node_numb) if node_numb == x: return True left = False right = False if (children_left[node_numb] !=-1): left = find_path(children_left[node_numb], path, x) if (children_right[node_numb] !=-1): right = find_path(children_right[node_numb], path, x) if left or right : return True path.remove(node_numb) return False def get_rule(path, column_names): mask = '' for index, node in enumerate(path): #We check if we are not in the leaf if index!=len(path)-1: # Do we go under or over the threshold ? if (children_left[node] == path[index+1]): mask += "(df['{}']<= {}) \t ".format(column_names[feature[node]], threshold[node]) else: mask += "(df['{}']> {}) \t ".format(column_names[feature[node]], threshold[node]) # We insert the & at the right places mask = mask.replace("\t", "&", mask.count("\t") - 1) mask = mask.replace("\t", "") return mask最后,我们使用这两个函数首先存储每个叶子的创建路径。然后存储用于创建每个叶子的规则:
Leaves leave_id = clf.apply(X_test) paths ={} for leaf in np.unique(leave_id): path_leaf = [] find_path(0, path_leaf, leaf) paths[leaf] = np.unique(np.sort(path_leaf)) rules = {} for key in paths: rules[key] = get_rule(paths[key], pima.columns)使用您提供的数据,输出为:
rules = {3: "(df['insulin']<= 127.5) & (df['bp']<= 26.450000762939453) & (df['bp']<= 9.100000381469727) ", 4: "(df['insulin']<= 127.5) & (df['bp']<= 26.450000762939453) & (df['bp']> 9.100000381469`727)", 6: "(df['insulin']<= 127.5) & (df['bp']> 26.450000762939453) & (df['skin']<= 27.5) ", 7: "(df['insulin']<= 127.5) & (df['bp']> 26.450000762939453 & (df['skin']> 27.5) ", 10: "(df['insulin']> 127.5) & (df['bp']<= 28.149999618530273) &(df['insulin']<= 145.5) ", 11: "(df['insulin']> 127.5) & (df['bp']<= 28.149999618530273) & (df['insulin']> 145.5) ", 13: "(df['insulin']> 127.5) & (df['bp']> 28.149999618530273) & (df['insulin']<= 158.5) ", 14: "(df['insulin']> 127.5) & (df['bp']> 28.149999618530273) & (df['insulin']> 158.5) "}由于规则是字符串,你不能直接使用 df[rules[3]] 调用它们,你必须像这样使用 eval 函数 df[eval(rules[3])]
上面发布的解决方案非常适合查找每个终止节点的路径。我想知道是否可以以与上述链接(字典/列表格式)完全相同的输出格式存储每个节点(叶子和终止节点)的路径。
谢谢!