woodpecker/sklearn/decisiontree/decision_tree_structure.py
def show_decision_tree_prediction_path(self, sample, is_weighted=False):
        """Visual interpretation of prediction path.

        Show only the prediction path from a decision tree, instead of the whole tree.
        It helps to easily understand and follow the prediction path.
        The blue nodes are the nodes from prediction path and the black nodes are just blue nodes brothers.

        This kind of visualisation is very useful for debugging and understanding tree predictions.
        Also it is useful to explain to non technical people the reason behind tree predictions.

        :param is_weighted: boolean
            Whether or not to include weighted number of training samples reaching node i.
        :param sample: array of double, shape[features]
            The array of features values
        :return: graphviz.files.Source
        """

        logging.info(f"Make a prediction for sample {sample}")

        node_indicator = self.tree.decision_path([sample])
        decision_node_path = node_indicator.indices[node_indicator.indptr[0]:
                                                    node_indicator.indptr[1]]
        logging.info(f"decision path {decision_node_path}")

        g_tree = pgv.AGraph(strict=False, directed=True)
        g_tree.layout(prog='dot')
        for i in range(0, len(decision_node_path)):
            node_id = decision_node_path[i]
            node_label = self._get_node_path_info(node_id, sample, is_weighted)
            logging.debug(f"adding node id {node_id} with label {node_label}")

            g_tree.add_node(node_id, color="blue", label=node_label, fontsize=10, center=True, shape="ellipse")

            # check if node_id is not a leaf
            if self.children_left[node_id] != -1:
                g_tree.add_edge(node_id, self.children_left[node_id])

                # check if children_left[node_id] is not from the path and plot the node with black (neighbor node)
                if self.children_left[node_id] != decision_node_path[i + 1]:
                    left_node_id = self.children_left[node_id]
                    g_tree.add_node(left_node_id, label=self._get_node_path_info(left_node_id, sample, is_weighted),
                                    fontsize=10,
                                    center=True, shape="ellipse")

            # check if node_id is not a leaf
            if self.children_right[node_id] != -1:
                g_tree.add_edge(node_id, self.children_right[node_id])

                # check if children_right[node_id] is not from the path and plot the node with black (neighbor node)
                if self.children_right[node_id] != decision_node_path[i + 1]:
                    right_node_id = self.children_right[node_id]
                    g_tree.add_node(right_node_id, label=self._get_node_path_info(right_node_id, sample, is_weighted),
                                    fontsize=10,
                                    center=True, shape="ellipse")

        return graphviz.Source(g_tree.string())
Visual interpretation of prediction path.

        Show only the prediction path from a decision tree, instead of the whole tree.
        It helps to easily understand and follow the prediction path.
        The blue nodes are the nodes from prediction path and the black nodes are just blue nodes brothers.

        This kind of visualisation is very useful for debugging and understanding tree predictions.
        Also it is useful to explain to non technical people the reason behind tree predictions.

        :param is_weighted: boolean
            Whether or not to include weighted number of training samples reaching node i.
        :param sample: array of double, shape[features]
            The array of features values
        :return: graphviz.files.Source
        
Visual interpretation of prediction path.
Show only the prediction path from a decision tree, instead of the whole tree.
It helps to easily understand and follow the prediction path.
The blue nodes are the nodes from prediction path and the black nodes are just blue nodes brothers.

This kind of visualisation is very useful for debugging and understanding tree predictions.
Also it is useful to explain to non technical people the reason behind tree predictions.
Visual interpretation of prediction path . Show only the prediction path from a decision tree instead of the whole tree . It helps to easily understand and follow the prediction path . The blue nodes are the nodes from prediction path and the black nodes are just blue nodes brothers . This kind of visualisation is very useful for debugging and understanding tree predictions . Also it is useful to explain to non technical people the reason behind tree predictions .
def show_decision_tree_prediction_path(self, sample, is_weighted=False):
        logging.info(f"Make a prediction for sample {sample}")
        node_indicator = self.tree.decision_path([sample])
        decision_node_path = node_indicator.indices[node_indicator.indptr[0]:
                                                    node_indicator.indptr[1]]
        logging.info(f"decision path {decision_node_path}")
        g_tree = pgv.AGraph(strict=False, directed=True)
        g_tree.layout(prog='dot')
        for i in range(0, len(decision_node_path)):
            node_id = decision_node_path[i]
            node_label = self._get_node_path_info(node_id, sample, is_weighted)
            logging.debug(f"adding node id {node_id} with label {node_label}")
            g_tree.add_node(node_id, color="blue", label=node_label, fontsize=10, center=True, shape="ellipse")
            if self.children_left[node_id] != -1:
                g_tree.add_edge(node_id, self.children_left[node_id])
                if self.children_left[node_id] != decision_node_path[i + 1]:
                    left_node_id = self.children_left[node_id]
                    g_tree.add_node(left_node_id, label=self._get_node_path_info(left_node_id, sample, is_weighted),
                                    fontsize=10,
                                    center=True, shape="ellipse")
            if self.children_right[node_id] != -1:
                g_tree.add_edge(node_id, self.children_right[node_id])
                if self.children_right[node_id] != decision_node_path[i + 1]:
                    right_node_id = self.children_right[node_id]
                    g_tree.add_node(right_node_id, label=self._get_node_path_info(right_node_id, sample, is_weighted),
                                    fontsize=10,
                                    center=True, shape="ellipse")
        return graphviz.Source(g_tree.string())
def show_decision_tree_prediction_path ( self , sample , is_weighted = False ) : logging . info ( f"Make a prediction for sample {sample}" ) node_indicator = self . tree . decision_path ( [ sample ] ) decision_node_path = node_indicator . indices [ node_indicator . indptr [ 0 ] : node_indicator . indptr [ 1 ] ] logging . info ( f"decision path {decision_node_path}" ) g_tree = pgv . AGraph ( strict = False , directed = True ) g_tree . layout ( prog = 'dot' ) for i in range ( 0 , len ( decision_node_path ) ) : node_id = decision_node_path [ i ] node_label = self . _get_node_path_info ( node_id , sample , is_weighted ) logging . debug ( f"adding node id {node_id} with label {node_label}" ) g_tree . add_node ( node_id , color = "blue" , label = node_label , fontsize = 10 , center = True , shape = "ellipse" ) if self . children_left [ node_id ] != - 1 : g_tree . add_edge ( node_id , self . children_left [ node_id ] ) if self . children_left [ node_id ] != decision_node_path [ i + 1 ] : left_node_id = self . children_left [ node_id ] g_tree . add_node ( left_node_id , label = self . _get_node_path_info ( left_node_id , sample , is_weighted ) , fontsize = 10 , center = True , shape = "ellipse" ) if self . children_right [ node_id ] != - 1 : g_tree . add_edge ( node_id , self . children_right [ node_id ] ) if self . children_right [ node_id ] != decision_node_path [ i + 1 ] : right_node_id = self . children_right [ node_id ] g_tree . add_node ( right_node_id , label = self . _get_node_path_info ( right_node_id , sample , is_weighted ) , fontsize = 10 , center = True , shape = "ellipse" ) return graphviz . Source ( g_tree . string ( ) )
Visual interpretation of prediction path.
Visual interpretation of prediction path .
"""Visual interpretation of prediction path.

        Show only the prediction path from a decision tree, instead of the whole tree.
        It helps to easily understand and follow the prediction path.
        The blue nodes are the nodes from prediction path and the black nodes are just blue nodes brothers.

        This kind of visualisation is very useful for debugging and understanding tree predictions.
        Also it is useful to explain to non technical people the reason behind tree predictions.

        :param is_weighted: boolean
            Whether or not to include weighted number of training samples reaching node i.
        :param sample: array of double, shape[features]
            The array of features values
        :return: graphviz.files.Source
        """ # check if node_id is not a leaf # check if children_left[node_id] is not from the path and plot the node with black (neighbor node) # check if node_id is not a leaf # check if children_right[node_id] is not from the path and plot the node with black (neighbor node)
None
self
sample
array of double, shape[features]
The array of features values
array of double shape [ features ] The array of features values
is_weighted
boolean
Whether or not to include weighted number of training samples reaching node i.
boolean Whether or not to include weighted number of training samples reaching node i .