def train(self, iterations=10, maxmiss=10): """ Train using Recommended Greedy Algorithm """ scores = { 'Iteration': [], #'Network':[], 'Score': [] } score_check = maxmiss niter = iterations nodes = [i for i in self.data.columns] best = score_pom(export_pom(self.net, by='label'), self.data) #print("START LOOP") while score_check > 0 and niter > 0: n = dc(self.net) upd = set() ops = [n.add_edge, n.del_edge, n.rev_edge] for f in ops: edge = np.random.choice(nodes, size=2, replace=False) f(edge[0], edge[1]) upd.add(edge[0]) upd.add(edge[1]) if n.acyclic(): n.calc_cpt(self.data, alpha=self.alpha, change=upd) score = score_pom(export_pom(n, by='label'), self.data) scores['Iteration'].append(iterations - niter) #scores['Network'].append(n) scores['Score'].append(score) #print(best, score, niter, score_check) if score > best: self.net = n best = score niter = niter - 1 score_check = maxmiss continue else: score_check = score_check - 1 niter = niter - 1 continue else: niter = niter - 1 continue self.scores = scores
def train(self, iterations=10, maxmiss=10): """ Train using Recommended Greedy Algorithm """ scores = {'Iteration': [], 'Network': [], 'Score': []} score_check = maxmiss niter = iterations nodes = [i for i in self.data.columns] best = score_pom(export_pom(self.net, by='label'), self.data) #print("START LOOP") while score_check > 0 and niter > 0: n = net(data=self.data) n.import_dag(self.net.export_dag()) ops = [n.add_edge, n.del_edge, n.rev_edge] for f in ops: # Choose the first node in a uniform, random way v1 = np.random.choice(nodes) # Choose the second with probabilities weighted by mi v2 = np.random.choice(self.mi_weights[v1].index, p=self.mi_weights[v1]) f(v1, v2) if n.acyclic(): n.calc_cpt(self.data, alpha=self.alpha) score = score_pom(export_pom(n, by='label'), self.data) scores['Iteration'].append(iterations - niter) scores['Network'].append(n) scores['Score'].append(score) #print(best, score, niter, score_check) if score > best: self.net = n best = score niter = niter - 1 score_check = maxmiss continue else: score_check = score_check - 1 niter = niter - 1 continue else: niter = niter - 1 continue self.scores = scores