module mltree.tree_structure
¶
Functions¶
function 
truncated documentation 

Returns the tree object. 

Returns the leave every observations of X falls into. 

Finds the common node to nodes i and j. 

Lists nodes involved into the path to find node i. 

Returns the indices of every leave in a tree. 

The function determines which leaves are neighbors. The method uses some memory as it creates creates a grid of … 

Returns a dictionary 

Determines the ranges for a node all dimensions. 
Documentation¶
Helpers to investigate a tree structure.

mlinsights.mltree.tree_structure.
predict_leaves
(model, X)[source]¶ Returns the leave every observations of X falls into.
 Parameters
model – a decision tree
X – observations
 Returns
array of leaves

mlinsights.mltree.tree_structure.
tree_find_common_node
(tree, i, j, parents=None)[source]¶ Finds the common node to nodes i and j.
 Parameters
tree – tree
i – node index (
tree.nodes[i]
)j – node index (
tree.nodes[j]
)parents – precomputed parents (None > calls
tree_node_range
)
 Returns
common root, remaining path to i, remaining path to j

mlinsights.mltree.tree_structure.
tree_find_path_to_root
(tree, i, parents=None)[source]¶ Lists nodes involved into the path to find node i.
 Parameters
tree – tree
i – node index (
tree.nodes[i]
)parents – precomputed parents (None > calls
tree_node_range
)
 Returns
one array of size (D, 2) where D is the number of dimensions

mlinsights.mltree.tree_structure.
tree_leave_index
(model)[source]¶ Returns the indices of every leave in a tree.
 Parameters
model – something which has a member
tree_
 Returns
leave indices

mlinsights.mltree.tree_structure.
tree_leave_neighbors
(model)[source]¶ The function determines which leaves are neighbors. The method uses some memory as it creates creates a grid of the feature spaces, each split multiplies the number of cells by two.
 Parameters
model – a sklearn.tree.DecisionTreeRegressor, a sklearn.tree.DecisionTreeClassifier, a model which has a member
tree_
 Returns
a dictionary
{(i, j): (dimension, x1, x2)}
, i, j are node indices, if , the observations goes to node i, j otherwise, i < j. The border is somewhere in the segment[x1, x2]
.
The following example shows what the function returns in case of simple grid in two dimensions.
<<<
import numpy from sklearn.tree import DecisionTreeClassifier from mlinsights.mltree import tree_leave_neighbors X = numpy.array([[0, 0], [0, 1], [0, 2], [1, 0], [1, 1], [1, 2], [2, 0], [2, 1], [2, 2]]) y = list(range(X.shape[0])) clr = DecisionTreeClassifier(max_depth=4) clr.fit(X, y) nei = tree_leave_neighbors(clr) import pprint pprint.pprint(nei)
>>>
{(2, 4): [(0, (0.0, 0.0), (1.0, 0.0))], (2, 8): [(1, (0.0, 0.0), (0.0, 1.0))], (4, 5): [(0, (1.0, 0.0), (2.0, 0.0))], (4, 10): [(1, (1.0, 0.0), (1.0, 1.0))], (5, 11): [(1, (2.0, 0.0), (2.0, 1.0))], (8, 10): [(0, (0.0, 1.0), (1.0, 1.0))], (8, 13): [(1, (0.0, 1.0), (0.0, 2.0))], (10, 11): [(0, (1.0, 1.0), (2.0, 1.0))], (10, 15): [(1, (1.0, 1.0), (1.0, 2.0))], (11, 16): [(1, (2.0, 1.0), (2.0, 2.0))], (13, 15): [(0, (0.0, 2.0), (1.0, 2.0))], (15, 16): [(0, (1.0, 2.0), (2.0, 2.0))]}

mlinsights.mltree.tree_structure.
tree_node_parents
(tree)[source]¶ Returns a dictionary
{node_id: parent_id}
. Parameters
tree – tree
 Returns
parents

mlinsights.mltree.tree_structure.
tree_node_range
(tree, i, parents=None)[source]¶ Determines the ranges for a node all dimensions.
nan
means infinity. Parameters
tree – tree
i – node index (
tree.nodes[i]
)parents – precomputed parents (None > calls
tree_node_range
)
 Returns
one array of size (D, 2) where D is the number of dimensions
The following example shows what the function returns in case of simple grid in two dimensions.
<<<
import numpy from sklearn.tree import DecisionTreeClassifier from mlinsights.mltree import tree_leave_index, tree_node_range X = numpy.array([[0, 0], [0, 1], [0, 2], [1, 0], [1, 1], [1, 2], [2, 0], [2, 1], [2, 2]]) y = list(range(X.shape[0])) clr = DecisionTreeClassifier(max_depth=4) clr.fit(X, y) leaves = tree_leave_index(clr) ra = tree_node_range(clr, leaves[0]) print(ra)
>>>
[[nan 0.5] [nan 0.5]]