Git Product home page Git Product logo

pbnt's Introduction

Python Bayesian Network Toolbox (PBNT)
Bayes Network Model for Python 2.7

PBNT is a bayesian network model for python that was created by Elliot Cohen in 2005. This version updates his version that was built for Python 2.4 and adds support for modern python libraries. Most namely, it removes the reference to numArray and replaces it with numPy.

With this library it is possible to input a Bayesian Network with probabilities/conditional probabilities on each node to calculate the marginal and conditional probabilities of queries on the network.

The original version of the project can be found here

PBNT Usage

You must first have the NumPy package installed.

###The included example network: The example files give a simple example of how a Bayes Network can be implemented. It uses a Bayes Network created from 4 nodes, Cloudy, Rainy, Sprinkler, and WetGrass.

Here is a layout of what the network looks like Inline-style: alt text

To run the example files navigate to the examples directory and run:

$ python exampleinference.py

if everything is working properly, you should get:

The marginal probability of sprinkler=false: 0.801029
The marginal probability of wetgrass=false | cloudy=False, rain=True: 0.055

Creating a Model:

BayesNode(id,numValues,name)

  • id: Integer Identification of the node
  • numValues: Number of associated values with the node
  • name: name for the node in form name="name_of_node"

Example:

cNode = BayesNode(0, 2, name="cloudy")

Declare Parent and Child Nodes to nodes:

cNode.add_child(sNode)
cNode.add_parent(cNode)

Assign Nodes to an array

nodes = [cNode, sNode, rNode, wNode]

set the distributions

An example to set distribution of probability that is not conditional:

#cloudy distribution
cDistribution = DiscreteDistribution(cNode)
index = cDistribution.generate_index([],[])
cDistribution[index] = 0.5
cNode.set_dist(cDistribution)

Note: The distribution index is 0.5, which sets the probability for both true and false automatically since (1 - 0.5) is 0.5.

An example for conditional probability

#sprinkler
dist = zeros([cNode.size(),sNode.size()], dtype=float32)
dist[0,] = 0.5
dist[1,] = [0.9,0.1]
sDistribution = ConditionalDiscreteDistribution(nodes=[cNode, sNode], table=dist)
sNode.set_dist(sDistribution)

Note: Dist[1,] = [0.9,0.1] is read that given cNode (cloudy) == True, the probability of Sprinkler being False is 0.9 and true is 0.1.

Once the distributions are created, assign the node array to a Bayes Network

bnet = BayesNet(nodes)

Building an Inference:

Based on the index of the nodes in your model, assign your nodes to variables:

for node in water.nodes:
      if node.id == 0:
          cloudy = node
      if node.id == 1:
          sprinkler = node
      if node.id == 2:
          rain = node
      if node.id == 3:
          wetgrass = node

Then create the inference engine:

engine = JunctionTreeEngine(bnet)

Set evidence if needed:

engine.evidence[cloudy] = False
engine.evidence[rain] = True

Generate a Query and calculate the marginal probability given the evidence

Q = engine.marginal(wetgrass)[0]
index = Q.generate_index([False],range(Q.nDims))

Note: The first line sets the variable we are examining, in this case wetgrass. The generate index function specifies the result we are looking for, in this instance, we are looking to see if wetgrass is false.

Print out the probability found

print Q[index]

Project information

Modified from:
Python Bayes Network Toolbox. Copyright (c) 2005, Elliot Cohen. All rights reserved.

Updated to work with Python 2.7 by Brandon Mikulka

pbnt's People

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.