def test_create_tree(self): """Unittest for create tree. :return: """ data, feat_names = trees.create_dataset() decision_tree = { 'no surfacing': { 0: 'no', 1: { 'flippers': { 0: 'no', 1: 'yes' } } } } self.assertEqual(decision_tree, trees.create_tree(data, feat_names)) data = [[1, 2, 'yes'], [1, 3, 'yes'], [2, 3, 'no']] feat_names = ['no surfacing', 'flippers'] decision_tree = {'no surfacing': {1: 'yes', 2: 'no'}} self.assertEqual(decision_tree, trees.create_tree(data, feat_names))
import trees import tree_plotter my_data, _ = trees.create_dataset() print(trees.cal_shannon_ent(my_data)) my_data[0][-1] = 'maybe' print(trees.cal_shannon_ent(my_data)) my_data, _ = trees.create_dataset() trees.split_dataset(my_data, 0, 1) trees.split_dataset(my_data, 0, 0) trees.choose_best_feature_to_split(my_data) my_data, labels = trees.create_dataset() trees.create_tree(my_data, labels) tree_plotter.create_plot()
def test_calc_entropy(self): data, feat_names = trees.create_dataset() entropy = -(0.4 * log(0.4, 2) + 0.6 * log(0.6, 2)) self.assertEqual(entropy, trees.calc_entropy(data))
import trees dataset, labels = trees.create_dataset() print(dataset) entropy = trees.entropy(dataset) print(entropy)