def test_score_titanic(self): scorer = BicScore(self.titanic_data2) titanic = BayesianModel([("Sex", "Survived"), ("Pclass", "Survived")]) self.assertAlmostEqual(scorer.score(titanic), -1896.7250012840179) titanic2 = BayesianModel([("Pclass", "Sex")]) titanic2.add_nodes_from(["Sex", "Survived", "Pclass"]) self.assertLess(scorer.score(titanic2), scorer.score(titanic))
def model_change(dag, data): bay_model = [] data = pd.DataFrame(data) print(data) for i in range(len(dag)): for j in range(np.size(data, 1)): if dag[i][j] != 0: bay_model.append((str(i), str(j))) best_model = BayesianModel(bay_model) bic = BicScore(data) return bic.score(best_model)
#nodes=['A','S', 'T','L' ,'B', 'E', 'X', 'D'] nodes = ['Pollution', 'Smoker', 'Cancer', 'Xray', 'Dyspnoea'] nao_dag = [] bic_score = BicScore(data) ind_size = round((len(nodes) * len(nodes) - len(nodes)) / 2) gen_max = 50 gen = 0 melhor_fit = [] #populacao inicial while len(ind) < pop_size: aux = [random.randint(min_valor, max_valor) for i in range(ind_size)] if aux not in nao_dag: G = vetor_Rede(aux, nodes) if G: ind.append(aux) fitness.append(abs(bic_score.score(G))) else: nao_dag.append(aux) while gen < gen_max: print(gen) filhos = [] filhos_fitness = [] while len(filhos) < pop_size: [fitness_sel1, ind_sel1] = torneio(fitness, ind) [fitness_sel2, ind_sel2] = torneio(fitness, ind) r = random.random() if r <= p_cruzamento: [filho1, filho2] = cruzamento_binario(ind_sel1, ind_sel2, ind_size) if filho1 not in nao_dag: G = vetor_Rede(filho1, nodes) if G: