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)
nodesis 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)
nodeis the name of the node to be added.parentsis a list of strings, with the names of the parents of the node.probabilitiesis 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
queryis a string with the query to be made. The format isnode|evidence, wherenodeis the name of the node to be queried, andevidenceis a list of strings with the evidence of the query. The format of the evidence isnode=value, wherenodeis the name of the node, andvalueis 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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file memebayes-0.2.1.tar.gz.
File metadata
- Download URL: memebayes-0.2.1.tar.gz
- Upload date:
- Size: 2.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
04e8956616d6d1880140ecde8012f5c233f0a81f2a44bd271c7db98793ea64b0
|
|
| MD5 |
fe8cfea48cab44e69e747e197007acee
|
|
| BLAKE2b-256 |
ce0c15f3db6ca41df7eabb4fb9ace884f1fa459dd694edd4ad3b0429558f25d0
|
File details
Details for the file memebayes-0.2.1-py3-none-any.whl.
File metadata
- Download URL: memebayes-0.2.1-py3-none-any.whl
- Upload date:
- Size: 2.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7af0c06e02fa8e762ff7dcdfed7b696935094c717065efe1fe1df022783b5321
|
|
| MD5 |
f055c245a512c8855e47eea01fad79f3
|
|
| BLAKE2b-256 |
b84b94ed5ca88e6829e17be6ab4ecaa77fb95d0f7571ec0943fb7706f8e48f01
|