繁体   English   中英

python上的树递归

[英]Tree recursion on python

我有以下树算法,它打印每个叶子的条件:

def _grow_tree(self, X, y, depth=0):
    # Identify best split
    idx, thr = self._best_split(X, y)

    # Indentation for tree description
    indent = "    " * depth

    indices_left = X.iloc[:, idx] < thr
    X_left = X[indices_left]
    y_left = y_train[X_left.reset_index().loc[:,'id'].values]

    X_right = X[~indices_left]
    y_right = y_train[X_right.reset_index().loc[:,'id'].values]

    self.tree_describe.append(indent +"if x['"+ X.columns[idx] + "'] <= " +\
                             str(thr) + ':')
    # Grow on left side of the tree  
    node.left = self._grow_tree(X_left, y_left, depth + 1)

    self.tree_describe.append(indent +"else: #if x['"+ X.columns[idx] + "'] > " +\
                         str(thr) + ':')
    # Grow on right side of the tree
    node.right = self._grow_tree(X_right, y_right, depth + 1)

    return node

这会为特定情况生成以下打印:

["if x['VAR1'] <= 0.5:",
 "    if x['VAR2'] <= 0.5:",
 "    else: #if x['VAR2'] > 0.5:",
 "else: #if x['VAR1'] > 0.5:",
 "    if x['VAR3'] <= 0.5:",
 "    else: #if x['VAR3'] > 0.5:"]

我怎样才能获得以下输出?:

["if x['VAR1'] <= 0.5:",
 "    if x['VAR1'] <= 0.5&x['VAR2'] <= 0.5",
 "    else: #if x['VAR1'] <= 0.5&x['VAR2'] > 0.5:",
 "else: #if x['VAR1'] > 0.5:",
 "    if x['VAR1'] > 0.5&x['VAR3'] <= 0.5:",
 "    else: #if x['VAR1'] > 0.5&x['VAR3'] > 0.5:"]

您可以为您的函数引入一个新参数,该参数将具有需要添加到每个更深条件的更高级别条件的字符串:

我还建议使用.format()来构建字符串:

def _grow_tree(self, X, y, depth=0, descr=""):

    idx, thr = self._best_split(X, y)

    indent = "    " * depth

    cond = "x['{}'] <= {}{}".format(X.columns[idx], thr, descr)
    self.tree_describe.append("{}if {}:".format(indent, cond))

    node.left = self._grow_tree(X_left, y_left, depth + 1, " & " + cond)

    cond = "x['{}'] > {}{}".format(X.columns[idx], thr, descr)
    self.tree_describe.append("{}else: #if {}:".format(indent, cond))

    node.right = self._grow_tree(X_right, y_right, depth + 1, " & " + cond)

    return node

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM