NOTE: I wrote this code to go along with Daphne Koller's book and no longer maintain the repository, although the code should be easily adaptable. If you have any questions, please email me at ncullen at seas dot upenn dot edu.
Bayesian Networks in Python
This module provides a convenient and intuitive interface for reading, writing, plotting, performing inference, parameter learning, structure learning, and classification over Discrete Bayesian Networks - along with some other utility functions. There seems to be a lack of many high-quality options for BNs in Python, so I hope this project will be a useful addition. Overview
I am a graduate student in the Di2Ag laboratory at Dartmouth College, and would love to collaborate on this project with anyone who has an interest in graphical models - Send me an email at [email protected]. If you're a researcher or student and want to use this module, I am happy to give an overview of the code/functionality or answer any questions.
For an up-to-date list of issues, go to the "issues" tab in this repository. Below is an updated list of features, along with information on usage/examples:
Current features
- Exact Marginal Inference - Sum-Product Variable Elimination - Clique Tree Message Passing - Approximate Marginal Inference - Forward Sampling - Likelihood Weighted Sampling - Gibbs (MCMC) Sampling - Loopy Belief Propagation Marginal Inference
- Exact MAP Inference - Max-Product Variable Elimination - Integer Linear Programming - Approximate MAP Inference - LP Relaxation MAP/MPE Inference
- Algorithms - PC - Grow-Shrink - IAMB/Lambda-IAMB/Fast-IAMB - Independence Tests - Marginal Mutual Information - Conditional Mutual Information - Pearson Chi-Square Constraint-Based Structure Learning
- Algorithms - Greedy Hill Climbing - Tabu Search - Random Restarts - Scoring Functions - BIC/AIC/MDL - BDe/BDeu/K2 Score-Based Structure Learning
- Naive Bayes - Tree-Augmented Naive Bayes - Chow-Liu Tree-Based Structure Learning
- MMPC - MMHC Hybrid Structure Leanring
- GOBNILP Solver Exact Structure Learning
- Maximum Likelihood Estimation - Dirichlet-Multinomial Estimation Parameter Learning
- Naive Bayes - Tree-Augmented Naive Bayes - General DAG Classification
- Empty/Tree/Polytree/Forest - General DAG Multi-Dimensional Classification
- Structure-Based Distance Metrics - Missing Edges - Extra Edges - Incorrect Edge Orientation - Hamming Distance - Parameter-Based Distance Metrics - KL-Divergence and JS-Divergence - Manhattan and Euclidean - Hellinger - Minkowski Comparing Two Bayesian Networks
- Determine Class Equivalence - Discretize continuous data - Orient a PDAG - Generate random sample dataset from a BN - Markov Blanket operations Utility Functionality
I previously wrote a Python wrapper for the GOBNILP project - a state-of-the-art integer programming solver for Bayesian network structure learning that can find the EXACT Global Maximum of any score-based objective function. It also links to CPLEX for incredible speed. The wrappers can be found in the "pyGOBN" project at www.github.com/ncullen93/pyGOBN. For an overview of GOBNILP or to see its great benchmarks on even the most massive datasets, visit https://www.cs.york.ac.uk/aig/sw/gobnilp/.
This package includes a number of examples to help users get acquainted with the intuitive syntax and functionality of pyBN. For an updated list of examples, check out the collection of ipython notebooks in the "examples" folder located in the master directory. Examples
Here is a list of current examples:
- ReadWrite : an introduction to reading (writing) BayesNet object from (to) files, along with an overview of the attributes and data structures inherit to BayesNet objects.
- Drawing : an introduction to the drawing/plotting capabilities of pyBN with both small and large Bayesian networks.
- FactorOperations : an introduction to the Factor class, an exploration of the numerous attributes belonging to a Factor in pyBN, an overview of every Factor operation function at the users' hands, and a short discussion of what makes Factor operations so fast and efficient in pyBN.
Getting up-and-running with this package is simple: Usage
- Click "Download ZIP" button towards the upper right corner of the page.
- Unpack the ZIP file wherever you want on your local machine. You should now have a folder called "pyBN-master"
- In your python terminal, change directories to be IN pyBN-master. Typing "ls" should show you "data", "examples" and "pyBN" folders. Stay in the "pyBN-master" directory for now!
- In your python terminal, simply type "from pyBN import ". This will load all of the module's functions, classes, etc.
- You are now free to use the package! Perhaps you want to start by creating a BayesNet object using "bn = BayesNet()" and so on.