def test_get_max_class(self): dict1 = {'a':10, 'b':11, 'c': 6} dict2 = {'a':14, 'b':11, 'c': 6} res1 = BNClassifier.get_max_class(dict1) res2 = BNClassifier.get_max_class(dict2) self.assertEquals('b', res1) self.assertEquals('a', res2)
def test_counts(self): cols = [1,2] row = [0,1,0] data = [[0,1,1], [0,1,0], [0,0,0], [0,1,1]] classes = ['a', 'b', 'a', 'b'] poss = ['a', 'b'] counts = BNClassifier.counts(cols, row, data, classes, poss) self.assertEquals(1, counts['a']) self.assertEquals(2, counts['b'])
def test_get_list_of_cols(self): n1 = BNNode([], []) n1.col_index = 0 n2 = BNNode([], []) n2.col_index = 1 n3 = BNNode([], []) n3.col_index = 2 n1.parents.append(n2) n1.parents.append(n3) col_list = BNClassifier.get_list_of_cols(n1) self.assertEquals(1, col_list[0]) self.assertEquals(2, col_list[1])
def test_get_count(self): n1 = BNNode([], []) n1.col_index = 0 n2 = BNNode([], []) n2.col_index = 1 n3 = BNNode([], []) n3.col_index = 2 n1.parents.append(n2) n1.parents.append(n3) cols = [1,2] row = [0,1,0] data = [[0,1,0], [0,1,0], [0,0,0], [0,1,0]] classes = ['a', 'b', 'a', 'b'] poss = ['a', 'b'] counts = BNClassifier.get_count(row, n1, data, classes, poss)
BN.setup_node(node, nodes) # initial confusion matrix confusion = {'0':{'0':0, '1':0}, '1':{'0':0, '1':0}} # do k-fold validation total_count = 0 kfold = KFold(100, new_data, new_classes) while kfold.has_next(): dat, cls = kfold.get_next() correct_count = 0 for i in range(0, len(dat)): row = dat[i] guess = BNClassifier.classify(row, nodes, dat, cls, ['0', '1']) if guess == cls[i]: correct_count += 1 confusion[cls[i]][guess] += 1 total_count += correct_count for node in nodes: pind = "" for pnode in node.parents: pind += " "+str(pnode.col_index) print "Node "+str(node.col_index)+" has parents: "+pind