Book Image

Building Probabilistic Graphical Models with Python

By : Kiran R Karkera
Book Image

Building Probabilistic Graphical Models with Python

By: Kiran R Karkera

Overview of this book

<p>With the increasing prominence in machine learning and data science applications, probabilistic graphical models are a new tool that machine learning users can use to discover and analyze structures in complex problems. The variety of tools and algorithms under the PGM framework extend to many domains such as natural language processing, speech processing, image processing, and disease diagnosis.</p> <p>You've probably heard of graphical models before, and you're keen to try out new landscapes in the machine learning area. This book gives you enough background information to get started on graphical models, while keeping the math to a minimum.</p>
Table of Contents (15 chapters)
Building Probabilistic Graphical Models with Python
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Bayesian parameter learning example using MLE


In the job_interview_libpgm.ipynb IPython Notebook, we will use the libpgm implementation of maximum likelihood estimates to learn the parameters of the CPDs in the job interview network.

Here is the code from libpgm that loads the CPDs, as shown before:

from libpgm.graphskeleton import GraphSkeleton
from libpgm.nodedata import NodeData
from libpgm.discretebayesiannetwork import DiscreteBayesianNetwork
from libpgm.tablecpdfactor import TableCPDFactor
from libpgm.pgmlearner import PGMLearner
import pandas as pd
nd = NodeData()
skel = GraphSkeleton()
jsonpath="job_interview.txt"
nd.load(jsonpath)
skel.load(jsonpath)
skel.toporder()

We can create the Bayesian network and get random samples using the following code:

bn = DiscreteBayesianNetwork(skel, nd)
samples=bn.randomsample(2000)

In the following code, we instantiate the PGMLearner class. The discrete_mle_estimateparams method already knows the structure of the network. As discussed in the earlier...