Beispiel #1
0
    def __init__(self, merge_leaf_func, criterion, max_depth=None):

        self.tree_ = None
        self.feature_importances_ = None
        self.output_shape_ = None

        self._tree_builder = TreeBuilder(merge_leaf_func=merge_leaf_func,
                                         criterion=criterion,
                                         max_depth=max_depth)
Beispiel #2
0
def main():
    tree = TreeBuilder()
    tree.build_tree(tree.file, tree.root)
    print("Behavior tree loaded...")
    tree.root.print()
    print()

    behavior = input("Event: ('q' to exit): ")

    while behavior is not 'q':
        search = Search()

        # DEPTH FIRST
        response = search.depth_first_recursive(behavior, tree.root)

        # BREADTH FIRST
        #response = search.breadth_first(behavior, tree.root)

        print("Response = {}".format(response))
        behavior = input("Event: ('q' to exit): ")
Beispiel #3
0
def main():
    tree = TreeBuilder()
    tree.build_tree(tree.file, tree.root)
    print("Behavior tree loaded...")
    tree.root.print()
    print()

    behavior = input("Event: ('q' to exit): ")

    while behavior is not 'q':
        search = Search()

        # DEPTH FIRST
        response = search.depth_first_recursive(behavior, tree.root)

        # BREADTH FIRST
        #response = search.breadth_first(behavior, tree.root)

        print("Response = {}".format(response))
        behavior = input("Event: ('q' to exit): ")
Beispiel #4
0
def main():

    output_file = "conjugation_tree.json"
    tree_order = None

    if len(sys.argv) == 1:
        print("Please include required input file")
        exit()
    if len(sys.argv) >= 2:
        try:
            f = open(sys.argv[1], 'r')
            f.close()
        except OSError:
            print("Input file '", sys.argv[1], "' not found")
            exit()
        input_file = sys.argv[1]
        if not input_file.endswith(".csv"):
            print(
                "Input file must be a comma separated file and have the .csv extension"
            )
            exit()
    if len(sys.argv) >= 3:
        output_file = sys.argv[2]
        if not output_file.endswith(".json"):
            print(
                "Selected output file must be a json file (i.e. must have the .json extension)"
            )
            exit()

    if len(sys.argv) == 4:
        tree_order = sys.argv[3]
        if not tree_order.endswith(".csv"):
            print(
                "Selected tree order file must be a csv file (i.e. must have the .csv extension)"
            )
            exit()
        try:
            f = open(tree_order, 'r')
            f.close()
        except OSError:
            print("Tree order file '", tree_order, "' not found")
            exit()

        tree_order_file = open(tree_order, 'r')
        tree_order = tree_order_file.readline().split(',')

    c2d = CSVtoDict(input_file=input_file, order=tree_order)
    dict_list, attr_dict_list = c2d.execute()
    tb = TreeBuilder(dict_list, output_file, tree_order)
    ob = OptionBuilder(attr_dict_list, tree_order)
from tree_builder import TreeBuilder, DecisionNode, TreeClassifer

data = [['slashdot', 'USA', 'yes', 18, 'None'],
        ['google', 'France', 'yes', 23, 'Premium'],
        ['digg', 'USA', 'yes', 24, 'Basic'],
        ['kiwitobes', 'France', 'yes', 23, 'Basic'],
        ['google', 'UK', 'no', 21, 'Premium'],
        ['(direct)', 'New Zealand', 'no', 12, 'None'],
        ['(direct)', 'UK', 'no', 21, 'Basic'],
        ['google', 'USA', 'no', 24, 'Premium'],
        ['slashdot', 'France', 'yes', 19, 'None'],
        ['digg', 'USA', 'no', 18, 'None'], ['google', 'UK', 'no', 18, 'None'],
        ['kiwitobes', 'UK', 'no', 19, 'None'],
        ['digg', 'New Zealand', 'yes', 12, 'Basic'],
        ['slashdot', 'UK', 'no', 21, 'None'],
        ['google', 'UK', 'yes', 18, 'Basic'],
        ['kiwitobes', 'France', 'yes', 19, 'Basic']]

if __name__ == "__main__":
    tree_builder = TreeBuilder()
    tree = tree_builder.recursive_build_tree(data)
    # tree_builder.print_tree(tree)

    tree_classifier = TreeClassifer()
    print(
        tree_classifier.classify(['kiwitobes', 'France', 'yes', 19, 'Basic'],
                                 tree))
Beispiel #6
0
    fileObject.close()


# function to create a file and store the data in the file
def picklemaker(filename, objectname):
    # open the file for writing
    fileObject = open(filename, 'wb')

    # this writes the object a to the
    # file named 'testfile'
    pickle.dump(objectname, fileObject)

    # here we close the fileObject
    fileObject.close()


if __name__ == "__main__":
    try:
        tree_builder = pickleloader("tree_builder_pickled")

    except FileNotFoundError:
        tree_builder = TreeBuilder()

    try:
        tree_builder.build_word_tree(word, context, 3)
    except (KeyboardInterrupt, Exception):
        pass

    filename = "tree_builder_pickled"
    picklemaker(filename, tree_builder)
Beispiel #7
0
class Tree(BaseEstimator, metaclass=ABCMeta):
    """ Base class for the tree estimators.

    Arguments
    ---------
        merge_leaf_func: function,
            The method of merging elements in leave to scalar.

        criterion: string,
            The name of criterion to split data.

        max_depth: integer,
            The maximal depth of tree to build.
    """
    def __init__(self, merge_leaf_func, criterion, max_depth=None):

        self.tree_ = None
        self.feature_importances_ = None
        self.output_shape_ = None

        self._tree_builder = TreeBuilder(merge_leaf_func=merge_leaf_func,
                                         criterion=criterion,
                                         max_depth=max_depth)

    def fit(self, X, y):
        """ Build tree with respect to training data.

        Parameters
        ----------
            X : array, shape (n_samples, n_features)
                Training data

            y : array, shape (n_samples,)
                Target values.
        """
        self.output_shape_ = y.shape
        self.tree_ = self._tree_builder.build(X, y)
        self.feature_importances_ = self._tree_builder.feature_importances_

    # TODO: To refactor method to avoid using recursion.
    def _walk_tree(self, node, data, indexes):
        """ Passes data to the leaves of the tree and returns the node labels. """

        if node.leaf_value is not None:
            return np.zeros(shape=(data.shape[0])) + node.leaf_value, indexes

        data_left, indexes_left, data_right, indexes_right = \
            node.splitter.split(data, indexes)

        labels_left, indexes_left = self._walk_tree(node.left_node, data_left,
                                                    indexes_left)
        labels_right, indexes_right = self._walk_tree(node.right_node,
                                                      data_right,
                                                      indexes_right)

        labels = np.concatenate([labels_left, labels_right])
        indexes = np.concatenate([indexes_left, indexes_right])

        return labels, indexes

    def predict(self, X):
        """Predict labels

        Parameters
        ----------
            X : {array-like, sparse matrix}, shape (n_samples, n_features)
            Samples.

        Returns
        -------
            y : array, shape (n_samples,)
                Returns predicted values.
        """

        labels, indexes = self._walk_tree(self.tree_, X,
                                          np.arange(0, X.shape[0]))
        return labels[np.argsort(indexes)].reshape(-1, *self.output_shape_[1:])

    def fit_predict(self, X, y):
        self.fit(X, y)
        return self.predict(X)
 def build_tree(self):
     tree_builder = TreeBuilder(self)
     self._tree_file_path = tree_builder.build()
     return self._tree_file_path
Beispiel #9
0
def main():
    tree_order = None
    app_order = None
    conjugation_order = None

    if len(sys.argv) == 1:
        print("Please include required input file")
        exit()
    if len(sys.argv) >= 2:
        try:
            f = open(sys.argv[1], 'r')
            f.close()
        except OSError:
            print("Input file '", sys.argv[1], "' not found")
            exit()
        input_file = sys.argv[1]
        if not input_file.endswith(".csv"):
            print(
                "Input file must be a comma separated file and have the .csv extension"
            )
            exit()
    if len(sys.argv) >= 3:
        tree_order = sys.argv[2]
        if not tree_order.endswith(".csv"):
            print(
                "Selected tree order file must be a csv file (i.e. must have the .csv extension)"
            )
            exit()
        try:
            f = open(tree_order, 'r')
            f.close()
        except OSError:
            print("Order file '", tree_order, "' not found")
            exit()

        tree_order_file = open(tree_order, 'r')
        orders = tree_order_file.readlines()
        tree_order = orders[0].strip().split(',')
        tree_order = [x.strip().lower() for x in tree_order]
        if len(orders) >= 2:
            conjugation_order = orders[1].strip().split(',')
            conjugation_order = [x.strip().lower() for x in conjugation_order]

    print("Progress [=     ]", end='\r')
    makeToolTips(tree_order)
    c2d = CSVtoDict(input_file=input_file, order=tree_order)
    dict_list, attr_dict_list = c2d.execute()
    print("Progress [=>    ]", end='\r')
    tb = TreeBuilder(dict_list, "category_tree.json", tree_order)
    print("Progress [==>   ]", end='\r')
    ob = OptionBuilder(attr_dict_list, tree_order)
    print("Progress [===>  ]", end='\r')

    c2d = CSVtoDict(input_file=input_file, order=conjugation_order)
    dict_list, attr_dict_list = c2d.execute()
    print("Progress [====> ]", end='\r')
    tb = TreeBuilder(dict_list, "conjugation.json", conjugation_order)
    print("Progress [=====>]", end='\r')

    with open('JSON/conjugation.json', 'r') as f:
        f_str = f.read()
        d = json.loads(f_str)
        l = [conjugation_order, d]
        f.close()
    with open('JSON/conjugation.json', 'w') as f:
        json.dump(l, f, indent=4)
        f.close()

    print("Progress [======]", end='\r')
    print("Complete. See JSON folder.")

    TESTS.runtests()
# doc 3 is copy-pasted directly from Excel from a structure where element names are in
# columns A, B, C, D and element values are in column F
doc3 = """
World					
	Continent				Europe
		Country			France
			Capital		Paris
		Country			Germany
			Capital		Berlin
"""

# logger.debug(tree_struct)


# load doc 1
tr1 = TreeBuilder(DOC_STRUCT_SYNTAX1)
tree_root = tr1.load(doc1)
logging.info(tree_root.print())

# load doc 2
tr2 = TreeBuilder(DOC_STRUCT_SYNTAX2)
tree_root2 = tr2.load(doc2)
logging.info(tree_root2.print())
logging.info(tree_root2.xmlify())

# load doc 3
tr3 = TreeBuilder(DOC_STRUCT_SYNTAX3)
tree_root3 = tr3.load(doc3)
logging.info(tree_root3.print())
logging.info(tree_root3.xmlify())