Click here to hide/show the list of notebooks.
  pyAgrum on notebooks   pyAgrum jupyter
☰  learningClassifier 
pyAgrum 0.15.2   
Zipped notebooks   
generation: 2019-07-22 10:34  

Creative Commons License
This pyAgrum's notebook is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.

In [1]:
import sys
import os

import numpy as np

import pyAgrum as gum

%matplotlib inline
from pyAgrum.lib.bn2roc import showROC
In [2]:
bn=gum.loadBN(os.path.join("res","alarm.dsl"))
In [3]:
gum.generateCSV(bn,os.path.join("out","learn.csv"),10000,visible=True,with_labels=True)
print()
gum.generateCSV(bn,os.path.join("out","train.csv"),1000,visible=True,with_labels=True)
 out/learn.csv : [ ########################################################### ] 100%
Log2-Likelihood : -151734.25668516423

 out/train.csv : [ ########################################################### ] 100%
Log2-Likelihood : -15026.155910227557
Out[3]:
-15026.155910227557

Learning a BN from learn.csv

In [4]:
# Learning a BN from the database
learner=gum.BNLearner(os.path.join("out","train.csv"))

# # Hill-Climbing
learner.useGreedyHillClimbing()
bn2=learner.learnBN()
bn2.setProperty("name","Learned_{0}s".format(learner.currentTime()))
In [5]:
import pyAgrum.lib.notebook as gnb
gnb.showBN(bn2,size="8",format="svg")
G ARTCO2 ARTCO2 EXPCO2 EXPCO2 ARTCO2->EXPCO2 FIO2 FIO2 PVSAT PVSAT FIO2->PVSAT LVFAILURE LVFAILURE STROKEVOLUME STROKEVOLUME LVFAILURE->STROKEVOLUME HREKG HREKG ERRCAUTER ERRCAUTER HREKG->ERRCAUTER SHUNT SHUNT SAO2 SAO2 SHUNT->SAO2 PCWP PCWP KINKEDTUBE KINKEDTUBE HRBP HRBP ERRLOWOUTPUT ERRLOWOUTPUT HRBP->ERRLOWOUTPUT HR HR HRBP->HR PULMEMBOLUS PULMEMBOLUS PULMEMBOLUS->SHUNT PAP PAP PULMEMBOLUS->PAP MINVOLSET MINVOLSET VENTTUBE VENTTUBE MINVOLSET->VENTTUBE VENTMACH VENTMACH MINVOLSET->VENTMACH PRESS PRESS PRESS->LVFAILURE PRESS->KINKEDTUBE VENTALV VENTALV VENTALV->ARTCO2 MINVOL MINVOL VENTALV->MINVOL VENTALV->PVSAT INTUBATION INTUBATION VENTALV->INTUBATION BP BP CO CO CO->BP HRSAT HRSAT HRSAT->HREKG HRSAT->ERRCAUTER SAO2->LVFAILURE CATECHOL CATECHOL SAO2->CATECHOL VENTLUNG VENTLUNG VENTLUNG->KINKEDTUBE VENTLUNG->VENTALV VENTLUNG->EXPCO2 VENTLUNG->MINVOL VENTLUNG->INTUBATION LVEDVOLUME LVEDVOLUME LVEDVOLUME->LVFAILURE LVEDVOLUME->PCWP HYPOVOLEMIA HYPOVOLEMIA LVEDVOLUME->HYPOVOLEMIA CVP CVP LVEDVOLUME->CVP LVEDVOLUME->STROKEVOLUME DISCONNECT DISCONNECT DISCONNECT->VENTTUBE INSUFFANESTH INSUFFANESTH ERRLOWOUTPUT->INSUFFANESTH ERRLOWOUTPUT->VENTTUBE ERRLOWOUTPUT->HR TPR TPR TPR->BP ANAPHYLAXIS ANAPHYLAXIS TPR->ANAPHYLAXIS VENTTUBE->PRESS VENTTUBE->VENTLUNG VENTTUBE->INTUBATION VENTMACH->VENTTUBE STROKEVOLUME->CO STROKEVOLUME->HYPOVOLEMIA ANAPHYLAXIS->KINKEDTUBE HISTORY HISTORY HISTORY->LVFAILURE HISTORY->LVEDVOLUME PVSAT->SAO2 INTUBATION->SHUNT INTUBATION->ERRCAUTER INTUBATION->KINKEDTUBE INTUBATION->PRESS INTUBATION->MINVOL HR->HREKG HR->ERRCAUTER HR->CO HR->HRSAT HR->CATECHOL
In [6]:
print(bn2.names())
['ARTCO2', 'FIO2', 'LVFAILURE', 'HREKG', 'SHUNT', 'PCWP', 'ERRCAUTER', 'KINKEDTUBE', 'HRBP', 'PULMEMBOLUS', 'MINVOLSET', 'PRESS', 'VENTALV', 'BP', 'CO', 'HRSAT', 'SAO2', 'CATECHOL', 'VENTLUNG', 'LVEDVOLUME', 'DISCONNECT', 'INSUFFANESTH', 'EXPCO2', 'MINVOL', 'PAP', 'ERRLOWOUTPUT', 'TPR', 'VENTTUBE', 'HYPOVOLEMIA', 'CVP', 'VENTMACH', 'STROKEVOLUME', 'ANAPHYLAXIS', 'HISTORY', 'PVSAT', 'INTUBATION', 'HR']
In [7]:
gnb.showInference(bn2,evs={},size="12")
structs Inference in   7.11ms ARTCO2 EXPCO2 ARTCO2->EXPCO2 FIO2 PVSAT FIO2->PVSAT LVFAILURE STROKEVOLUME LVFAILURE->STROKEVOLUME HREKG ERRCAUTER HREKG->ERRCAUTER SHUNT SAO2 SHUNT->SAO2 PCWP KINKEDTUBE HRBP ERRLOWOUTPUT HRBP->ERRLOWOUTPUT HR HRBP->HR PULMEMBOLUS PULMEMBOLUS->SHUNT PAP PULMEMBOLUS->PAP MINVOLSET VENTTUBE MINVOLSET->VENTTUBE VENTMACH MINVOLSET->VENTMACH PRESS PRESS->LVFAILURE PRESS->KINKEDTUBE VENTALV VENTALV->ARTCO2 MINVOL VENTALV->MINVOL VENTALV->PVSAT INTUBATION VENTALV->INTUBATION BP CO CO->BP HRSAT HRSAT->HREKG HRSAT->ERRCAUTER SAO2->LVFAILURE CATECHOL SAO2->CATECHOL VENTLUNG VENTLUNG->KINKEDTUBE VENTLUNG->VENTALV VENTLUNG->EXPCO2 VENTLUNG->MINVOL VENTLUNG->INTUBATION LVEDVOLUME LVEDVOLUME->LVFAILURE LVEDVOLUME->PCWP HYPOVOLEMIA LVEDVOLUME->HYPOVOLEMIA CVP LVEDVOLUME->CVP LVEDVOLUME->STROKEVOLUME DISCONNECT DISCONNECT->VENTTUBE INSUFFANESTH ERRLOWOUTPUT->INSUFFANESTH ERRLOWOUTPUT->VENTTUBE ERRLOWOUTPUT->HR TPR TPR->BP ANAPHYLAXIS TPR->ANAPHYLAXIS VENTTUBE->PRESS VENTTUBE->VENTLUNG VENTTUBE->INTUBATION VENTMACH->VENTTUBE STROKEVOLUME->CO STROKEVOLUME->HYPOVOLEMIA ANAPHYLAXIS->KINKEDTUBE HISTORY HISTORY->LVFAILURE HISTORY->LVEDVOLUME PVSAT->SAO2 INTUBATION->SHUNT INTUBATION->ERRCAUTER INTUBATION->KINKEDTUBE INTUBATION->PRESS INTUBATION->MINVOL HR->HREKG HR->ERRCAUTER HR->CO HR->HRSAT HR->CATECHOL

Two classifiers from the learned BN

In [8]:
print(bn2.variableFromName("HRSAT"))
print(bn2.variableFromName("INTUBATION"))
HRSAT<HIGH,LOW,NORMAL>
INTUBATION<ESOPHAGEAL,NORMAL,ONESIDED>
In [9]:
showROC(bn2,os.path.join("out","train.csv"),'HRSAT','LOW',visible=False) 
 result in out/train.csv-ROC_Learned_0.059534249000000004s-HRSAT-LOW.png