class _TreeBag: def __init__(self, features: np.ndarray, data: np.ndarray, labels: np.ndarray, max_depth: int, min_leaf_points: int): self.features = features self.d_tree = Tree(max_depth=max_depth, min_node_points=min_leaf_points) self.d_tree.train(data, labels) def predict(self, x): x_bag = x[self.features] return self.d_tree.predict(x_bag)
# declare shared hyper parameters max_depth = 100 min_samples = 10 tree_clf = DecisionTreeClassifier(max_depth=max_depth, min_samples_leaf=min_samples) tree_clf.fit(X,y) sk_tree_graph = export_graphviz(tree_clf, out_file=None, class_names=iris.target_names, rounded=True, filled=True) # build tree graph for sklearn model folder = os.path.dirname(__file__) sk_tree_source = Source(sk_tree_graph, filename='skl_treebag.gv', directory=folder+'\images', format='png') sk_tree_source.render() # my tree class t = Tree(max_depth=max_depth, min_node_points=min_samples) Xme = X Yme = np.atleast_2d(y).T t.train(Xme, Yme) build_graph(t, '.\images') print(t.predict(np.array([1,7])))
import numpy as np from tree.node import Node from tree.tree import Tree x = np.random.random((100, 10)) y = np.atleast_2d(np.random.randint(1, size=100)).T N = Tree() N.train(x, y) x_test = np.random.random((1, 10)) result = N.predict(x=x_test) print(result)