Click here to hide/show the list of notebooks.
  pyAgrum on notebooks   pyAgrum jupyter
☰  learningClassifier 
pyAgrum 0.16.3   
Zipped notebooks   
generation: 2019-10-20 09:16  

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 : -152038.0653689252

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

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")
G DISCONNECT DISCONNECT EXPCO2 EXPCO2 LVEDVOLUME LVEDVOLUME CVP CVP LVEDVOLUME->CVP LVFAILURE LVFAILURE LVEDVOLUME->LVFAILURE HYPOVOLEMIA HYPOVOLEMIA LVEDVOLUME->HYPOVOLEMIA HISTORY HISTORY LVEDVOLUME->HISTORY PCWP PCWP LVEDVOLUME->PCWP ERRCAUTER ERRCAUTER CATECHOL CATECHOL TPR TPR CATECHOL->TPR SAO2 SAO2 CATECHOL->SAO2 BP BP TPR->BP ANAPHYLAXIS ANAPHYLAXIS TPR->ANAPHYLAXIS STROKEVOLUME STROKEVOLUME STROKEVOLUME->LVEDVOLUME CO CO STROKEVOLUME->CO STROKEVOLUME->HYPOVOLEMIA STROKEVOLUME->HISTORY CO->BP KINKEDTUBE KINKEDTUBE PRESS PRESS KINKEDTUBE->PRESS HREKG HREKG KINKEDTUBE->HREKG VENTLUNG VENTLUNG VENTLUNG->EXPCO2 VENTLUNG->KINKEDTUBE MINVOL MINVOL VENTLUNG->MINVOL VENTTUBE VENTTUBE VENTLUNG->VENTTUBE VENTALV VENTALV VENTLUNG->VENTALV INTUBATION INTUBATION VENTLUNG->INTUBATION FIO2 FIO2 PVSAT PVSAT FIO2->PVSAT HR HR HR->ERRCAUTER HR->CATECHOL HR->CO HR->FIO2 HRBP HRBP HR->HRBP HRSAT HRSAT HR->HRSAT HR->HREKG ERRLOWOUTPUT ERRLOWOUTPUT ERRLOWOUTPUT->HRBP HISTORY->LVFAILURE ARTCO2 ARTCO2 ARTCO2->EXPCO2 VENTMACH VENTMACH VENTMACH->DISCONNECT MINVOLSET MINVOLSET VENTMACH->MINVOLSET MINVOL->INTUBATION PULMEMBOLUS PULMEMBOLUS PCWP->PULMEMBOLUS VENTTUBE->DISCONNECT VENTTUBE->KINKEDTUBE VENTTUBE->PRESS VENTTUBE->VENTMACH PVSAT->ERRCAUTER PVSAT->SAO2 SHUNT SHUNT SHUNT->SAO2 MINVOLSET->DISCONNECT VENTALV->ARTCO2 VENTALV->MINVOL VENTALV->PVSAT VENTALV->INTUBATION HRSAT->ERRCAUTER INSUFFANESTH INSUFFANESTH PULMEMBOLUS->DISCONNECT PULMEMBOLUS->KINKEDTUBE PULMEMBOLUS->PRESS PULMEMBOLUS->SHUNT PAP PAP PULMEMBOLUS->PAP HREKG->ERRCAUTER HREKG->HRSAT INTUBATION->KINKEDTUBE INTUBATION->PRESS INTUBATION->VENTTUBE INTUBATION->SHUNT
In [6]:
print(bn2.names())
['DISCONNECT', 'EXPCO2', 'LVEDVOLUME', 'ERRCAUTER', 'CATECHOL', 'TPR', 'STROKEVOLUME', 'CO', 'CVP', 'KINKEDTUBE', 'VENTLUNG', 'FIO2', 'SAO2', 'LVFAILURE', 'HR', 'HYPOVOLEMIA', 'ERRLOWOUTPUT', 'HISTORY', 'PRESS', 'BP', 'ARTCO2', 'VENTMACH', 'HRBP', 'MINVOL', 'PCWP', 'VENTTUBE', 'PVSAT', 'SHUNT', 'MINVOLSET', 'VENTALV', 'HRSAT', 'ANAPHYLAXIS', 'INSUFFANESTH', 'PULMEMBOLUS', 'HREKG', 'PAP', 'INTUBATION']
In [7]:
gnb.showInference(bn2,evs={},size="12")
structs Inference in   5.41ms DISCONNECT EXPCO2 LVEDVOLUME CVP LVEDVOLUME->CVP LVFAILURE LVEDVOLUME->LVFAILURE HYPOVOLEMIA LVEDVOLUME->HYPOVOLEMIA HISTORY LVEDVOLUME->HISTORY PCWP LVEDVOLUME->PCWP ERRCAUTER CATECHOL TPR CATECHOL->TPR SAO2 CATECHOL->SAO2 BP TPR->BP ANAPHYLAXIS TPR->ANAPHYLAXIS STROKEVOLUME STROKEVOLUME->LVEDVOLUME CO STROKEVOLUME->CO STROKEVOLUME->HYPOVOLEMIA STROKEVOLUME->HISTORY CO->BP KINKEDTUBE PRESS KINKEDTUBE->PRESS HREKG KINKEDTUBE->HREKG VENTLUNG VENTLUNG->EXPCO2 VENTLUNG->KINKEDTUBE MINVOL VENTLUNG->MINVOL VENTTUBE VENTLUNG->VENTTUBE VENTALV VENTLUNG->VENTALV INTUBATION VENTLUNG->INTUBATION FIO2 PVSAT FIO2->PVSAT HR HR->ERRCAUTER HR->CATECHOL HR->CO HR->FIO2 HRBP HR->HRBP HRSAT HR->HRSAT HR->HREKG ERRLOWOUTPUT ERRLOWOUTPUT->HRBP HISTORY->LVFAILURE ARTCO2 ARTCO2->EXPCO2 VENTMACH VENTMACH->DISCONNECT MINVOLSET VENTMACH->MINVOLSET MINVOL->INTUBATION PULMEMBOLUS PCWP->PULMEMBOLUS VENTTUBE->DISCONNECT VENTTUBE->KINKEDTUBE VENTTUBE->PRESS VENTTUBE->VENTMACH PVSAT->ERRCAUTER PVSAT->SAO2 SHUNT SHUNT->SAO2 MINVOLSET->DISCONNECT VENTALV->ARTCO2 VENTALV->MINVOL VENTALV->PVSAT VENTALV->INTUBATION HRSAT->ERRCAUTER INSUFFANESTH PULMEMBOLUS->DISCONNECT PULMEMBOLUS->KINKEDTUBE PULMEMBOLUS->PRESS PULMEMBOLUS->SHUNT PAP PULMEMBOLUS->PAP HREKG->ERRCAUTER HREKG->HRSAT INTUBATION->KINKEDTUBE INTUBATION->PRESS INTUBATION->VENTTUBE INTUBATION->SHUNT

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.049381048999999996s-HRSAT-LOW.png