A bayesian network implementation
Project description
Bayesian Network
Public library for a Bayesian Network in python. This library uses only re
as dependency. It works as a standalone library and can be used in any python project.
It is defined as a class BayesNetwork
which has the following methods:
add_node
: Adds a node to the networkedit_node
: Edits a node in the networkcompact
: Shows a compact representation of the networkdefined
: Checks if every node is definedquery
: Queries the network for a given node
Installation
pip install memebayes
Usage
Importing the library
from memebayes import BayesNetwork
Creating a network
network = BayesNetwork(nodes:list)
nodes
is a list of strings, with the names of the nodes in the network.
Adding a node
network.add_node(node:str, parents:list, probabilities:dict)
node
is the name of the node to be added.parents
is a list of strings, with the names of the parents of the node.probabilities
is a dictionary with the probabilities of the node. The keys are the possible values of the node, and the values are the probabilities of the node given the parents. If the node has no parents, the keys are the possible values of the node, and the values are the probabilities of the node.
Editing a node
network.edit_node(node:str, parents:list, probabilities:dict)
Compacting the network
network.compact() # Returns a string Ej.P(C)P(B|C)P(A|B,C)
Checking if the network is defined
network.defined() # Returns a boolean
Querying the network
network.query(query:str) # Returns the probabilities of the node given the evidence
query
is a string with the query to be made. The format isnode|evidence
, wherenode
is the name of the node to be queried, andevidence
is a list of strings with the evidence of the query. The format of the evidence isnode=value
, wherenode
is the name of the node, andvalue
is the value of the node. If there is no evidence, the format isnode
.
Examples
from memebayes import BayesNetwork
net = BayesNetwork(["B","E","A","J","M"]) # Create a network with the nodes B, E, A, J and M
net.edit_node("B",pt={"B":0.001}) # Edit the node B with the probability of being true
net.edit_node("E",pt={"E":0.002}) # Edit the node E with the probability of being true
net.edit_node("A",parents=["B","E"], pt = {"A|BE":0.95,"A|B-E":0.94,"A|-BE":0.29,"A|-B-E":0.001}) # Edit the node A with all the possible probabilities of being true given the parents B and E
net.edit_node("J", parents = ["A"], pt = {"J|A": 0.9, "J|-A": 0.5}) # Edit the node J with the probability of being true given that A is true
net.edit_node("M", parents=["A"], pt={"M|A": 0.7, "M|-A": 0.01}) # Edit the node M with the probability of being true given that A is true
print(net.compact()) # Print the compact representation of the network (Ej.P(B)P(E)P(A|B,E)P(J|A)P(M|A))
print(net.defined()) # Check if the network is defined (True)
print(net.query("B")) # Query the network for the node B (0.001)
print(net.query("B|JM")) # Query the network for the node B given that J and M are true (0.0007)
References
This library is based on the following references:
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
memebayes-0.2.1.tar.gz
(2.5 kB
view hashes)
Built Distribution
Close
Hashes for memebayes-0.2.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7af0c06e02fa8e762ff7dcdfed7b696935094c717065efe1fe1df022783b5321 |
|
MD5 | f055c245a512c8855e47eea01fad79f3 |
|
BLAKE2b-256 | b84b94ed5ca88e6829e17be6ab4ecaa77fb95d0f7571ec0943fb7706f8e48f01 |