Learning classifiers

Creative Commons License

aGrUM

interactive online version

In [1]:
import pyAgrum.skbn as skbn
import pyAgrum.lib.notebook as gnb

import os
import pandas

skbn is a pyAgrum’s module that allows to use bayesian networks as classifier in the scikit-learn environment. ## Initialization of parameters

First, we initialize the parameters to indicate properties we want our classifier to have.

In [2]:
BNTest= skbn.BNClassifier(learningMethod = 'Chow-Liu', prior= 'Smoothing', priorWeight = 0.5,
                          discretizationStrategy = 'quantile', usePR = True, significant_digit = 13)

Then, we train the classifier thanks to two types of objects.

Learn from csv file

In [3]:
BNTest.fit(data = 'res/creditCardTest.csv', targetName = 'Class')
In [4]:
for i in BNTest.bn.nodes():
    print(BNTest.bn.variable(i))
Class:Labelized({0.0|1.0})
Time:Discretized(<(0;1578[,[1578;3733[,[3733;6982[,[6982;11033[,[11033;170348)>)
V1:Discretized(<(-30.5524;-1.33295[,[-1.33295;-0.654664[,[-0.654664;0.305375[,[0.305375;1.18346[,[1.18346;2.13239)>)
V2:Discretized(<(-25.6405;-0.362408[,[-0.362408;0.104022[,[0.104022;0.582468[,[0.582468;1.12626[,[1.12626;22.0577)>)
V3:Discretized(<(-31.1037;0.107723[,[0.107723;0.675277[,[0.675277;1.14525[,[1.14525;1.73106[,[1.73106;4.10172)>)
V4:Discretized(<(-4.65755;-0.835683[,[-0.835683;0.0334235[,[0.0334235;0.648386[,[0.648386;1.44563[,[1.44563;12.1147)>)
V5:Discretized(<(-22.1055;-0.813666[,[-0.813666;-0.355923[,[-0.355923;0.0329468[,[0.0329468;0.534605[,[0.534605;11.9743)>)
V6:Discretized(<(-7.5748;-0.789778[,[-0.789778;-0.370597[,[-0.370597;0.0353554[,[0.0353554;0.711815[,[0.711815;10.0339)>)
V7:Discretized(<(-43.5572;-0.691953[,[-0.691953;-0.264738[,[-0.264738;0.111993[,[0.111993;0.57616[,[0.57616;12.2192)>)
V8:Discretized(<(-41.0443;-0.248778[,[-0.248778;-0.0618973[,[-0.0618973;0.101159[,[0.101159;0.417327[,[0.417327;20.0072)>)
V9:Discretized(<(-13.4341;-0.258886[,[-0.258886;0.432783[,[0.432783;1.00315[,[1.00315;1.60675[,[1.60675;10.3929)>)
V10:Discretized(<(-24.5883;-0.887242[,[-0.887242;-0.486914[,[-0.486914;-0.17427[,[-0.17427;0.281998[,[0.281998;12.2599)>)
V11:Discretized(<(-2.59533;-0.21685[,[-0.21685;0.467606[,[0.467606;1.06928[,[1.06928;1.89436[,[1.89436;12.0189)>)
V12:Discretized(<(-18.6837;-2.60336[,[-2.60336;-1.98917[,[-1.98917;-1.01028[,[-1.01028;0.297745[,[0.297745;3.77484)>)
V13:Discretized(<(-3.38951;-0.277526[,[-0.277526;0.487335[,[0.487335;1.192[,[1.192;1.87168[,[1.87168;4.46541)>)
V14:Discretized(<(-19.2143;-0.198436[,[-0.198436;0.39438[,[0.39438;1.12921[,[1.12921;1.5604[,[1.5604;5.74873)>)
V15:Discretized(<(-4.49894;-0.898218[,[-0.898218;-0.252119[,[-0.252119;0.228109[,[0.228109;0.673846[,[0.673846;2.53366)>)
V16:Discretized(<(-14.1299;-0.73753[,[-0.73753;-0.191439[,[-0.191439;0.226074[,[0.226074;0.649708[,[0.649708;3.93088)>)
V17:Discretized(<(-25.1628;-0.37327[,[-0.37327;0.0631357[,[0.0631357;0.445363[,[0.445363;0.906548[,[0.906548;7.89339)>)
V18:Discretized(<(-9.49875;-0.642528[,[-0.642528;-0.179343[,[-0.179343;0.16627[,[0.16627;0.556347[,[0.556347;4.11556)>)
V19:Discretized(<(-4.93273;-0.673233[,[-0.673233;-0.228783[,[-0.228783;0.150301[,[0.150301;0.636972[,[0.636972;5.22834)>)
V20:Discretized(<(-13.276;-0.183662[,[-0.183662;-0.0677703[,[-0.0677703;0.0444333[,[0.0444333;0.232763[,[0.232763;11.059)>)
V21:Discretized(<(-22.7976;-0.298193[,[-0.298193;-0.179497[,[-0.179497;-0.0548622[,[-0.0548622;0.105119[,[0.105119;27.2028)>)
V22:Discretized(<(-8.88702;-0.649014[,[-0.649014;-0.291808[,[-0.291808;0.00962799[,[0.00962799;0.351258[,[0.351258;8.36199)>)
V23:Discretized(<(-19.2543;-0.215265[,[-0.215265;-0.0924607[,[-0.0924607;-1.53e-05[,[-1.53e-05;0.122579[,[0.122579;13.8762)>)
V24:Discretized(<(-2.51238;-0.441547[,[-0.441547;-0.0137249[,[-0.0137249;0.248364[,[0.248364;0.468669[,[0.468669;3.2002)>)
V25:Discretized(<(-4.78161;-0.238382[,[-0.238382;0.024469[,[0.024469;0.212094[,[0.212094;0.411607[,[0.411607;5.52509)>)
V26:Discretized(<(-1.33856;-0.390763[,[-0.390763;-0.124995[,[-0.124995;0.128837[,[0.128837;0.66481[,[0.66481;3.51735)>)
V27:Discretized(<(-7.9761;-0.0970824[,[-0.0970824;-0.0255692[,[-0.0255692;0.0347242[,[0.0347242;0.216123[,[0.216123;4.17339)>)
V28:Discretized(<(-3.05408;-0.0430296[,[-0.0430296;0.00633497[,[0.00633497;0.0299365[,[0.0299365;0.111331[,[0.111331;4.86077)>)
Amount:Discretized(<(0;2.78[,[2.78;11.66[,[11.66;25.52[,[25.52;73.5[,[73.5;4002.88)>)
In [5]:
gnb.sideBySide(BNTest.bn,gnb.getInference(BNTest.bn,size='15!'))
G V12 V12 V11 V11 V12->V11 V13 V13 V12->V13 V15 V15 V12->V15 V7 V7 V5 V5 V7->V5 V8 V8 V7->V8 V18 V18 V26 V26 V17 V17 V16 V16 V17->V16 V23 V23 V3 V3 V16->V18 V14 V14 V2 V2 V2->V7 V1 V1 V2->V1 Amount Amount V2->Amount V19 V19 V27 V27 V25 V25 V20 V20 V20->V19 V1->V23 V1->V3 V1->V27 V1->V25 V1->V20 V21 V21 V1->V21 V28 V28 V1->V28 Class Class Time Time Class->Time Time->V12 Time->V17 Time->V14 V9 V9 Time->V9 V10 V10 V4 V4 V10->V4 V22 V22 V9->V2 V9->V10 V6 V6 V8->V6 V4->V26 V24 V24 V21->V22 V6->V24
structs Inference in   6.94ms Class 2024-02-06T17:27:41.970920 image/svg+xml Matplotlib v3.8.2, https://matplotlib.org/ Time 2024-02-06T17:27:42.186167 image/svg+xml Matplotlib v3.8.2, https://matplotlib.org/ Class->Time V9 2024-02-06T17:27:42.507003 image/svg+xml Matplotlib v3.8.2, https://matplotlib.org/ Time->V9 V12 2024-02-06T17:27:42.609933 image/svg+xml Matplotlib v3.8.2, https://matplotlib.org/ Time->V12 V14 2024-02-06T17:27:42.703588 image/svg+xml Matplotlib v3.8.2, https://matplotlib.org/ Time->V14 V17 2024-02-06T17:27:42.797304 image/svg+xml Matplotlib v3.8.2, https://matplotlib.org/ Time->V17 V1 2024-02-06T17:27:42.242875 image/svg+xml Matplotlib v3.8.2, https://matplotlib.org/ V3 2024-02-06T17:27:42.307544 image/svg+xml Matplotlib v3.8.2, https://matplotlib.org/ V1->V3 V20 2024-02-06T17:27:42.894292 image/svg+xml Matplotlib v3.8.2, https://matplotlib.org/ V1->V20 V21 2024-02-06T17:27:42.928931 image/svg+xml Matplotlib v3.8.2, https://matplotlib.org/ V1->V21 V23 2024-02-06T17:27:42.996146 image/svg+xml Matplotlib v3.8.2, https://matplotlib.org/ V1->V23 V25 2024-02-06T17:27:43.064783 image/svg+xml Matplotlib v3.8.2, https://matplotlib.org/ V1->V25 V27 2024-02-06T17:27:43.135190 image/svg+xml Matplotlib v3.8.2, https://matplotlib.org/ V1->V27 V28 2024-02-06T17:27:43.170607 image/svg+xml Matplotlib v3.8.2, https://matplotlib.org/ V1->V28 V2 2024-02-06T17:27:42.276182 image/svg+xml Matplotlib v3.8.2, https://matplotlib.org/ V2->V1 V7 2024-02-06T17:27:42.443076 image/svg+xml Matplotlib v3.8.2, https://matplotlib.org/ V2->V7 Amount 2024-02-06T17:27:43.428874 image/svg+xml Matplotlib v3.8.2, https://matplotlib.org/ V2->Amount V4 2024-02-06T17:27:42.343736 image/svg+xml Matplotlib v3.8.2, https://matplotlib.org/ V26 2024-02-06T17:27:43.102130 image/svg+xml Matplotlib v3.8.2, https://matplotlib.org/ V4->V26 V5 2024-02-06T17:27:42.377270 image/svg+xml Matplotlib v3.8.2, https://matplotlib.org/ V6 2024-02-06T17:27:42.411324 image/svg+xml Matplotlib v3.8.2, https://matplotlib.org/ V24 2024-02-06T17:27:43.029572 image/svg+xml Matplotlib v3.8.2, https://matplotlib.org/ V6->V24 V7->V5 V8 2024-02-06T17:27:42.474500 image/svg+xml Matplotlib v3.8.2, https://matplotlib.org/ V7->V8 V8->V6 V9->V2 V10 2024-02-06T17:27:42.538880 image/svg+xml Matplotlib v3.8.2, https://matplotlib.org/ V9->V10 V10->V4 V11 2024-02-06T17:27:42.576937 image/svg+xml Matplotlib v3.8.2, https://matplotlib.org/ V12->V11 V13 2024-02-06T17:27:42.641677 image/svg+xml Matplotlib v3.8.2, https://matplotlib.org/ V12->V13 V15 2024-02-06T17:27:42.735758 image/svg+xml Matplotlib v3.8.2, https://matplotlib.org/ V12->V15 V16 2024-02-06T17:27:42.766903 image/svg+xml Matplotlib v3.8.2, https://matplotlib.org/ V18 2024-02-06T17:27:42.828347 image/svg+xml Matplotlib v3.8.2, https://matplotlib.org/ V16->V18 V17->V16 V19 2024-02-06T17:27:42.860938 image/svg+xml Matplotlib v3.8.2, https://matplotlib.org/