def classify_data_item(self, data_item, decision_tree):
        """
        Function that classifies a data item from test data set by traversing the trained decision tree.
        :param data_item:
        :param decision_tree:
        :return:
        """

        test_data = TestData()

        current_node = decision_tree

        split_feature_value = data_item[current_node.split_feature_index]

        # Traverse tree until it finds the leaf node or the split on the feature value is not found.
        while len(current_node.children
                  ) > 0 and split_feature_value in current_node.children:
            current_node = current_node.children[split_feature_value]
            split_feature_value = data_item[current_node.split_feature_index]

        classified = current_node.class_label

        test_data.predicted_class = classified
        test_data.is_error = True if data_item[0] != classified else False
        test_data.is_false_negative = True if data_item[0] == GlobalVectors.POSITIVE_VALUE \
                                              and classified == GlobalVectors.NEGATIVE_VALUE else False
        test_data.is_true_negative = True if data_item[0] == GlobalVectors.NEGATIVE_VALUE \
                                              and classified == GlobalVectors.NEGATIVE_VALUE else False
        test_data.is_false_positive = True if data_item[0] == GlobalVectors.NEGATIVE_VALUE \
                                              and classified == GlobalVectors.POSITIVE_VALUE else False
        test_data.is_true_positive = True if data_item[0] == GlobalVectors.POSITIVE_VALUE \
                                              and classified == GlobalVectors.POSITIVE_VALUE else False

        return test_data
示例#2
0
    def classify_data_item(self, data_item, learn_tree_list):
        """
        Function that classifies a data item from test data set by traversing the trained decision tree.
        :param data_item:
        :param decision_tree:
        :return:
        """

        result_dict = dict()

        test_data = TestData()

        # Iterating to all the learnt bag trees to find majority vote.
        for ltree in learn_tree_list:
            current_node = ltree.decision_tree

            split_feature_value = data_item[current_node.split_feature_index]

            # Traverse tree until it finds the leaf node or the split on the feature value is not found.
            while len(current_node.children
                      ) > 0 and split_feature_value in current_node.children:
                current_node = current_node.children[split_feature_value]
                split_feature_value = data_item[
                    current_node.split_feature_index]

            if current_node.class_label not in result_dict:
                result_dict[current_node.class_label] = 1
            else:
                result_dict[current_node.class_label] += 1

        # Getting the majority vote here.
        classified = self.get_majority_vote(result_dict)

        test_data.predicted_class = classified
        test_data.is_error = True if data_item[0] != classified else False
        test_data.is_false_negative = True if data_item[0] == GlobalVectors.POSITIVE_VALUE \
                                              and classified == GlobalVectors.NEGATIVE_VALUE else False
        test_data.is_true_negative = True if data_item[0] == GlobalVectors.NEGATIVE_VALUE \
                                              and classified == GlobalVectors.NEGATIVE_VALUE else False
        test_data.is_false_positive = True if data_item[0] == GlobalVectors.NEGATIVE_VALUE \
                                              and classified == GlobalVectors.POSITIVE_VALUE else False
        test_data.is_true_positive = True if data_item[0] == GlobalVectors.POSITIVE_VALUE \
                                              and classified == GlobalVectors.POSITIVE_VALUE else False

        return test_data