Python - Bayesian Network
Bayesian Network in pgmpy
- Import modules
1
2
3
4
5
6import pandas as pd
import networkx as nx
import matplotlib.pyplot as plt
from pgmpy.models import BayesianNetwork
from pgmpy.factors.discrete.CPD import TabularCPD
from pgmpy.inference import VariableElimination
- Construct the Bayesian Network
1
2
3
4
5
6
7
8
9
10
11student_model = BayesianNetwork([
("A", "C"),
("B", "C"),
("C", "D"),
("C", "E")])
# Plot the network
nx.draw_circular(
student_model, with_labels=True, arrowsize=10, node_size=4000, alpha=0.3, font_weight="bold"
)
plt.show()
- Set up the Conditional Probability Distribution and add the relationship
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40# Set up the Conditional Probability Distribution
A_cpd = TabularCPD(
variable = "A",
variable_card = 2,
values = [[.4],
[.6]]
)
expl_B_cpd = TabularCPD(
variable = "B",
variable_card = 2,
values = [[.7],
[.3]]
)
concept_c_cpd = TabularCPD(
variable = "C",
variable_card = 2,
values = [[.9, .5, .4, .05],
[.1, .5, .6, .95]],
evidence = ["A", "B"],
evidence_card = [2,2]
)
concept_d_cpd = TabularCPD(
variable = "D",
variable_card = 2,
values = [[.75, .1],
[.25, .9]],
evidence = ['C'],
evidence_card = [2]
)
concept_e_cpd = TabularCPD(
variable = "E",
variable_card = 2,
values = [[.8, .3],
[.2, .7]],
evidence = ['C'],
evidence_card = [2]
)
# Add the relationships
student_model.add_cpds(A_cpd, expl_B_cpd, concept_c_cpd, concept_d_cpd, concept_e_cpd)
- Finally, we can inference
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25from pgmpy.inference import VariableElimination
student_model_infer = VariableElimination(student_model)
print('Conditional Probability Distribution of the understanding of D given evidence: not understand Expl A and B,')
prob_understand_concept_d = student_model_infer.query(variables = ['D'], evidence = {'A':0, 'B':0})
print(prob_understand_concept_d, '\n')
# Conditional Probability Distribution of the understanding of D given evidence: not understand Expl A and B,
# +------+----------+
# | D | phi(D) |
# +======+==========+
# | D(0) | 0.6850 |
# +------+----------+
# | D(1) | 0.3150 |
# +------+----------+
print('Conditional Probability Distribution of the understanding of E given evidence: understand C,')
prob_understand_concept_e = student_model_infer.query(variables = ['E'], evidence = {'C':1})
print(prob_understand_concept_e)
# Conditional Probability Distribution of the understanding of E given evidence: understand C,
# +------+----------+
# | E | phi(E) |
# +======+==========+
# | E(0) | 0.3000 |
# +------+----------+
# | E(1) | 0.7000 |
# +------+----------+
Inference based on data
- We can inference based on a given data, then we can skip the step 3 that setting up the probability distribution
1
2
3
4
5
6
7
8
9
10
11
12
13
14# Learning conditional probability distribution using Maximum likelihood estimators
data = pd.read_csv('data.csv')
student_model.fit(data, estimator=MaximumLikelihoodEstimator)
# Inferencing with Bayesian Network
model_infer = VariableElimination(student_model)
# Probability of a student understands concept D, given evidence: understands both explanation A & B
prob_C = model_infer.query(variables = ['C'], evidence = {'A':1, 'B':0})
# prob_D = model_infer.query(variables = ['E'],evidence = {'A':1, 'B':0})
# Probability of a student understands concept E, given evidence: understands concept C
prob_E = model_infer.query(variables = ['E'], evidence = {'A':0})
print(prob_E)
All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.


