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

Out[3]:
-30450.412065080312

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 CATECHOL CATECHOL HR HR CATECHOL->HR PRESS PRESS HRBP HRBP MINVOLSET MINVOLSET VENTMACH VENTMACH MINVOLSET->VENTMACH SHUNT SHUNT HYPOVOLEMIA HYPOVOLEMIA STROKEVOLUME STROKEVOLUME HYPOVOLEMIA->STROKEVOLUME CO CO STROKEVOLUME->CO CVP CVP PAP PAP PULMEMBOLUS PULMEMBOLUS PAP->PULMEMBOLUS LVEDVOLUME LVEDVOLUME LVEDVOLUME->HYPOVOLEMIA LVEDVOLUME->CVP PCWP PCWP LVEDVOLUME->PCWP VENTLUNG VENTLUNG EXPCO2 EXPCO2 VENTLUNG->EXPCO2 VENTALV VENTALV VENTLUNG->VENTALV ARTCO2 ARTCO2 PULMEMBOLUS->SHUNT VENTTUBE VENTTUBE VENTTUBE->PRESS VENTTUBE->VENTLUNG BP BP CO->BP HR->HRBP HR->CO HREKG HREKG HR->HREKG HRSAT HRSAT HR->HRSAT LVFAILURE LVFAILURE LVFAILURE->STROKEVOLUME LVFAILURE->LVEDVOLUME ERRCAUTER ERRCAUTER ERRCAUTER->HREKG ERRCAUTER->HRSAT MINVOL MINVOL TPR TPR TPR->BP ANAPHYLAXIS ANAPHYLAXIS ANAPHYLAXIS->TPR SAO2 SAO2 SAO2->CATECHOL INSUFFANESTH INSUFFANESTH HISTORY HISTORY HISTORY->LVFAILURE PVSAT PVSAT PVSAT->SAO2 INTUBATION INTUBATION INTUBATION->PRESS INTUBATION->SHUNT FIO2 FIO2 FIO2->PVSAT KINKEDTUBE KINKEDTUBE KINKEDTUBE->VENTLUNG ERRLOWOUTPUT ERRLOWOUTPUT ERRLOWOUTPUT->HRBP VENTMACH->VENTTUBE DISCONNECT DISCONNECT DISCONNECT->VENTTUBE DISCONNECT->ERRLOWOUTPUT VENTALV->ARTCO2 VENTALV->MINVOL VENTALV->PVSAT VENTALV->INTUBATION HRSAT->HREKG
Learned with 10000 lines in 0.264s
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 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 FIO2->PVSAT LVFAILURE LVFAILURE LVFAILURE->STROKEVOLUME LVFAILURE->LVEDVOLUME HISTORY HISTORY DISCONNECT DISCONNECT DISCONNECT->ERRLOWOUTPUT VENTTUBE VENTTUBE DISCONNECT->VENTTUBE ANAPHYLAXIS ANAPHYLAXIS TPR TPR ANAPHYLAXIS->TPR PAP->PULMEMBOLUS CO CO STROKEVOLUME->CO TPR->CATECHOL BP BP TPR->BP LVEDVOLUME->HYPOVOLEMIA 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->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 DISCONNECT DISCONNECT KINKEDTUBE->DISCONNECT VENTLUNG VENTLUNG PRESS PRESS KINKEDTUBE->PRESS HYPOVOLEMIA HYPOVOLEMIA STROKEVOLUME STROKEVOLUME LVEDVOLUME LVEDVOLUME HYPOVOLEMIA->LVEDVOLUME INTUBATION INTUBATION SHUNT SHUNT INTUBATION->SHUNT MINVOL MINVOL INTUBATION->MINVOL INTUBATION->PRESS VENTALV VENTALV MINVOLSET MINVOLSET VENTMACH VENTMACH PULMEMBOLUS PULMEMBOLUS PULMEMBOLUS->KINKEDTUBE PAP PAP PULMEMBOLUS->PAP PULMEMBOLUS->SHUNT INSUFFANESTH INSUFFANESTH CATECHOL CATECHOL INSUFFANESTH->CATECHOL ERRLOWOUTPUT ERRLOWOUTPUT HRBP HRBP ERRCAUTER ERRCAUTER HRSAT HRSAT ERRCAUTER->HRSAT HREKG HREKG ERRCAUTER->HREKG FIO2 FIO2 PVSAT PVSAT FIO2->PVSAT LVFAILURE LVFAILURE HISTORY HISTORY LVFAILURE->HISTORY ANAPHYLAXIS ANAPHYLAXIS DISCONNECT->ANAPHYLAXIS VENTTUBE VENTTUBE TPR TPR PAP->FIO2 STROKEVOLUME->HYPOVOLEMIA STROKEVOLUME->LVFAILURE STROKEVOLUME->LVEDVOLUME CO CO STROKEVOLUME->CO TPR->ANAPHYLAXIS BP BP TPR->BP LVEDVOLUME->LVFAILURE PCWP PCWP LVEDVOLUME->PCWP CVP CVP LVEDVOLUME->CVP VENTMACH->MINVOLSET VENTMACH->DISCONNECT SAO2 SAO2 SHUNT->SAO2 VENTTUBE->KINKEDTUBE VENTTUBE->INTUBATION VENTTUBE->DISCONNECT VENTTUBE->VENTMACH VENTTUBE->VENTLUNG VENTTUBE->PRESS VENTLUNG->KINKEDTUBE VENTLUNG->INTUBATION VENTLUNG->MINVOL VENTLUNG->VENTALV EXPCO2 EXPCO2 VENTALV->INTUBATION ARTCO2 ARTCO2 VENTALV->ARTCO2 VENTALV->PVSAT ARTCO2->CATECHOL PVSAT->SAO2 SAO2->ERRCAUTER SAO2->CATECHOL SAO2->HRBP EXPCO2->VENTTUBE EXPCO2->VENTLUNG EXPCO2->VENTALV EXPCO2->ARTCO2 CATECHOL->TPR HR HR HR->ERRLOWOUTPUT HR->CATECHOL HR->HRSAT HR->CO HR->HREKG HRBP->ERRLOWOUTPUT HRBP->HR CO->BP
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 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 MINVOLSET->VENTMACH PULMEMBOLUS PULMEMBOLUS PAP PAP PULMEMBOLUS->PAP PULMEMBOLUS->SHUNT INSUFFANESTH INSUFFANESTH CATECHOL CATECHOL INSUFFANESTH->CATECHOL ERRLOWOUTPUT ERRLOWOUTPUT DISCONNECT DISCONNECT ERRLOWOUTPUT->DISCONNECT HRBP HRBP ERRLOWOUTPUT->HRBP ERRCAUTER ERRCAUTER HRSAT HRSAT ERRCAUTER->HRSAT HREKG HREKG ERRCAUTER->HREKG FIO2 FIO2 PVSAT PVSAT FIO2->PVSAT LVFAILURE LVFAILURE LVFAILURE->KINKEDTUBE LVFAILURE->STROKEVOLUME LVFAILURE->LVEDVOLUME HISTORY HISTORY VENTTUBE VENTTUBE DISCONNECT->VENTTUBE ANAPHYLAXIS ANAPHYLAXIS ANAPHYLAXIS->KINKEDTUBE TPR TPR ANAPHYLAXIS->TPR PAP->FIO2 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 ARTCO2 ARTCO2 VENTALV->ARTCO2 VENTALV->PVSAT ARTCO2->EXPCO2 ARTCO2->CATECHOL PVSAT->SAO2 SAO2->ERRCAUTER 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())
{'CATECHOL', 'PRESS', 'HRBP', 'MINVOLSET', 'SHUNT', 'HYPOVOLEMIA', 'STROKEVOLUME', 'CVP', 'PAP', 'LVEDVOLUME', 'VENTLUNG', 'ARTCO2', 'PULMEMBOLUS', 'VENTTUBE', 'CO', 'HR', 'LVFAILURE', 'ERRCAUTER', 'MINVOL', 'TPR', 'ANAPHYLAXIS', 'SAO2', 'PCWP', 'INSUFFANESTH', 'BP', 'HISTORY', 'PVSAT', 'INTUBATION', 'HREKG', 'EXPCO2', 'FIO2', 'KINKEDTUBE', 'ERRLOWOUTPUT', 'VENTMACH', 'DISCONNECT', 'VENTALV', 'HRSAT'}
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