def main(): """ The main function """ graph = watersprinkler.main() nbr_samples = 1000 # sample the network N times cases = graph.sample(nbr_samples) # create a new bayesian network with all parameters set to 1 graph_copy = deepcopy(graph) graph_copy.init_distributions() # Learn the parameters from the set of cases engine = learning.MLLearningEngine(graph_copy) # cases = engine.read_file('file.xls') #To use the data of file.xls start_time = time() engine.learn_ml_params(cases) print 'Learned from %d cases in %1.3f secs' % \ (nbr_samples, (time() - start_time)) # print the learned parameters for vertex in graph_copy.all_v: print vertex.name, vertex.distribution.cpt,'\n' # print the parameters for vertex in graph.all_v: print vertex.distribution,'\n'
def main(): """ The main function """ graph = watersprinkler.main() # create an inference Engine # choose the one you like by commenting/uncommenting the appropriate line engine = JoinTree(graph) #ie = MCMCEngine(G) # perform inference with no evidence results = engine.marginalise_all() print '==============================================================' print 'Without evidence:\n' for name, res in results.items(): print name, res, '\n' print '==============================================================' print 'With evidence:\n' # add some evidence engine.set_obs({'s':1}) # s=1 # perform inference with evidence results = engine.marginalise_all() # notice that the JoinTree engine does not perform a full message pass # but only distributes the new evidence... for res in results.values(): print res, '\n'
def main(): """ This is the main function """ graph = watersprinkler.main() nbr_samples = 2000 # sample the network N times # cases = [{'c':0,'s':1,'r':0,'w':1},{...},...] cases = graph.sample(nbr_samples) # delete some observations for i in range(500): case = cases[3*i] rand = random.sample(['c', 's', 'r', 'w'], 1)[0] case[rand] = '?' for i in range(50): case = cases[3*i] rand = random.sample(['c', 's', 'r', 'w'], 1)[0] case[rand] = '?' # copy the BN graph_copy = deepcopy(graph) # set all parameters to 1s graph_copy.init_distributions() # Learn the parameters from the set of cases engine = learning.EMLearningEngine(graph_copy) # cases = engine.read_file('file.xls') #To use the data of file.xls start_time = time() engine.em_learning(cases, 10) print 'Learned from %d cases in %1.3f secs' % \ (nbr_samples, (time() - start_time)) # print the learned parameters print "Learned paramters" for vertex in graph_copy.all_v: print vertex.name, vertex.distribution.cpt, '\n' ### print the parameters print "Orignal Parameters" for vertex in graph.all_v: print vertex.name, vertex.distribution.cpt, '\n'
def main(): """ This is the main function """ graph = watersprinkler.main() nbr_samples = 2000 # sample the network several times cases = graph.sample(nbr_samples) # delete some observations for i in range(500): case = cases[3*i] rand = random.sample(['c', 's', 'r', 'w'], 1)[0] case[rand] = '?' for i in range(50): case = cases[3*i] rand = random.sample(['c', 's', 'r', 'w'], 1)[0] case[rand] = '?' # Create a new BNet with no edgraphes graph_copy = bayesnet.BNet('Water Sprinkler Bayesian Network2') for name in "c s r w".split(): graph_copy.add_v(bayesnet.BVertex(name, True, 2)) graph_copy.init_distributions() # Learn the structure struct_engine = learning.SEMLearningEngine(graph_copy) struct_engine.sem_learning(cases) print 'learned structure: ', struct_engine.network print 'total bic score: ', \ struct_engine.global_bic_score(nbr_samples, cases, 0) # Learn the structure struct_engine = learning.SEMLearningEngine(graph_copy) struct_engine.sem_learning_approx(cases) print 'learned structure: ', struct_engine.network print 'total bic score: ', \ struct_engine.global_bic_score(nbr_samples, cases, 1)