Esempio n. 1
0
    def _get_leaf_nodes(self, node, i_tree, class_label, box):
        from copy import deepcopy
        from art.metrics.verification_decisions_trees import LeafNode, Box, Interval

        leaf_nodes = list()

        if 'split_index' in node:
            node_left = node['left_child']
            node_right = node['right_child']

            box_left = deepcopy(box)
            box_right = deepcopy(box)

            feature = node['split_feature']
            box_split_left = Box(intervals={feature: Interval(-np.inf, node['threshold'])})
            box_split_right = Box(intervals={feature: Interval(node['threshold'], np.inf)})

            if box.intervals:
                box_left.intersect_with_box(box_split_left)
                box_right.intersect_with_box(box_split_right)
            else:
                box_left = box_split_left
                box_right = box_split_right

            leaf_nodes += self._get_leaf_nodes(node_left, i_tree, class_label, box_left)
            leaf_nodes += self._get_leaf_nodes(node_right, i_tree, class_label, box_right)

        if 'leaf_index' in node:
            leaf_nodes.append(LeafNode(tree_id=i_tree, class_label=class_label, node_id=node['leaf_index'], box=box,
                                       value=node['leaf_value']))

        return leaf_nodes
Esempio n. 2
0
    def _get_leaf_nodes(self, node_id, i_tree, class_label, box):
        from copy import deepcopy
        from art.metrics.verification_decisions_trees import LeafNode, Box, Interval

        leaf_nodes = list()

        if self.get_left_child(node_id) != self.get_right_child(node_id):

            node_left = self.get_left_child(node_id)
            node_right = self.get_right_child(node_id)

            box_left = deepcopy(box)
            box_right = deepcopy(box)

            feature = self.get_feature_at_node(node_id)
            box_split_left = Box(intervals={feature: Interval(-np.inf, self.get_threshold_at_node(node_id))})
            box_split_right = Box(intervals={feature: Interval(self.get_threshold_at_node(node_id), np.inf)})

            if box.intervals:
                box_left.intersect_with_box(box_split_left)
                box_right.intersect_with_box(box_split_right)
            else:
                box_left = box_split_left
                box_right = box_split_right

            leaf_nodes += self._get_leaf_nodes(node_left, i_tree, class_label, box_left)
            leaf_nodes += self._get_leaf_nodes(node_right, i_tree, class_label, box_right)

        else:
            leaf_nodes.append(LeafNode(tree_id=i_tree, class_label=class_label, node_id=node_id, box=box,
                                       value=self.get_values_at_node(node_id)[0, 0]))

        return leaf_nodes
Esempio n. 3
0
    def _get_leaf_nodes(self, node, i_tree, class_label,
                        box) -> List["LeafNode"]:
        from art.metrics.verification_decisions_trees import LeafNode, Box, Interval

        leaf_nodes: List[LeafNode] = []

        if "children" in node:
            if node["children"][0]["nodeid"] == node["yes"] and node[
                    "children"][1]["nodeid"] == node["no"]:
                node_left = node["children"][0]
                node_right = node["children"][1]
            elif node["children"][1]["nodeid"] == node["yes"] and node[
                    "children"][0]["nodeid"] == node["no"]:
                node_left = node["children"][1]
                node_right = node["children"][0]
            else:
                raise ValueError

            box_left = deepcopy(box)
            box_right = deepcopy(box)

            feature = int(node["split"][1:])
            box_split_left = Box(
                intervals={
                    feature: Interval(-np.inf, node["split_condition"])
                })
            box_split_right = Box(
                intervals={feature: Interval(node["split_condition"], np.inf)})

            if box.intervals:
                box_left.intersect_with_box(box_split_left)
                box_right.intersect_with_box(box_split_right)
            else:
                box_left = box_split_left
                box_right = box_split_right

            leaf_nodes += self._get_leaf_nodes(node_left, i_tree, class_label,
                                               box_left)
            leaf_nodes += self._get_leaf_nodes(node_right, i_tree, class_label,
                                               box_right)

        if "leaf" in node:
            leaf_nodes.append(
                LeafNode(
                    tree_id=i_tree,
                    class_label=class_label,
                    node_id=node["nodeid"],
                    box=box,
                    value=node["leaf"],
                ))

        return leaf_nodes
Esempio n. 4
0
    def _get_leaf_nodes(self, node, i_tree, class_label, box):
        from copy import deepcopy
        from art.metrics.verification_decisions_trees import LeafNode, Box, Interval

        leaf_nodes = list()

        if 'children' in node:
            if node['children'][0]['nodeid'] == node['yes'] and node[
                    'children'][1]['nodeid'] == node['no']:
                node_left = node['children'][0]
                node_right = node['children'][1]
            elif node['children'][1]['nodeid'] == node['yes'] and node[
                    'children'][0]['nodeid'] == node['no']:
                node_left = node['children'][1]
                node_right = node['children'][0]
            else:
                raise ValueError

            box_left = deepcopy(box)
            box_right = deepcopy(box)

            feature = int(node['split'][1:])
            box_split_left = Box(
                intervals={
                    feature: Interval(-np.inf, node['split_condition'])
                })
            box_split_right = Box(
                intervals={feature: Interval(node['split_condition'], np.inf)})

            if box.intervals:
                box_left.intersect_with_box(box_split_left)
                box_right.intersect_with_box(box_split_right)
            else:
                box_left = box_split_left
                box_right = box_split_right

            leaf_nodes += self._get_leaf_nodes(node_left, i_tree, class_label,
                                               box_left)
            leaf_nodes += self._get_leaf_nodes(node_right, i_tree, class_label,
                                               box_right)

        if 'leaf' in node:
            leaf_nodes.append(
                LeafNode(tree_id=i_tree,
                         class_label=class_label,
                         node_id=node['nodeid'],
                         box=box,
                         value=node['leaf']))

        return leaf_nodes
    def get_trees(self):
        """
        Get the decision trees.

        :return: A list of decision trees.
        :rtype: `[Tree]`
        """

        import json
        from art.metrics.verification_decisions_trees import Box, Tree

        booster_dump = self._model.get_booster().get_dump(dump_format="json")
        trees = list()

        for i_tree, tree_dump in enumerate(booster_dump):
            box = Box()

            if self._model.n_classes_ == 2:
                class_label = -1
            else:
                class_label = i_tree % self._model.n_classes_

            tree_json = json.loads(tree_dump)
            trees.append(
                Tree(class_id=class_label,
                     leaf_nodes=self._get_leaf_nodes(tree_json, i_tree,
                                                     class_label, box)))

        return trees
Esempio n. 6
0
    def get_trees(self) -> list:
        """
        Get the decision trees.

        :return: A list of decision trees.
        """
        from art.metrics.verification_decisions_trees import Box, Tree

        booster_dump = self._model.dump_model()["tree_info"]
        trees = list()

        for i_tree, tree_dump in enumerate(booster_dump):
            box = Box()

            # pylint: disable=W0212
            if self._model._Booster__num_class == 2:
                class_label = -1
            else:
                class_label = i_tree % self._model._Booster__num_class

            trees.append(
                Tree(
                    class_id=class_label,
                    leaf_nodes=self._get_leaf_nodes(
                        tree_dump["tree_structure"], i_tree, class_label, box),
                ))

        return trees
Esempio n. 7
0
    def get_trees(self):
        """
        Get the decision trees.

        :return: A list of decision trees.
        :rtype: `[Tree]`
        """
        from art.metrics.verification_decisions_trees import Box, Tree

        trees = list()

        for i_tree, decision_tree_model in enumerate(self._model.estimators_):
            box = Box()

            #     if num_classes == 2:
            #         class_label = -1
            #     else:
            #         class_label = i_tree % num_classes

            decision_tree_classifier = ScikitlearnDecisionTreeClassifier(model=decision_tree_model)

            for i_class in range(self._model.n_classes_):
                class_label = i_class

                # pylint: disable=W0212
                trees.append(Tree(class_id=class_label,
                                  leaf_nodes=decision_tree_classifier._get_leaf_nodes(0, i_tree, class_label, box)))

        return trees
Esempio n. 8
0
    def get_trees(self):
        """
        Get the decision trees.

        :return: A list of decision trees.
        :rtype: `[Tree]`
        """
        from art.metrics.verification_decisions_trees import Box, Tree

        trees = list()
        num_trees, num_classes = self._model.estimators_.shape

        for i_tree in range(num_trees):
            box = Box()

            for i_class in range(num_classes):
                decision_tree_classifier = ScikitlearnDecisionTreeRegressor(
                    model=self._model.estimators_[i_tree, i_class])

                if num_classes == 2:
                    class_label = None
                else:
                    class_label = i_class

                # pylint: disable=W0212
                trees.append(Tree(class_id=class_label,
                                  leaf_nodes=decision_tree_classifier._get_leaf_nodes(0, i_tree, class_label, box)))

        return trees
Esempio n. 9
0
    def _get_leaf_nodes(self, node, i_tree, class_label,
                        box) -> List["LeafNode"]:
        from art.metrics.verification_decisions_trees import Box, Interval, LeafNode

        leaf_nodes: List[LeafNode] = list()

        if "split_index" in node:
            node_left = node["left_child"]
            node_right = node["right_child"]

            box_left = deepcopy(box)
            box_right = deepcopy(box)

            feature = node["split_feature"]
            box_split_left = Box(
                intervals={feature: Interval(-np.inf, node["threshold"])})
            box_split_right = Box(
                intervals={feature: Interval(node["threshold"], np.inf)})

            if box.intervals:
                box_left.intersect_with_box(box_split_left)
                box_right.intersect_with_box(box_split_right)
            else:
                box_left = box_split_left
                box_right = box_split_right

            leaf_nodes += self._get_leaf_nodes(node_left, i_tree, class_label,
                                               box_left)
            leaf_nodes += self._get_leaf_nodes(node_right, i_tree, class_label,
                                               box_right)

        if "leaf_index" in node:
            leaf_nodes.append(
                LeafNode(
                    tree_id=i_tree,
                    class_label=class_label,
                    node_id=node["leaf_index"],
                    box=box,
                    value=node["leaf_value"],
                ))

        return leaf_nodes