Path: blob/master/deprecated/notebooks/student_pgm.ipynb
1192 views
Kernel: Python 3
The (simplified) student Bayes net
This model is from https://github.com/pgmpy/pgmpy_notebook/blob/master/notebooks/2. Bayesian Networks.ipynb
In [1]:
Out[1]:
|████████████████████████████████| 1.9 MB 4.1 MB/s
Collecting superimport
Downloading superimport-0.3.4.tar.gz (6.0 kB)
Requirement already satisfied: requests in /usr/local/lib/python3.7/dist-packages (from superimport) (2.23.0)
Requirement already satisfied: idna<3,>=2.5 in /usr/local/lib/python3.7/dist-packages (from requests->superimport) (2.10)
Requirement already satisfied: chardet<4,>=3.0.2 in /usr/local/lib/python3.7/dist-packages (from requests->superimport) (3.0.4)
Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /usr/local/lib/python3.7/dist-packages (from requests->superimport) (1.24.3)
Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.7/dist-packages (from requests->superimport) (2021.10.8)
Building wheels for collected packages: superimport
Building wheel for superimport (setup.py) ... done
Created wheel for superimport: filename=superimport-0.3.4-py3-none-any.whl size=5888 sha256=aff8dbabb07f3802e05efca77d01225c61beaf08305bca7ad76b0891f4a287a9
Stored in directory: /root/.cache/pip/wheels/6c/66/dc/337052d868002cf3830606ee34d91d1ceff6a67bf8df982c72
Successfully built superimport
Installing collected packages: superimport
Successfully installed superimport-0.3.4
INFO:numexpr.utils:NumExpr defaulting to 2 threads.
In [2]:
Model
In [3]:
Out[3]:
/usr/local/lib/python3.7/dist-packages/pgmpy/models/BayesianModel.py:10: FutureWarning: BayesianModel has been renamed to BayesianNetwork. Please use BayesianNetwork class, BayesianModel will be removed in future.
FutureWarning,
Basic CPDs
In [4]:
Out[4]:
True
In [ ]:
+----------+----------+----------+----------+----------+
| Intel | Intel(0) | Intel(0) | Intel(1) | Intel(1) |
+----------+----------+----------+----------+----------+
| Diff | Diff(0) | Diff(1) | Diff(0) | Diff(1) |
+----------+----------+----------+----------+----------+
| Grade(0) | 0.3 | 0.05 | 0.9 | 0.5 |
+----------+----------+----------+----------+----------+
| Grade(1) | 0.4 | 0.25 | 0.08 | 0.3 |
+----------+----------+----------+----------+----------+
| Grade(2) | 0.3 | 0.7 | 0.02 | 0.2 |
+----------+----------+----------+----------+----------+
CPDs with names states
In [5]:
Out[5]:
INFO:root:Replacing existing CPD for Diff
INFO:root:Replacing existing CPD for Intel
INFO:root:Replacing existing CPD for Grade
INFO:root:Replacing existing CPD for Letter
INFO:root:Replacing existing CPD for SAT
True
In [6]:
Out[6]:
+----------+------------+------------+-------------+-------------+
| Intel | Intel(Low) | Intel(Low) | Intel(High) | Intel(High) |
+----------+------------+------------+-------------+-------------+
| Diff | Diff(Easy) | Diff(Hard) | Diff(Easy) | Diff(Hard) |
+----------+------------+------------+-------------+-------------+
| Grade(A) | 0.3 | 0.05 | 0.9 | 0.5 |
+----------+------------+------------+-------------+-------------+
| Grade(B) | 0.4 | 0.25 | 0.08 | 0.3 |
+----------+------------+------------+-------------+-------------+
| Grade(C) | 0.3 | 0.7 | 0.02 | 0.2 |
+----------+------------+------------+-------------+-------------+
In [ ]:
+------------+-----+
| Diff(Easy) | 0.6 |
+------------+-----+
| Diff(Hard) | 0.4 |
+------------+-----+
+-------------+-----+
| Intel(Low) | 0.7 |
+-------------+-----+
| Intel(High) | 0.3 |
+-------------+-----+
+----------+------------+------------+-------------+-------------+
| Intel | Intel(Low) | Intel(Low) | Intel(High) | Intel(High) |
+----------+------------+------------+-------------+-------------+
| Diff | Diff(Easy) | Diff(Hard) | Diff(Easy) | Diff(Hard) |
+----------+------------+------------+-------------+-------------+
| Grade(A) | 0.3 | 0.05 | 0.9 | 0.5 |
+----------+------------+------------+-------------+-------------+
| Grade(B) | 0.4 | 0.25 | 0.08 | 0.3 |
+----------+------------+------------+-------------+-------------+
| Grade(C) | 0.3 | 0.7 | 0.02 | 0.2 |
+----------+------------+------------+-------------+-------------+
+--------------+----------+----------+----------+
| Grade | Grade(A) | Grade(B) | Grade(C) |
+--------------+----------+----------+----------+
| Letter(Bad) | 0.1 | 0.4 | 0.99 |
+--------------+----------+----------+----------+
| Letter(Good) | 0.9 | 0.6 | 0.01 |
+--------------+----------+----------+----------+
+-----------+------------+-------------+
| Intel | Intel(Low) | Intel(High) |
+-----------+------------+-------------+
| SAT(Bad) | 0.95 | 0.2 |
+-----------+------------+-------------+
| SAT(Good) | 0.05 | 0.8 |
+-----------+------------+-------------+
Inference
In [ ]:
/usr/local/lib/python3.7/dist-packages/statsmodels/tools/_testing.py:19: FutureWarning: pandas.util.testing is deprecated. Use the functions in the public API at pandas.testing instead.
import pandas.util.testing as tm
Posterior given Grade=C
In [ ]:
0%| | 0/1 [00:00<?, ?it/s]
0%| | 0/1 [00:00<?, ?it/s]
0%| | 0/1 [00:00<?, ?it/s]
0%| | 0/1 [00:00<?, ?it/s]
Pr(Difficulty=Hard|Grade=C) = 0.63
Pr(Intelligence=High|Grade=C) = 0.08
Posterior given Grade=C, SAT=Good
In [ ]:
0%| | 0/1 [00:00<?, ?it/s]
0%| | 0/1 [00:00<?, ?it/s]
0%| | 0/1 [00:00<?, ?it/s]
0%| | 0/1 [00:00<?, ?it/s]
Pr(Difficulty=Hard|Grade=C,SAT=Good) = 0.76
Pr(Intelligence=High|Grade=C,SAT=Good) = 0.58
Visualization
DAG
In [ ]:
<class 'graphviz.dot.Digraph'>
'student_pgm.pdf'
In [7]:
CPTs
In [8]:
Out[8]:
'student_pgm_with_cpt.pdf'
Marginals
In [ ]:
0%| | 0/1 [00:00<?, ?it/s]
0%| | 0/1 [00:00<?, ?it/s]
0%| | 0/1 [00:00<?, ?it/s]
0%| | 0/1 [00:00<?, ?it/s]
0it [00:00, ?it/s]
0it [00:00, ?it/s]
0%| | 0/2 [00:00<?, ?it/s]
0%| | 0/2 [00:00<?, ?it/s]
{'Diff': array([0.37070938, 0.62929062]), 'Grade': array([0., 0., 1.]), 'Intel': array([0.92105263, 0.07894737]), 'Letter': array([0.99, 0.01]), 'SAT': array([0.89078947, 0.10921053])}
In [ ]:
'student_pgm_marginals_given_grade.pdf'
In [ ]:
0%| | 0/1 [00:00<?, ?it/s]
0%| | 0/1 [00:00<?, ?it/s]
0%| | 0/1 [00:00<?, ?it/s]
0%| | 0/1 [00:00<?, ?it/s]
0it [00:00, ?it/s]
0it [00:00, ?it/s]
{'Diff': array([0.24044002, 0.75955998]), 'Grade': array([0., 0., 1.]), 'Intel': array([0.42168675, 0.57831325]), 'Letter': array([0.99, 0.01]), 'SAT': array([0., 1.])}
'student_pgm_marginals_given_grade_sat.pdf'
In [ ]:
In [ ]: