У меня есть следующий алгоритм дерева, который печатает условия для каждого листа:
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:"]