Functions from pyagrum
Useful functions in pyAgrum
Section titled “Useful functions in pyAgrum”pyagrum.getPosterior(model, , target, evs=None)
Section titled “pyagrum.getPosterior(model, , target, evs=None)”Compute the posterior of a single target (variable) in a BN given evidence
getPosterior uses a VariableElimination inference. If more than one target is needed with the same set of evidence or if the same target is needed with more than one set of evidence, this function is not relevant since it creates a new inference engine every time it is called.
- Parameters:
- bn (pyagrum.BayesNet or pyagrum.MarkovRandomField) – The probabilistic Graphical Model
- target (string or int) – variable name or id (forced keyword argument)
- evs (Dict [**name |**id:val , name |**id : List [ val1 , val2 ] , … ] . (**optional forced keyword argument )) – the (hard and soft) evidence
- Returns: posterior (pyagrum.Tensor or other)
pyagrum.mutilateBN(bn, intervention=None, observation=None)
Section titled “pyagrum.mutilateBN(bn, intervention=None, observation=None)”Modify the bayesian network bn to reflect the effect of interventions and/or observations on a set of variables. Due to the causal nature of interventions, we suppose the given bn to have a causal interpretation. Warning: experimental use of evidence definition
Interventions or observations can be HARD or SOFT.
Hard interventions or observations: : 1) [0,… 1, 0] -> sum(x) = 1 3) X : [n] -> with n a value
Soft interventions or observations: : 1. X : [empty list] -> equiprobability is assumed 2. X : [x1, … xn] -> sum(x) = 1 3. X : [1, … 1, 0] -> sum(x) >= 1 4. X : [n1, n2, n3] -> with n_i values that could happen
X is the name of a variable
- Parameters:
- bn (pyagrum.pyagrum.BayesNet) – A bayesian network
- intervention (Dict [**str ,**List [**str |**float |**int ] ]) – set of variables on which we intervene to force the value
- observation (Dict [**str ,**List [**str |**float |**int ] ]) – set of variables whose value is observed
- Returns:
- inter_bn (new bayesian network reflecting the interventions and observations (pyagrum.pyagrum.BayesNet))
- evidence (dictionary of all evidences for future inferences (dict))
Input/Output for Bayesian networks
Section titled “Input/Output for Bayesian networks”pyagrum.availableBNExts()
Section titled “pyagrum.availableBNExts()”Give the list of all formats known by pyAgrum to save a Bayesian network.
- Returns: a string which lists all suffixes for supported BN file formats.
pyagrum.loadBN(filename, listeners=None, verbose=False, **opts)
Section titled “pyagrum.loadBN(filename, listeners=None, verbose=False, **opts)”load a BN from a file with optional listeners and arguments
- Parameters:
- filename (str) – the name of the input file
- listeners (List [**object ]) – list of functions to execute when listening
- verbose (bool) – whether to print or not warning messages
- system (str) – (for O3PRM) name of the system to flatten in a BN
- classpath (List [**str ]) – (for O3PRM) list of folders containing classes
- Returns: a BN from a file using one of the availableBNExts() suffixes.
- Return type: pyagrum.BayesNet
Notes
Listeners could be added in order to monitor its loading.
pkl suffix is used to load a pickled BN. In this case, listeners and options are ignored.
Examples
>>> import pyagrum as gum>>>>>> # creating listeners>>> def foo_listener(progress):>>> if progress==200:>>> print(' BN loaded ')>>> return>>> elif progress==100:>>> car='%'>>> elif progress%10==0:>>> car='#'>>> else:>>> car='.'>>> print(car,end='',flush=True)>>>>>> def bar_listener(progress):>>> if progress==50:>>> print('50%')>>>>>> # loadBN with list of listeners>>> pyagrum.loadBN('./bn.bif',listeners=[foo_listener,bar_listener])>>> # .........#.........#.........#.........#..50%>>> # .......#.........#.........#.........#.........#.........% | bn loadedpyagrum.saveBN(bn, filename, allowModificationWhenSaving=None)
Section titled “pyagrum.saveBN(bn, filename, allowModificationWhenSaving=None)”save a BN into a file using the format corresponding to one of the availableWriteBNExts() suffixes.
- Parameters:
- bn (pyagrum.BayesNet) – the BN to save
- filename (str) – the name of the output file
- allowModificationWhenSaving (bool) – whether syntax errors in the BN should throw a FatalError or can be corrected. Also controlled by pyagrum.config[“BN”,”allow_modification_when_saving”].
Notes
pkl suffix is used to save a BN using pickle. In this case, options are ignored.
Input/Output for Markov random fields
Section titled “Input/Output for Markov random fields”pyagrum.availableMRFExts()
Section titled “pyagrum.availableMRFExts()”Give the list of all formats known by pyAgrum to save a Markov random field.
- Returns: a string which lists all suffixes for supported MRF file formats.
- Return type: str
pyagrum.loadMRF(filename, listeners=None, verbose=False)
Section titled “pyagrum.loadMRF(filename, listeners=None, verbose=False)”load a MRF from a file with optional listeners and arguments
- Parameters:
- filename (str) – the name of the input file
- listeners (List [**Object ]) – list of functions to execute
- verbose (bool) – whether to print or not warning messages
- Returns:
- pyagrum.MarkovRandomField – a MRF from a file using one of the availableMNExts() suffixes.
- Listeners could be added in order to monitor its loading.
- pkl suffix is used to save a BN using pickle. In this case, options are ignored.
Examples
>>> import pyagrum as gum>>>>>> # creating listeners>>> def foo_listener(progress):>>> if progress==200:>>> print(' BN loaded ')>>> return>>> elif progress==100:>>> car='%'>>> elif progress%10==0:>>> car='#'>>> else:>>> car='.'>>> print(car,end='',flush=True)>>>>>> def bar_listener(progress):>>> if progress==50:>>> print('50%')>>>>>> # loadBN with list of listeners>>> pyagrum.loadMRF('./bn.uai',listeners=[foo_listener,bar_listener])>>> # .........#.........#.........#.........#..50%>>> # .......#.........#.........#.........#.........#.........% | bn loadedpyagrum.saveMRF(mn, filename)
Section titled “pyagrum.saveMRF(mn, filename)”save a MRF into a file using the format corresponding to one of the availableWriteMNExts() suffixes.
- Parameters:
- mn (pyagrum.MarkovRandomField )) – the MRF to save
- filename (str) – the name of the output file
Input for influence diagram
Section titled “Input for influence diagram”pyagrum.availableIDExts()
Section titled “pyagrum.availableIDExts()”Give the list of all formats known by pyAgrum to save a influence diagram.
- Returns: a string which lists all suffixes for supported ID file formats.
- Return type: str
pyagrum.loadID(filename)
Section titled “pyagrum.loadID(filename)”read a pyagrum.InfluenceDiagram from a ID file
- Parameters: filename (str) – the name of the input file
- Returns: the InfluenceDiagram
- Return type: pyagrum.InfluenceDiagram
pyagrum.saveID(infdiag, filename)
Section titled “pyagrum.saveID(infdiag, filename)”save an ID into a file using the format corresponding to one of the availableWriteIDExts() suffixes.
- Parameters:
- infdiag (pyagrum.InfluenceDiagram) – the Influence Diagram to save
- filename (str) – the name of the output file