Click here to hide/show the list of notebooks.
  pyAgrum on notebooks   pyAgrum jupyter
☰  learningClassifier 
pyAgrum 0.18.0   
Zipped notebooks   
generation: 2020-06-11 14:09  

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

In [1]:
%load_ext autoreload
%autoreload 2

import sys
import os

import numpy as np

import pyAgrum as gum

%matplotlib inline
from pyAgrum.lib.bn2roc import showROC
from pyAgrum.lib.bn2roc import showPR
from pyAgrum.lib.bn2roc import showROC_PR
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 : -150928.90432902108

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

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

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) 
Out[9]:
(0.9761857571266072, 0.1129190662, 0.9681523203633746, 0.37500267945)