Learning BN as probabilistic classifier¶

Learning a Bayesian network can be used to obtain a classifier for one of the nodes of the model. For more about classifier, see pyAgrum.skbn.

In [1]:

import sys
import os

import numpy as np

import pyAgrum as gum
import pyAgrum.lib.notebook as gnb

%matplotlib inline
from pyAgrum.lib.bn2roc import showROC
from pyAgrum.lib.bn2roc import showPR
from pyAgrum.lib.bn2roc import showROC_PR

SIZE_LEARN=10000
SIZE_VALID=2000

In [2]:

bn=gum.loadBN("res/alarm.dsl")
bn

Out[2]:

In [3]:

gum.generateSample(bn,SIZE_LEARN,"out/learn.csv",show_progress=True,with_labels=True)
gum.generateSample(bn,SIZE_VALID,"out/train.csv",show_progress=True,with_labels=True)

out/learn.csv: 100%|███████████████████████████████████████|

Log2-Likelihood : -151899.5026574697

out/train.csv: 100%|███████████████████████████████████████|

Log2-Likelihood : -30262.038006648694



Out[3]:

-30262.038006648694


Learning a BN from learn.csv¶

In [4]:

# Learning a BN from the database
learner=gum.BNLearner("out/train.csv")

bn2=learner.useMIIC().learnBN()
currentTime=learner.currentTime()

In [5]:

gnb.flow.add(gnb.getBN(bn2,size="9"),f"Learned with {SIZE_LEARN} lines in {currentTime:.3f}s")
gnb.flow.display()


Learned with 10000 lines in 0.159s
In [6]:

import pyAgrum.lib.bn_vs_bn as bnvsbn
gnb.flow.display()


Diff with MIIC
In [7]:

bn3=learner.useGreedyHillClimbing().useNMLCorrection().useScoreBDeu().learnBN()
gnb.flow.display()


Diff with GHC/NMD/BDEU
In [8]:

bn4=learner.useGreedyHillClimbing().useNMLCorrection().useScoreBDeu().setInitialDAG(bn2.dag()).learnBN()
gnb.flow.add(gnb.getBNDiff(bn,bn4,size="8!"),"Diff with GHC/NMD/BDEU with intial DAG from MIIC")
gnb.flow.display()


Diff with GHC/NMD/BDEU with intial DAG from MIIC
In [9]:

print(bn2.names())

{'HRBP', 'STROKEVOLUME', 'HRSAT', 'HREKG', 'LVEDVOLUME', 'EXPCO2', 'CO', 'ERRLOWOUTPUT', 'HR', 'PULMEMBOLUS', 'VENTTUBE', 'LVFAILURE', 'FIO2', 'ANAPHYLAXIS', 'KINKEDTUBE', 'PRESS', 'MINVOL', 'HYPOVOLEMIA', 'VENTMACH', 'PCWP', 'SAO2', 'DISCONNECT', 'PAP', 'VENTLUNG', 'SHUNT', 'HISTORY', 'PVSAT', 'ERRCAUTER', 'VENTALV', 'CATECHOL', 'INTUBATION', 'INSUFFANESTH', 'ARTCO2', 'MINVOLSET', 'CVP', 'TPR', 'BP'}

In [10]:

gnb.showInference(bn2,evs={},size="14")


Two classifiers from the learned BN¶

In [11]:

print(bn2.variableFromName("HRSAT"))
print(bn2.variableFromName("INTUBATION"))

HRSAT:Labelized({HIGH|LOW|NORMAL})
INTUBATION:Labelized({ESOPHAGEAL|NORMAL|ONESIDED})

In [12]:

showROC(bn2,"out/train.csv",'HRSAT','LOW',show_progress=False)
showROC(bn2,"out/train.csv",'HRSAT','NORMAL',show_progress=False)
showROC(bn2,"out/train.csv",'HRSAT','HIGH',show_progress=False);

In [13]:

showROC(bn2,"out/train.csv",'INTUBATION',"ESOPHAGEAL",show_progress=False);

In [14]:

showPR(bn2,"out/train.csv",'HRSAT','LOW',show_progress=False);

In [15]:

showPR(bn2,"out/train.csv",'INTUBATION',"ESOPHAGEAL",show_progress=False);

In [16]:

showROC_PR(bn2,"out/train.csv",'HRSAT','LOW',show_progress=False);

In [17]:

showROC_PR(bn2,"out/train.csv",'INTUBATION',"ESOPHAGEAL",show_progress=False);

In [ ]: