Functions from pyAgrum¶

Useful functions in pyAgrum

pyAgrum.about()

pyAgrum.fastBN(structure, domain_size=2)
Create a Bayesian network with a dot-like syntax which specifies:
• the structure ‘a->b->c;b->d<-e;’.
• the type of the variables with different syntax:
• by default, a variable is a gum.RangeVariable using the default domain size (second argument)
• with ‘a[10]’, the variable is a gum.RangeVariable using 10 as domain size (from 0 to 9)
• with ‘a[3,7]’, the variable is a gum.RangeVariable using a domainSize from 3 to 7
• with ‘a[1,3.14,5,6.2]’, the variable is a gum.DiscretizedVariable using the given ticks (at least 3 values)
• with ‘a{top|middle|bottom}’, the variable is a gum.LabelizedVariable using the given labels.

Note

• If the dot-like string contains such a specification more than once for a variable, the first specification will be used.
• the CPTs are randomly generated.

Examples

>>> import pyAgrum as gum
>>> bn=gum.fastBN('A->B[1,3]<-C{yes|No}->D[2,4]<-E[1,2.5,3.9]',6)

Parameters: structure (str) – the string containing the specification domainSize (int) – the default domain size for variables the resulting bayesian network pyAgrum.BayesNet
pyAgrum.getPosterior(model, evs, target)

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.MarkovNet) – The probabilistic Graphical Model evs (dictionaryDict) – {name/id:val, name/id : [ val1, val2 ], …} target (string or int) – variable name or id posterior (pyAgrum.Potential)

Input/Output for bayesian networks¶

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)

load a file with optional listeners and arguments

Parameters: filename – the name of the input file listeners – list of functions to execute verbose – whether to print or not warning messages system – (for O3PRM) name of the system to flatten in a BN classpath – (for O3PRM) list of folders containing classes a BN from a file using one of the availableBNExts() suffixes.

Examples

>>> import pyAgrum as gum
>>>
>>> # creating listeners
>>> def foo_listener(progress):
>>>    if progress==200:
>>>        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
>>> # .........#.........#.........#.........#..50%
>>> # .......#.........#.........#.........#.........#.........% | bn loaded

pyAgrum.saveBN(bn, filename)

save a BN into a file using the format corresponding to one of the availableWriteBNExts() suffixes.

Parma bn(gum.BayesNet):
the BN to save
Parameters:filename(str) – the name of the output file

Input for influence diagram¶

pyAgrum.loadID(filename)

read a gum.InfluenceDiagram from a bifxml file

Parameters: filename – the name of the input file an InfluenceDiagram