Uncertainty - The Bayesian Network
Bayesian Networks are a type of Probabilistic Graphical Model that can be used to build models from data and/or expert opinion. Bayesian networks are probabilistic because they are built from probability distributions and also use the laws of probability for prediction and anomaly detection, for reasoning and diagnostics decision making under uncertainty and times series prediction.
They can be used for a wide range of tasks including prediction, anomaly detection, diagnostics, automated insight, reasoning, time series prediction and decision making under uncertainty.
Real world applications are probabilistic in nature, and to represent the relationship between multiple events, we need a Bayesian network.
In Bayesian network, we deal with a number of interrelated (random) variables. We explore how the joint distribution of the variables can be described by exploiting what we know about their natural interrelationships via conditional distributions. We use graph theory to explain their interrelationship. If data are available on the random variables, we fit a Bayesian network model which describes their relationship in a succinct way. Bayesian networks are a marriage between probability theory and graphs. One of the main goals in Bayesian networks is prediction.
In this blog we will train the Bayesian Network in Python. My self Happy khatun. I am a student of city university. And this blog is a part of our AI (Artificial Intillgecne) Lab Course conducted by our most honorable teacher Nuruzzaman Faruqui sir.
'''
pomegranate is a python package that implements fast, efficient, and extremely flexible probabilistic models
ranging from probability distributions to Bayesian networks to mixtures of hidden Markov models.
'''
from pomegranate import *
from pomegranate.base import Node
# Rain has no parent Node. So it's of DiscreteDistribution
rain = Node(DiscreteDistribution(
{
'none': 0.7,
'light': 0.2,
'heavy': 0.1
}
), name='rain')
# Maintenance Node is conditional on rain
maintanence = Node(ConditionalProbabilityTable(
[
['none', 'yes', 0.4],
['none', 'no', 0.6],
['light', 'yes', 0.2],
['light', 'no', 0.8],
['heavy', 'yes', 0.1],
['heavy', 'no', 0.9],
], [rain.distribution]), name='maintanence')
# train Node is conditional on rain,maintanence
train = Node(ConditionalProbabilityTable(
[
['none', 'yes', 'ontime', 0.8],
['none', 'yes', 'delayed', 0.2],
['none', 'no', 'ontime', 0.9],
['none', 'no', 'delayed', 0.1],
['light', 'yes', 'ontime', 0.6],
['light', 'yes', 'delayed', 0.4],
['light', 'no', 'ontime', 0.7],
['light', 'no', 'delayed', 0.3],
['heavy', 'yes', 'ontime', 0.4],
['heavy', 'yes', 'delayed', 0.6],
['heavy', 'no', 'ontime', 0.5],
['heavy', 'no', 'delayed', 0.5],
], [rain.distribution, maintanence.distribution]),
name='train')
# Appointment Node is conditional on train
appointment = Node(ConditionalProbabilityTable(
[
['ontime', 'attend', 0.9],
['ontime', 'miss', 0.1],
['delayed', 'attend', 0.6],
['delayed', 'miss', 0.4],
], [train.distribution]), name='appointment')
# create a Bayesian Network to add states or Nodes
model = BayesianNetwork()
# Add Nodes
model.add_states(rain, maintanence, train, appointment)
# Add edges connecting Nodes
model.add_edge(rain, maintanence)
model.add_edge(rain, train)
model.add_edge(maintanence, train)
model.add_edge(train, appointment)
# Finalize the model
model.bake()
Now we have to implement another following code:
from model import model
# calculate predictions based on the evidence that the train was delayed
predictions = model.predict_proba(
{
"train": "delayed"
}
)
# print predictions for each node
for node, prediction in zip(model.states, predictions):
if isinstance(prediction, str):
print(f"{node.name} : {prediction} ")
else:
print(f" {node.name} ")
for value, probability in prediction.parameters[0].items():
print(f" {value}: {probability: .4f} ")
After running this code we get following result:
Bayesian Networks are very convenient for representing systems of probabilistic causal relationships.
A Bayesian Network, provides a way of defining a probabilistic model for a complex problem by stating all of the conditional independence assumptions for the known variables, whilst allowing the presence of unknown (latent) variables.
Here we describe Bayesian Networking in a easiest way .This is the easiest source to learn
python programming.








