def run_bayes_bp(self, fold=10): nodes_with_in_degree = [] for node in self.mybp.G.nodes(): nodes_with_in_degree.append([node, self.mybp.G.in_degree(node)]) nodes = sorted(nodes_with_in_degree, key=lambda x: x[1], reverse=True) trunk_size = len(nodes) / fold train_ids = [node[0] for node in nodes[0:trunk_size]] test_ids = [node[0] for node in nodes[trunk_size:]] fscore = 0.0 accuracy = 0.0 """apply Belief Propagation""" nodes_belief = dict.fromkeys(self.mybp.G.nodes(), [0.5, 0.5]) for node in train_ids: if self.mybp.nodes_label[node] == "d": nodes_belief[node] = [0.99, 0.01] else: nodes_belief[node] = [0.01, 0.99] edge_messages = self.mybp.initialize_messages(self.mybp.G) new_nodes_belief, edge_messages = bp.beliefprop( self.mybp.G, edge_messages, nodes_belief) accuracy, fscore = self.mybp.evaluation(self.mybp.nodes_label, new_nodes_belief, train_ids) print "\tbayes_based_bp: ", accuracy, fscore return accuracy, fscore
def run_bayes_bp(self, fold = 10): nodes_with_in_degree = [] for node in self.mybp.G.nodes(): nodes_with_in_degree.append([node, self.mybp.G.in_degree(node)]) nodes = sorted(nodes_with_in_degree, key = lambda x : x[1], reverse = True) trunk_size = len(nodes)/fold train_ids = [node[0] for node in nodes[0:trunk_size]] test_ids = [node[0] for node in nodes[trunk_size:]] fscore = 0.0 accuracy = 0.0 """apply Belief Propagation""" nodes_belief = dict.fromkeys(self.mybp.G.nodes(), [0.5, 0.5]) for node in train_ids: if self.mybp.nodes_label[node] == "d": nodes_belief[node] = [0.99, 0.01] else: nodes_belief[node] = [0.01, 0.99] edge_messages = self.mybp.initialize_messages(self.mybp.G) new_nodes_belief, edge_messages = bp.beliefprop(self.mybp.G, edge_messages, nodes_belief) accuracy, fscore = self.mybp.evaluation(self.mybp.nodes_label, new_nodes_belief, train_ids) print "\tbayes_based_bp: ", accuracy, fscore return accuracy, fscore
def run_bayes_bp(self, fold=10, iters=5): nodes = list(self.mybp.G.nodes()) f = open( "../results/results_fscore_accuracy_feb27_bp/bp_result_bayes_adjusted_%dfold.csv" % fold, "w") f.write("bayes_based_bp,bayes_adjusted_bp\n") f.write( "iter,bayes_adjusted_bp_fscore,bayes_adjusted_bp_accuracy,bayes_fscore,bayes_accuracy\n" ) for iter in range(iters): random.shuffle(nodes) # shuffle the list for every iteration trunk_size = len(nodes) / fold mean_fscore_bayes = 0.0 mean_accuracy_bayes = 0.0 mean_fscore_bayes_adjusted_bp = 0.0 mean_accuracy_bayes_adjusted_bp = 0.0 for i in range(fold): print "iter %d " % i train_ids = [] test_ids = [] for j in range(fold): if j == i: if j == fold - 1: train_ids.extend(nodes[j * trunk_size:len(nodes)]) else: train_ids.extend(nodes[j * trunk_size:(j + 1) * trunk_size]) else: if j == fold - 1: test_ids.extend(nodes[j * trunk_size:len(nodes)]) else: test_ids.extend(nodes[j * trunk_size:(j + 1) * trunk_size]) """apply Naive Bayes""" train_x = [] train_Y = [] test_x = [] test_Y = [] for id in train_ids: train_x.append(self.toList(self.vector[id])) if self.mybp.nodes_label[id] == "d": train_Y.append(1) else: train_Y.append(0) for id in test_ids: test_x.append(self.toList(self.vector[id])) if self.mybp.nodes_label[id] == "d": test_Y.append(1) else: test_Y.append(0) clf = GaussianNB() clf.fit(train_x, train_Y) y_predicted = clf.predict(test_x) y_predicted_prob = clf.predict_proba(test_x) print y_predicted_prob f_score_bayes = metrics.f1_score(test_Y, y_predicted) accuracy_bayes = metrics.accuracy_score(test_Y, y_predicted) mean_fscore_bayes += f_score_bayes mean_accuracy_bayes += accuracy_bayes print "\tnaive bayes: ", accuracy_bayes, f_score_bayes """apply bayes_adjusted Belief Propagation""" nodes_belief = dict.fromkeys(self.mybp.G.nodes(), [0.5, 0.5]) for node in train_ids: if self.mybp.nodes_label[node] == "d": nodes_belief[node] = [0.99, 0.01] else: nodes_belief[node] = [0.01, 0.99] edge_messages = self.mybp.initialize_messages(self.mybp.G) new_nodes_belief, edge_messages = bp.beliefprop( self.mybp.G, edge_messages, nodes_belief) predicted_prob = {} for k in range(len(test_ids)): predicted_prob.update({test_ids[k]: y_predicted_prob[k]}) accuracy_bayes_adjusted_bp, fscore_bayes_adjusted_bp = self.evaluate( self.mybp.nodes_label, new_nodes_belief, train_ids, predicted_prob) mean_accuracy_bayes_adjusted_bp += accuracy_bayes_adjusted_bp mean_fscore_bayes_adjusted_bp += fscore_bayes_adjusted_bp print "\tbayes_adjusted_bp: ", accuracy_bayes_adjusted_bp, fscore_bayes_adjusted_bp f.write( "f%d,%f,%f,%f,%f\n" % (i, fscore_bayes_adjusted_bp, accuracy_bayes_adjusted_bp, f_score_bayes, accuracy_bayes)) f.write("i%d,%f,%f,%f,%f\n" % (iter, mean_fscore_bayes_adjusted_bp / float(fold), mean_accuracy_bayes_adjusted_bp / float(fold), mean_fscore_bayes / float(fold), mean_accuracy_bayes / float(fold))) f.close()
def run_bayes_bp(self, fold = 10, iters = 5): nodes = list(self.mybp.G.nodes()) f = open("../results/results_fscore_accuracy_feb27_bp/bp_result_bayes_adjusted_%dfold.csv" %fold,"w") f.write("bayes_based_bp,bayes_adjusted_bp\n") f.write("iter,bayes_adjusted_bp_fscore,bayes_adjusted_bp_accuracy,bayes_fscore,bayes_accuracy\n") for iter in range(iters): random.shuffle(nodes) # shuffle the list for every iteration trunk_size = len(nodes)/fold mean_fscore_bayes = 0.0 mean_accuracy_bayes = 0.0 mean_fscore_bayes_adjusted_bp = 0.0 mean_accuracy_bayes_adjusted_bp = 0.0 for i in range(fold): print "iter %d " %i train_ids = [] test_ids = [] for j in range(fold): if j == i: if j == fold-1: train_ids.extend(nodes[j*trunk_size:len(nodes)]) else: train_ids.extend(nodes[j*trunk_size:(j+1)*trunk_size]) else: if j == fold-1: test_ids.extend(nodes[j*trunk_size:len(nodes)]) else: test_ids.extend(nodes[j*trunk_size:(j+1)*trunk_size]) """apply Naive Bayes""" train_x = [] train_Y = [] test_x = [] test_Y = [] for id in train_ids: train_x.append(self.toList(self.vector[id])) if self.mybp.nodes_label[id] == "d": train_Y.append(1) else: train_Y.append(0) for id in test_ids: test_x.append(self.toList(self.vector[id])) if self.mybp.nodes_label[id] == "d": test_Y.append(1) else: test_Y.append(0) clf = GaussianNB() clf.fit(train_x, train_Y) y_predicted = clf.predict(test_x) y_predicted_prob = clf.predict_proba(test_x) print y_predicted_prob f_score_bayes = metrics.f1_score(test_Y, y_predicted) accuracy_bayes = metrics.accuracy_score(test_Y, y_predicted) mean_fscore_bayes += f_score_bayes mean_accuracy_bayes += accuracy_bayes print "\tnaive bayes: ", accuracy_bayes, f_score_bayes """apply bayes_adjusted Belief Propagation""" nodes_belief = dict.fromkeys(self.mybp.G.nodes(), [0.5, 0.5]) for node in train_ids: if self.mybp.nodes_label[node] == "d": nodes_belief[node] = [0.99, 0.01] else: nodes_belief[node] = [0.01, 0.99] edge_messages = self.mybp.initialize_messages(self.mybp.G) new_nodes_belief, edge_messages = bp.beliefprop(self.mybp.G, edge_messages, nodes_belief) predicted_prob = {} for k in range(len(test_ids)): predicted_prob.update({test_ids[k] : y_predicted_prob[k]}) accuracy_bayes_adjusted_bp, fscore_bayes_adjusted_bp = self.evaluate(self.mybp.nodes_label, new_nodes_belief, train_ids, predicted_prob) mean_accuracy_bayes_adjusted_bp += accuracy_bayes_adjusted_bp mean_fscore_bayes_adjusted_bp += fscore_bayes_adjusted_bp print "\tbayes_adjusted_bp: ", accuracy_bayes_adjusted_bp, fscore_bayes_adjusted_bp f.write("f%d,%f,%f,%f,%f\n" %(i, fscore_bayes_adjusted_bp, accuracy_bayes_adjusted_bp, f_score_bayes, accuracy_bayes)) f.write("i%d,%f,%f,%f,%f\n" %(iter, mean_fscore_bayes_adjusted_bp/float(fold), mean_accuracy_bayes_adjusted_bp/float(fold), mean_fscore_bayes/float(fold), mean_accuracy_bayes/float(fold))) f.close()