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.

Creative Commons License

aGrUM

interactive online version

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]:
G PCWP PCWP ANAPHYLAXIS ANAPHYLAXIS TPR TPR ANAPHYLAXIS->TPR LVFAILURE LVFAILURE LVEDVOLUME LVEDVOLUME LVFAILURE->LVEDVOLUME HISTORY HISTORY LVFAILURE->HISTORY STROKEVOLUME STROKEVOLUME LVFAILURE->STROKEVOLUME LVEDVOLUME->PCWP CVP CVP LVEDVOLUME->CVP VENTALV VENTALV ARTCO2 ARTCO2 VENTALV->ARTCO2 PVSAT PVSAT VENTALV->PVSAT CATECHOL CATECHOL TPR->CATECHOL BP BP TPR->BP HRBP HRBP VENTMACH VENTMACH VENTTUBE VENTTUBE VENTMACH->VENTTUBE HRSAT HRSAT SAO2 SAO2 SAO2->CATECHOL FIO2 FIO2 FIO2->PVSAT SHUNT SHUNT SHUNT->SAO2 VENTLUNG VENTLUNG VENTLUNG->VENTALV MINVOL MINVOL VENTLUNG->MINVOL EXPCO2 EXPCO2 VENTLUNG->EXPCO2 PRESS PRESS HR HR CATECHOL->HR HREKG HREKG CO CO STROKEVOLUME->CO VENTTUBE->VENTLUNG VENTTUBE->PRESS ARTCO2->CATECHOL ARTCO2->EXPCO2 MINVOLSET MINVOLSET MINVOLSET->VENTMACH KINKEDTUBE KINKEDTUBE KINKEDTUBE->VENTLUNG KINKEDTUBE->PRESS ERRCAUTER ERRCAUTER ERRCAUTER->HRSAT ERRCAUTER->HREKG INSUFFANESTH INSUFFANESTH INSUFFANESTH->CATECHOL PVSAT->SAO2 CO->BP PULMEMBOLUS PULMEMBOLUS PULMEMBOLUS->SHUNT PAP PAP PULMEMBOLUS->PAP HYPOVOLEMIA HYPOVOLEMIA HYPOVOLEMIA->LVEDVOLUME HYPOVOLEMIA->STROKEVOLUME INTUBATION INTUBATION INTUBATION->VENTALV INTUBATION->SHUNT INTUBATION->VENTLUNG INTUBATION->PRESS INTUBATION->MINVOL DISCONNECT DISCONNECT DISCONNECT->VENTTUBE HR->HRBP HR->HRSAT HR->HREKG HR->CO ERRLOWOUTPUT ERRLOWOUTPUT ERRLOWOUTPUT->HRBP
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 : -151597.1529075702
out/train.csv: 100%|███████████████████████████████████████|
Log2-Likelihood : -29981.28271719143

Out[3]:
-29981.28271719143

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()

G PCWP PCWP LVEDVOLUME LVEDVOLUME LVEDVOLUME->PCWP CVP CVP LVEDVOLUME->CVP ANAPHYLAXIS ANAPHYLAXIS TPR TPR ANAPHYLAXIS->TPR LVFAILURE LVFAILURE LVFAILURE->LVEDVOLUME STROKEVOLUME STROKEVOLUME LVFAILURE->STROKEVOLUME VENTALV VENTALV MINVOL MINVOL VENTALV->MINVOL ARTCO2 ARTCO2 VENTALV->ARTCO2 PVSAT PVSAT VENTALV->PVSAT INTUBATION INTUBATION VENTALV->INTUBATION HRBP HRBP BP BP TPR->BP VENTMACH VENTMACH VENTTUBE VENTTUBE VENTMACH->VENTTUBE HRSAT HRSAT HREKG HREKG HRSAT->HREKG SAO2 SAO2 CATECHOL CATECHOL SAO2->CATECHOL HISTORY HISTORY HISTORY->LVFAILURE FIO2 FIO2 VENTLUNG VENTLUNG VENTLUNG->VENTALV EXPCO2 EXPCO2 VENTLUNG->EXPCO2 PRESS PRESS HR HR CATECHOL->HR SHUNT SHUNT SHUNT->SAO2 CO CO STROKEVOLUME->CO VENTTUBE->VENTLUNG VENTTUBE->PRESS ARTCO2->EXPCO2 MINVOLSET MINVOLSET MINVOLSET->VENTMACH KINKEDTUBE KINKEDTUBE KINKEDTUBE->VENTLUNG ERRCAUTER ERRCAUTER ERRCAUTER->HRSAT ERRCAUTER->HREKG INSUFFANESTH INSUFFANESTH PVSAT->SAO2 PVSAT->FIO2 CO->BP PULMEMBOLUS PULMEMBOLUS PULMEMBOLUS->SHUNT HYPOVOLEMIA HYPOVOLEMIA HYPOVOLEMIA->LVEDVOLUME HYPOVOLEMIA->STROKEVOLUME INTUBATION->PRESS INTUBATION->SHUNT PAP PAP PAP->PULMEMBOLUS DISCONNECT DISCONNECT DISCONNECT->HISTORY DISCONNECT->VENTTUBE HR->HRBP HR->HRSAT HR->HREKG HR->CO ERRLOWOUTPUT ERRLOWOUTPUT ERRLOWOUTPUT->HRBP
Learned with 10000 lines in 0.239s
In [6]:
import pyAgrum.lib.bn_vs_bn as bnvsbn
gnb.flow.add(gnb.getBNDiff(bn,bn2,size="8!"),"Diff with MIIC")
gnb.flow.add(bnvsbn.graphDiffLegend())
gnb.flow.display()
G KINKEDTUBE KINKEDTUBE VENTLUNG VENTLUNG KINKEDTUBE->VENTLUNG PRESS PRESS KINKEDTUBE->PRESS HYPOVOLEMIA HYPOVOLEMIA STROKEVOLUME STROKEVOLUME HYPOVOLEMIA->STROKEVOLUME LVEDVOLUME LVEDVOLUME HYPOVOLEMIA->LVEDVOLUME INTUBATION INTUBATION SHUNT SHUNT INTUBATION->SHUNT INTUBATION->VENTLUNG MINVOL MINVOL INTUBATION->MINVOL INTUBATION->PRESS VENTALV VENTALV MINVOLSET MINVOLSET VENTMACH VENTMACH MINVOLSET->VENTMACH PULMEMBOLUS PULMEMBOLUS PAP PAP PULMEMBOLUS->SHUNT INSUFFANESTH INSUFFANESTH CATECHOL CATECHOL INSUFFANESTH->CATECHOL ERRLOWOUTPUT ERRLOWOUTPUT HRBP HRBP ERRLOWOUTPUT->HRBP ERRCAUTER ERRCAUTER HRSAT HRSAT ERRCAUTER->HRSAT HREKG HREKG ERRCAUTER->HREKG FIO2 FIO2 PVSAT PVSAT LVFAILURE LVFAILURE LVFAILURE->STROKEVOLUME LVFAILURE->LVEDVOLUME HISTORY HISTORY DISCONNECT DISCONNECT DISCONNECT->HISTORY VENTTUBE VENTTUBE DISCONNECT->VENTTUBE ANAPHYLAXIS ANAPHYLAXIS TPR TPR ANAPHYLAXIS->TPR PAP->PULMEMBOLUS CO CO STROKEVOLUME->CO TPR->CATECHOL BP BP TPR->BP PCWP PCWP LVEDVOLUME->PCWP CVP CVP LVEDVOLUME->CVP VENTMACH->VENTTUBE SAO2 SAO2 SHUNT->SAO2 HISTORY->LVFAILURE VENTTUBE->VENTLUNG VENTTUBE->PRESS VENTLUNG->MINVOL VENTLUNG->VENTALV EXPCO2 EXPCO2 VENTLUNG->EXPCO2 VENTALV->INTUBATION VENTALV->MINVOL ARTCO2 ARTCO2 VENTALV->ARTCO2 VENTALV->PVSAT ARTCO2->EXPCO2 ARTCO2->CATECHOL PVSAT->FIO2 PVSAT->SAO2 SAO2->CATECHOL HR HR CATECHOL->HR HR->HRBP HR->HRSAT HR->CO HR->HREKG HRSAT->HREKG CO->BP
Diff with MIIC
G a->b overflow c->d Missing e->f reversed g->h Correct
In [7]:
bn3=learner.useGreedyHillClimbing().useNMLCorrection().useScoreBDeu().learnBN()
gnb.flow.add(gnb.getBNDiff(bn,bn3,size="8!"),"Diff with GHC/NMD/BDEU")
gnb.flow.add(bnvsbn.graphDiffLegend())
gnb.flow.display()
G KINKEDTUBE KINKEDTUBE ERRLOWOUTPUT ERRLOWOUTPUT KINKEDTUBE->ERRLOWOUTPUT VENTLUNG VENTLUNG PRESS PRESS KINKEDTUBE->PRESS HYPOVOLEMIA HYPOVOLEMIA STROKEVOLUME STROKEVOLUME HYPOVOLEMIA->STROKEVOLUME LVEDVOLUME LVEDVOLUME HYPOVOLEMIA->LVEDVOLUME PCWP PCWP HYPOVOLEMIA->PCWP INTUBATION INTUBATION SHUNT SHUNT INTUBATION->SHUNT MINVOL MINVOL INTUBATION->MINVOL INTUBATION->PRESS VENTALV VENTALV MINVOLSET MINVOLSET VENTMACH VENTMACH PULMEMBOLUS PULMEMBOLUS PAP PAP PULMEMBOLUS->PAP PULMEMBOLUS->SHUNT INSUFFANESTH INSUFFANESTH CATECHOL CATECHOL INSUFFANESTH->CATECHOL HRBP HRBP ERRLOWOUTPUT->HRBP ERRCAUTER ERRCAUTER HR HR ERRCAUTER->HR HRSAT HRSAT HREKG HREKG FIO2 FIO2 PVSAT PVSAT FIO2->PVSAT LVFAILURE LVFAILURE HISTORY HISTORY LVFAILURE->HISTORY DISCONNECT DISCONNECT VENTTUBE VENTTUBE DISCONNECT->VENTTUBE ANAPHYLAXIS ANAPHYLAXIS ANAPHYLAXIS->ERRLOWOUTPUT TPR TPR STROKEVOLUME->LVFAILURE STROKEVOLUME->PCWP CO CO STROKEVOLUME->CO TPR->ANAPHYLAXIS BP BP TPR->BP LVEDVOLUME->LVFAILURE CVP CVP LVEDVOLUME->CVP VENTMACH->MINVOLSET VENTMACH->VENTTUBE PCWP->LVEDVOLUME SHUNT->KINKEDTUBE SAO2 SAO2 SHUNT->SAO2 HISTORY->DISCONNECT VENTTUBE->KINKEDTUBE VENTTUBE->INTUBATION VENTTUBE->VENTLUNG VENTTUBE->PRESS EXPCO2 EXPCO2 VENTTUBE->EXPCO2 VENTLUNG->KINKEDTUBE VENTLUNG->INTUBATION VENTLUNG->MINVOL VENTLUNG->VENTALV ARTCO2 ARTCO2 VENTLUNG->ARTCO2 VENTALV->INTUBATION VENTALV->PVSAT ARTCO2->VENTALV ARTCO2->CATECHOL PVSAT->SAO2 SAO2->CATECHOL SAO2->HREKG EXPCO2->VENTLUNG EXPCO2->ARTCO2 CATECHOL->TPR HR->CATECHOL HR->HRBP HR->CO HRSAT->ERRCAUTER HRSAT->HR CO->BP HREKG->ERRCAUTER HREKG->HR HREKG->HRSAT
Diff with GHC/NMD/BDEU
G a->b overflow c->d Missing e->f reversed g->h Correct
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.add(bnvsbn.graphDiffLegend())
gnb.flow.display()
G KINKEDTUBE KINKEDTUBE ERRCAUTER ERRCAUTER KINKEDTUBE->ERRCAUTER VENTLUNG VENTLUNG KINKEDTUBE->VENTLUNG PRESS PRESS KINKEDTUBE->PRESS HYPOVOLEMIA HYPOVOLEMIA STROKEVOLUME STROKEVOLUME HYPOVOLEMIA->STROKEVOLUME LVEDVOLUME LVEDVOLUME HYPOVOLEMIA->LVEDVOLUME INTUBATION INTUBATION SHUNT SHUNT INTUBATION->SHUNT INTUBATION->VENTLUNG MINVOL MINVOL INTUBATION->MINVOL INTUBATION->PRESS VENTALV VENTALV INTUBATION->VENTALV MINVOLSET MINVOLSET VENTMACH VENTMACH PULMEMBOLUS PULMEMBOLUS PAP PAP PULMEMBOLUS->PAP PULMEMBOLUS->SHUNT INSUFFANESTH INSUFFANESTH CATECHOL CATECHOL INSUFFANESTH->CATECHOL ERRLOWOUTPUT ERRLOWOUTPUT ERRLOWOUTPUT->KINKEDTUBE ANAPHYLAXIS ANAPHYLAXIS ERRLOWOUTPUT->ANAPHYLAXIS HRBP HRBP ERRLOWOUTPUT->HRBP HRSAT HRSAT ERRCAUTER->HRSAT HREKG HREKG ERRCAUTER->HREKG FIO2 FIO2 PVSAT PVSAT FIO2->PVSAT LVFAILURE LVFAILURE LVFAILURE->STROKEVOLUME LVFAILURE->LVEDVOLUME HISTORY HISTORY DISCONNECT DISCONNECT DISCONNECT->HISTORY VENTTUBE VENTTUBE DISCONNECT->VENTTUBE TPR TPR ANAPHYLAXIS->TPR CO CO STROKEVOLUME->CO TPR->CATECHOL BP BP TPR->BP PCWP PCWP LVEDVOLUME->PCWP CVP CVP LVEDVOLUME->CVP VENTMACH->MINVOLSET VENTMACH->VENTTUBE SAO2 SAO2 SHUNT->SAO2 HISTORY->LVFAILURE VENTTUBE->VENTLUNG VENTTUBE->PRESS VENTLUNG->MINVOL VENTLUNG->VENTALV EXPCO2 EXPCO2 VENTLUNG->EXPCO2 ARTCO2 ARTCO2 VENTALV->ARTCO2 VENTALV->PVSAT ARTCO2->EXPCO2 ARTCO2->CATECHOL PVSAT->SAO2 SAO2->CATECHOL HR HR CATECHOL->HR HR->HRBP HR->HRSAT HR->CO HR->HREKG CO->BP
Diff with GHC/NMD/BDEU with intial DAG from MIIC
G a->b overflow c->d Missing e->f reversed g->h Correct
In [9]:
print(bn2.names())
{'PCWP', 'LVEDVOLUME', 'ANAPHYLAXIS', 'LVFAILURE', 'VENTALV', 'HRBP', 'TPR', 'VENTMACH', 'CVP', 'HRSAT', 'SAO2', 'HISTORY', 'FIO2', 'VENTLUNG', 'PRESS', 'BP', 'CATECHOL', 'SHUNT', 'HREKG', 'STROKEVOLUME', 'VENTTUBE', 'MINVOL', 'ARTCO2', 'MINVOLSET', 'KINKEDTUBE', 'ERRCAUTER', 'INSUFFANESTH', 'PVSAT', 'CO', 'PULMEMBOLUS', 'HYPOVOLEMIA', 'EXPCO2', 'INTUBATION', 'PAP', 'DISCONNECT', 'HR', 'ERRLOWOUTPUT'}
In [10]:
gnb.showInference(bn2,evs={},size="10")
../_images/notebooks_32-Learning_learningClassifier_13_0.svg

Two classifiers from the learned BN

In [11]:
print(bn2["HRSAT"])
print(bn2["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);
../_images/notebooks_32-Learning_learningClassifier_16_0.svg
../_images/notebooks_32-Learning_learningClassifier_16_1.svg
../_images/notebooks_32-Learning_learningClassifier_16_2.svg
In [13]:
showROC(bn2,"out/train.csv",'INTUBATION',"ESOPHAGEAL",show_progress=False);
../_images/notebooks_32-Learning_learningClassifier_17_0.svg
In [14]:
showPR(bn2,"out/train.csv",'HRSAT','LOW',show_progress=False);
../_images/notebooks_32-Learning_learningClassifier_18_0.svg
In [15]:
showPR(bn2,"out/train.csv",'INTUBATION',"ESOPHAGEAL",show_progress=False);
../_images/notebooks_32-Learning_learningClassifier_19_0.svg
In [16]:
showROC_PR(bn2,"out/train.csv",'HRSAT','LOW',show_progress=False);
../_images/notebooks_32-Learning_learningClassifier_20_0.svg
In [17]:
showROC_PR(bn2,"out/train.csv",'INTUBATION',"ESOPHAGEAL",show_progress=False);
../_images/notebooks_32-Learning_learningClassifier_21_0.svg