'Sex': 1, 'Pclass': 1 }) q1 = bn.inference.fit(model, variables=['Survived'], evidence={'Sex': 0}) bn.print_CPD(model) # %% DAG = bn.import_DAG('sprinkler', CPD=False) DAG = bn.import_DAG('asia') bn.plot(DAG) bn.print_CPD(DAG) df = bn.sampling(DAG, n=1000) vector = bn.adjmat2vec(DAG['adjmat']) adjmat = bn.vec2adjmat(vector['source'], vector['target']) # %% from pgmpy.factors.discrete import TabularCPD edges = [('A', 'E'), ('S', 'E'), ('E', 'O'), ('E', 'R'), ('O', 'T'), ('R', 'T')] DAG = bn.make_DAG(edges) bn.plot(DAG) cpd_A = TabularCPD(variable='A', variable_card=3, values=[[0.3], [0.5], [0.2]]) print(cpd_A) cpd_S = TabularCPD(variable='S', variable_card=2, values=[[0.6], [0.4]]) print(cpd_S) cpd_E = TabularCPD(variable='E', variable_card=2,
def test_vec2adjmat(): DAG = bn.import_DAG('Sprinkler', verbose=0) out = bn.adjmat2vec(DAG['adjmat']) # TEST: conversion assert bn.vec2adjmat(out['source'], out['target']).shape == DAG['adjmat'].shape
model_hc_bic = bn.structure_learning.fit(df, methodtype='hc', scoretype='bic', verbose=0) # %% Chow-Liu algorithm DAG = bn.import_DAG('sprinkler', verbose=0) df = bn.sampling(DAG, n=1000, verbose=0) # Structure learning model_hc_bic = bn.structure_learning.fit(df, methodtype='cl', root_node='Cloudy', verbose=0) G = bn.plot(model) # %% Load example dataframe from sprinkler import bnlearn as bn DAG = bn.import_DAG('alarm', verbose=0) to_vector = bn.adjmat2vec(DAG['adjmat']) to_adjmat = bn.vec2adjmat(to_vector['source'], to_vector['target']) # %% Load example dataframe from sprinkler df = bn.import_example('sprinkler', verbose=0) # Structure learning model = bn.structure_learning.fit(df, verbose=0) # Plot G = bn.plot(model, verbose=0) model_hc_bic = bn.structure_learning.fit(df, methodtype='hc', scoretype='bic', verbose=0) # %% Try all methods vs score types model_hc_bic = bn.structure_learning.fit(df, methodtype='hc', scoretype='bic') model_hc_k2 = bn.structure_learning.fit(df, methodtype='hc', scoretype='k2') model_hc_bdeu = bn.structure_learning.fit(df, methodtype='hc', scoretype='bdeu') model_ex_bic = bn.structure_learning.fit(df, methodtype='ex', scoretype='bic')