[PYTHON] pgmpy: Versuch eines diskreten Beckennetzwerks bis zur Schlussfolgerung

Auslösen

Interessiert an Meta-Learning, grafischen neuronalen Netzen, Verwendung von Wissensstrukturen einschließlich der Verwendung als Vorwissen und Wissensspeicherung. In diesem Zusammenhang suchte ich nach einer Bibliothek, mit der sich ein basianisches Netzwerk problemlos implementieren lässt. Da pgmpy gut aussah, werde ich den allgemeinen Fluss aufzeichnen.

pgmpy:pgmpy is a python library for working with Probabilistic Graphical Models. https://pgmpy.org/

Referenz

Implementierung eines basianischen Netzwerks mit Titanic-Daten https://qiita.com/YuyaOmori/items/e051f0360d1f9562620b

Basian Network: Von der Einführung über die Anwendung bis zur Modellierung des Menschen https://staff.aist.go.jp/y.motomura/paper/BSJ0403.pdf

Umgebung

Windows10 Python3.7 Anaconda pgmpy==0.1.9

Installation

pip install pgmpy==0.1.9

Wenn es nicht GPU ohne Pytorch ist

conda install pytorch torchvision cpuonly -c pytorch

Daten

Verwenden Sie die folgenden Daten

python


import pandas as pd
df = pd.DataFrame()
df['t'] = [1, 1, 1, 1, 0, 0, 1, 1, 1, 2, 0, 0, 1, 1, 1, 2, 2, 2, 2, 2]
df['a'] = [2, 2, 2, 2, 1, 1, 1, 1, 2, 1, 1, 2, 0, 0, 0, 1, 1, 2, 2, 2]
df['h'] = [0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1]

Lauf

Definition der Modellstruktur

python


from pgmpy.models import BayesianModel
model = BayesianModel([('t','h'),('a','h')])

Der Graph ist ein gerichteter Nichtzirkulationsgraph von t → h und a → h. image.png

Erstellen und überprüfen Sie die CPD im Modell

python


model.fit(df) #Bedingungen werden weggelassen. Achten Sie besonders auf die Überanpassung

print(model.get_cpds('t'))
print(model.get_cpds('a'))
print(model.get_cpds('h'))

Ausgabe


+------+-----+
| t(0) | 0.2 |
+------+-----+
| t(1) | 0.5 |
+------+-----+
| t(2) | 0.3 |
+------+-----+
+------+------+
| a(0) | 0.15 |
+------+------+
| a(1) | 0.4  |
+------+------+
| a(2) | 0.45 |
+------+------+
+------+------+--------------------+------+------+------+------+------+------+------+
| a    | a(0) | a(0)               | a(0) | a(1) | a(1) | a(1) | a(2) | a(2) | a(2) |
+------+------+--------------------+------+------+------+------+------+------+------+
| t    | t(0) | t(1)               | t(2) | t(0) | t(1) | t(2) | t(0) | t(1) | t(2) |
+------+------+--------------------+------+------+------+------+------+------+------+
| h(0) | 0.5  | 0.3333333333333333 | 0.5  | 1.0  | 0.0  | 0.0  | 1.0  | 0.6  | 0.0  |
+------+------+--------------------+------+------+------+------+------+------+------+
| h(1) | 0.5  | 0.6666666666666666 | 0.5  | 0.0  | 1.0  | 1.0  | 0.0  | 0.4  | 1.0  |
+------+------+--------------------+------+------+------+------+------+------+------+

Folgerung 1

python


from pgmpy.inference import VariableElimination
ve = VariableElimination(model)

#t=1,h=Was ist ein, wenn 1 gesetzt ist?
print(ve.map_query(variables=['a'], evidence={'t':1, 'h':1}))

Ausgabe


{'a': 1}

Folgerung 2

python


#t=0,1,Wenn es 2 ist, a,h Jeder geschätzte Wert? Was?
for i in [0,1,2]:
    print(ve.query(variables=['a', 'h'], evidence={'t':i}))

Ausgabe



+------+------+------------+
| a    | h    |   phi(a,h) |
+======+======+============+
| a(0) | h(0) |     0.0750 |
+------+------+------------+
| a(0) | h(1) |     0.0750 |
+------+------+------------+
| a(1) | h(0) |     0.4000 |
+------+------+------------+
| a(1) | h(1) |     0.0000 |
+------+------+------------+
| a(2) | h(0) |     0.4500 |
+------+------+------------+
| a(2) | h(1) |     0.0000 |
+------+------+------------+

+------+------+------------+
| h    | a    |   phi(h,a) |
+======+======+============+
| h(0) | a(0) |     0.0500 |
+------+------+------------+
| h(0) | a(1) |     0.0000 |
+------+------+------------+
| h(0) | a(2) |     0.2700 |
+------+------+------------+
| h(1) | a(0) |     0.1000 |
+------+------+------------+
| h(1) | a(1) |     0.4000 |
+------+------+------------+
| h(1) | a(2) |     0.1800 |
+------+------+------------+

+------+------+------------+
| a    | h    |   phi(a,h) |
+======+======+============+
| a(0) | h(0) |     0.0750 |
+------+------+------------+
| a(0) | h(1) |     0.0750 |
+------+------+------------+
| a(1) | h(0) |     0.0000 |
+------+------+------------+
| a(1) | h(1) |     0.4000 |
+------+------+------------+
| a(2) | h(0) |     0.0000 |
+------+------+------------+
| a(2) | h(1) |     0.4500 |
+------+------+------------+

Ergänzung

--model.fit (df) kann aufgeteilt werden, zum Beispiel: Beachten Sie, dass es möglicherweise einfacher zu handhaben ist, wenn es geteilt wird.

python


#CPD-Erstellungsteil
from pgmpy.estimators import BayesianEstimator
estimator = BayesianEstimator(model, df)
cpd_ta = estimator.estimate_cpd('t', prior_type='dirichlet', pseudo_counts=[[0],[0],[0]])
cpd_aa = estimator.estimate_cpd('a', prior_type='dirichlet', pseudo_counts=[[0],[0],[0]])
cpd_h = estimator.estimate_cpd('h', prior_type='dirichlet', pseudo_counts=[[0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0]])

#CPD-Eingangsteil
model.add_cpds(cpd_ta, cpd_aa, cpd_h) 

python


from pgmpy.factors.discrete import TabularCPD
cpd_h = TabularCPD(variable='h', variable_card=2,
                        values=[[1, 0.3, 0.5, 1, 0, 0, 1, 0.6, 0],
                                [0, 0.7, 0.5, 0, 1, 1, 0, 0.4, 1]],
                        evidence=['t', 'a'],
                        evidence_card=[3, 3])

――Es scheint, dass es auch eine Methode namens Dynamic Basin Network gibt, die das Zeitsystem berücksichtigt.

--NumPyro hat hierarchische Felder. Pyro auf PyTorch-Basis ist fast das gleiche. https://pyro.ai/numpyro/bayesian_hierarchical_linear_regression.html#2.-Modelling:-Bayesian-Hierarchical-Linear-Regression-with-Partial-Pooling https://qiita.com/takeajioka/items/ab299d75efa184eb1432

Recommended Posts

pgmpy: Versuch eines diskreten Beckennetzwerks bis zur Schlussfolgerung
Basian Network Package ~ Ausführung des Pebl-Tutorials ~