Skip to content

From a Bayesian network to a Classifier

This notebook shows how to build a classifier from an Bayesian network (and not from a database).

Creative Commons LicenseaGrUMinteractive online version
import pyagrum as gum
import pyagrum.skbn as skbn
import pyagrum.lib.notebook as gnb
bn = gum.loadBN("res/alarm.dsl")
gnb.showBN(bn, size="10")
print(bn.variable("HR"))

svg

HR:Labelized({LOW|NORMAL|HIGH})

Let’s say that you would like to use this Bayesian network to learn a classifier for the class HR (3 classes)

## generating the base of 100 values for testing purpose
print(f"LL(alarm-100)={gum.generateSample(bn, 100, 'out/alarm-100.csv')}")
LL(alarm-100)=-1440.3001764984908
bnc = skbn.BNClassifier()
bnc.fromTrainedModel(bn, targetAttribute="HR")
print(f"Binary classifier : {bnc.isBinaryClassifier}")
gnb.showBN(bnc.MarkovBlanket)
xTrain, yTrain = bnc.XYfromCSV(filename="out/alarm-100.csv")
print(f"predicted : {list(bnc.predict(xTrain))}")
print(f"in base : {yTrain.to_list()}")
Binary classifier : False

svg

predicted : [np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('NORMAL'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('NORMAL'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('NORMAL'), np.str_('HIGH'), np.str_('NORMAL'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('NORMAL'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('NORMAL'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('NORMAL'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('NORMAL'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('NORMAL'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('NORMAL'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH')]
in base : ['HIGH', 'HIGH', 'HIGH', 'HIGH', 'HIGH', 'HIGH', 'NORMAL', 'HIGH', 'HIGH', 'HIGH', 'HIGH', 'HIGH', 'NORMAL', 'HIGH', 'HIGH', 'HIGH', 'HIGH', 'HIGH', 'HIGH', 'HIGH', 'HIGH', 'HIGH', 'HIGH', 'HIGH', 'LOW', 'HIGH', 'NORMAL', 'HIGH', 'HIGH', 'HIGH', 'HIGH', 'NORMAL', 'HIGH', 'HIGH', 'HIGH', 'HIGH', 'HIGH', 'HIGH', 'HIGH', 'HIGH', 'NORMAL', 'HIGH', 'HIGH', 'HIGH', 'HIGH', 'HIGH', 'HIGH', 'HIGH', 'HIGH', 'HIGH', 'NORMAL', 'HIGH', 'HIGH', 'HIGH', 'HIGH', 'HIGH', 'HIGH', 'HIGH', 'HIGH', 'HIGH', 'HIGH', 'HIGH', 'HIGH', 'HIGH', 'HIGH', 'HIGH', 'HIGH', 'HIGH', 'HIGH', 'HIGH', 'HIGH', 'HIGH', 'HIGH', 'HIGH', 'HIGH', 'NORMAL', 'HIGH', 'HIGH', 'HIGH', 'HIGH', 'HIGH', 'HIGH', 'HIGH', 'HIGH', 'HIGH', 'NORMAL', 'HIGH', 'HIGH', 'HIGH', 'HIGH', 'HIGH', 'HIGH', 'HIGH', 'NORMAL', 'HIGH', 'HIGH', 'HIGH', 'HIGH', 'HIGH', 'HIGH']
print(list(bnc.predict(X="out/alarm-100.csv")))
[np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('NORMAL'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('NORMAL'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('NORMAL'), np.str_('HIGH'), np.str_('NORMAL'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('NORMAL'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('NORMAL'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('NORMAL'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('NORMAL'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('NORMAL'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('NORMAL'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH'), np.str_('HIGH')]
scoreCSV1 = bnc.score("out/alarm-100.csv", y=yTrain)
print("{0:.2f}% good predictions".format(100 * scoreCSV1))
99.00% good predictions

From a Bayesian network to a Binary classifier

Section titled “From a Bayesian network to a Binary classifier”

By targetting a specific label, one can create a binary classifier to predict this very target.

bnc = skbn.BNClassifier()
bnc.fromTrainedModel(bn, targetAttribute="HR", targetModality="LOW")
print(f"Binary classifier : {bnc.isBinaryClassifier}")
Binary classifier : True
gnb.showBN(bnc.MarkovBlanket)

svg

xTrain, yTrain = bnc.XYfromCSV(filename="out/alarm-100.csv")
print(f"predicted : {list(bnc.predict(xTrain))}")
print(f"in base : {yTrain.to_list()}")
predicted : [np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_]
in base : [False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, True, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False]
print(list(bnc.predict(X="out/alarm-100.csv")))
[np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_, np.False_]
scoreCSV1 = bnc.score("out/alarm-100.csv", y=yTrain)
print("{0:.2f}% good predictions".format(100 * scoreCSV1))
99.00% good predictions
print(f"LL(alarm-1000)={gum.generateSample(bn, 1000, 'out/alarm-1000.csv', with_labels=True)}")
bnc.showROC_PR("out/alarm-1000.csv")
LL(alarm-1000)=-14895.97330358777

svg

import pyagrum.lib.bn2roc as bn2roc
bn2roc.animROC(bnc.bn, "out/alarm-1000.csv", target="HR", label="LOW")
interactive(children=(IntSlider(value=50, description='rate'), Output(layout=Layout(height='250px'))), _dom_cl…
bn2roc.animPR(bnc.bn, "out/alarm-1000.csv", target="HR", label="LOW")
interactive(children=(IntSlider(value=50, description='rate'), Output(layout=Layout(height='250px'))), _dom_cl…