def test_make_DAG(): edges = [('Cloudy', 'Sprinkler')] DAG = bn.make_DAG(edges) # TEST 1 assert 'pgmpy.models.BayesianModel.BayesianModel' in str(type( DAG['model'])) # TEST 2 cpt_cloudy = TabularCPD(variable='Cloudy', variable_card=2, values=[[0.3], [0.7]]) cpt_sprinkler = TabularCPD(variable='Sprinkler', variable_card=2, values=[[0.4, 0.9], [0.6, 0.1]], evidence=['Cloudy'], evidence_card=[2]) assert bn.make_DAG(DAG, CPD=[cpt_cloudy, cpt_sprinkler], checkmodel=True)
'pop scale', 'label' ]] print(f"all data size:\t{df_final.size}") print(f"all data num of successes:\t{df_final['label'].sum()}") df_train, df_test = train_test_split(df_final, test_size=0.2, random_state=0) print(f"train size: \t{df_train.size}") print(f"train num of succes\t{df_train['label'].sum()}") edges = [('top director', 'budget scale'), ('top director', 'week_num'), ('top director', 'main genre'), ('week_num', 'pop scale'), ('budget scale', 'label'), ('main genre', 'top actor'), ('top actor', 'pop scale'), ('label', 'pop scale')] # Make the actual Bayesian DAG DAG = bnlearn.make_DAG(edges) # DAG is stored in adjacency matrix print(DAG['adjmat']) # No CPDs are in the DAG. Lets see what happens if we print it. bnlearn.print_CPD(DAG) # # Plot DAG. Note that it can be differently orientated if you re-make the plot. # print("plotting") # bnlearn.plot(DAG) # print("stop plot") # Parameter learning on the user-defined DAG and input data DAG = bnlearn.parameter_learning.fit(DAG, df_train)
# %% Inference using custom DAG import bnlearn as bn # Load asia DAG df = bn.import_example('asia') # from tabulate import tabulate # print(tabulate(df.head(), tablefmt="grid", headers="keys")) print(df) edges = [('smoke', 'lung'), ('smoke', 'bronc'), ('lung', 'xray'), ('bronc', 'xray')] # edges = [('smoke', 'xray'), # ('bronc', 'lung')] # Make the actual Bayesian DAG DAG = bn.make_DAG(edges) # Plot the DAG bn.plot(DAG) # Print the CPDs bn.print_CPD(DAG) # Learn its parameters from data and perform the inference. DAG = bn.parameter_learning.fit(DAG, df) # Print the CPDs bn.print_CPD(DAG) # Make inference q1 = bn.inference.fit(DAG, variables=['lung'], evidence={'smoke': 1}) q2 = bn.inference.fit(DAG, variables=['bronc'], evidence={'smoke': 1}) q3 = bn.inference.fit(DAG, variables=['lung'],