Learning

pyAgrum encloses all the learning processes for Bayesian network in a simple class BNLearner. This class gives access directly to the complete learning algorithm and theirs parameters (such as prior, scores, constraints, etc.) but also proposes low-level functions that eases the work on developping new learning algorithms (for instance, compute chi2 or conditioanl likelihood on the database, etc.).

class pyAgrum.BNLearner(filename, inducedTypes=True)
Parameters:
  • source (str or pandas.DataFrame) – the data to learn from

  • missingSymbols (List[str]) – list of string that will be interpreted as missing values (by default : [‘?’])

  • inducedTypes (Bool) – whether BNLearner should try to automatically find the type of each variable

BNLearner(filename,src) -> BNLearner
Parameters:
  • source (str or *pandas.DataFrame) – the data to learn from

  • src (pyAgrum.BayesNet) – the Bayesian network used to find those modalities

  • missingSymbols (List[str]) – list of string that will be interpreted as missing values (by default : [‘?’])

BNLearner(learner) -> BNLearner
Parameters:
  • learner (pyAgrum.BNLearner) – the BNLearner to copy

G2(*args)

G2 computes the G2 statistic and pvalue for two columns, given a list of other columns.

Parameters
  • name1 (str) – the name of the first column

  • name2 (str) – the name of the second column

  • knowing ([str]) – the list of names of conditioning columns

Returns

the G2 statistic and the associated p-value as a Tuple

Return type

statistic,pvalue

addForbiddenArc(*args)
Return type

BNLearner

addMandatoryArc(*args)
Return type

BNLearner

addPossibleEdge(*args)
Return type

BNLearner

chi2(*args)

chi2 computes the chi2 statistic and pvalue for two columns, given a list of other columns.

Parameters
  • name1 (str) – the name of the first column

  • name2 (str) – the name of the second column

  • knowing ([str]) – the list of names of conditioning columns

Returns

the chi2 statistic and the associated p-value as a Tuple

Return type

statistic,pvalue

currentTime()
Returns

get the current running time in second (float)

Return type

float

databaseWeight()
Return type

float

domainSize(*args)
Return type

int

epsilon()
Returns

the value of epsilon

Return type

float

eraseForbiddenArc(*args)
Return type

BNLearner

eraseMandatoryArc(*args)
Return type

BNLearner

erasePossibleEdge(*args)
Return type

BNLearner

fitParameters(bn)

Easy shortcut to LearnParameters method. fitParameters uses self to direcuptly populate the CPTs of bn.0

Parameters

bn (pyAgrum.BayesNet) – a BN which will directly have its parameters learned.

getNumberOfThreads()
Return type

int

hasMissingValues()
Return type

bool

history()
Returns

the scheme history

Return type

tuple

Raises

pyAgrum.OperationNotAllowed – If the scheme did not performed or if verbosity is set to false

idFromName(var_name)
Parameters

var_name (str) –

Return type

int

isGumNumberOfThreadsOverriden()
Return type

bool

latentVariables()

Warning

learner must be using 3off2 or MIIC algorithm

Returns

the list of latent variables

Return type

list

learnBN()

learn a BayesNet from a file (must have read the db before)

Returns

the learned BayesNet

Return type

pyAgrum.BayesNet

learnDAG()
Return type

DAG

learnEssentialGraph()
learnMixedStructure()
Return type

MixedGraph

learnParameters(*args)

learns a BN (its parameters) when its structure is known.

Parameters
  • dag (pyAgrum.DAG) –

  • bn (pyAgrum.BayesNet) –

  • take_into_account_score (bool) – The dag passed in argument may have been learnt from a structure learning. In this case, if the score used to learn the structure has an implicit prior (like K2 which has a 1-smoothing prior), it is important to also take into account this implicit prior for parameter learning. By default, if a score exists, we will learn parameters by taking into account the prior specified by methods usePriorXXX () + the implicit prior of the score, else we just take into account the prior specified by usePriorXXX ()

Returns

the learned BayesNet

Return type

pyAgrum.BayesNet

Raises
  • pyAgrum.MissingVariableInDatabase – If a variable of the BN is not found in the database

  • pyAgrum.UnknownLabelInDatabase – If a label is found in the database that do not correspond to the variable

logLikelihood(*args)
Return type

float

maxIter()
Returns

the criterion on number of iterations

Return type

int

maxTime()
Returns

the timeout(in seconds)

Return type

float

messageApproximationScheme()
Returns

the approximation scheme message

Return type

str

minEpsilonRate()
Returns

the value of the minimal epsilon rate

Return type

float

nameFromId(id)
Parameters

id (int) –

Return type

str

names()
Return type

List[str]

nbCols()
Return type

int

nbRows()
Return type

int

nbrIterations()
Returns

the number of iterations

Return type

int

periodSize()
Returns

the number of samples between 2 stopping

Return type

int

Raises

pyAgrum.OutOfBounds – If p<1

pseudoCount(vars)

access to pseudo-count (priors taken into account)

Parameters

vars (list[str]) – a list of name of vars to add in the pseudo_count

Return type

a Potential containing this pseudo-counts

rawPseudoCount(*args)
Return type

List[float]

recordWeight(i)
Parameters

i (int) –

Return type

float

setDatabaseWeight(new_weight)
Parameters

new_weight (float) –

Return type

None

setEpsilon(eps)
Parameters

eps (float) – the epsilon we want to use

Raises

pyAgrum.OutOfBounds – If eps<0

Return type

None

setForbiddenArcs(set)
Parameters

set (Set[Tuple[int, int]]) –

Return type

BNLearner

setInitialDAG(dag)
Parameters

dag (pyAgrum.DAG) – an initial DAG structure

Return type

BNLearner

setMandatoryArcs(set)
Parameters

set (Set[Tuple[int, int]]) –

Return type

BNLearner

setMaxIndegree(max_indegree)
Parameters

max_indegree (int) – the limit number of parents

Return type

BNLearner

setMaxIter(max)
Parameters

max (int) – the maximum number of iteration

Raises

pyAgrum.OutOfBounds – If max <= 1

Return type

None

setMaxTime(timeout)
Parameters
  • tiemout (float) – stopping criterion on timeout (in seconds)

  • timeout (float) –

Raises

pyAgrum.OutOfBounds – If timeout<=0.0

Return type

None

setMinEpsilonRate(rate)
Parameters

rate (float) – the minimal epsilon rate

Return type

None

setNumberOfThreads(nb)
Parameters

nb (int) –

Return type

None

setPeriodSize(p)
Parameters

p (int) – number of samples between 2 stopping

Raises

pyAgrum.OutOfBounds – If p<1

Return type

None

setPossibleEdges(set)
Parameters

set (Set[Tuple[int, int]]) –

Return type

BNLearner

setPossibleSkeleton(skeleton)
Parameters

skeleton (UndiGraph) –

Return type

BNLearner

setRecordWeight(i, weight)
Parameters
  • i (int) –

  • weight (float) –

Return type

None

setSliceOrder(*args)
Return type

BNLearner

setVerbosity(v)
Parameters

v (bool) – verbosity

Return type

None

state()
Return type

object

use3off2()

Indicate that we wish to use 3off2.

Return type

BNLearner

useAprioriBDeu()

Deprecated methods in BNLearner for pyAgrum>1.1.1

useAprioriDirichlet()

Deprecated methods in BNLearner for pyAgrum>1.1.1

useAprioriSmoothing()

Deprecated methods in BNLearner for pyAgrum>1.1.1

useBDeuPrior(weight=1.0)
Parameters

weight (float) –

Return type

BNLearner

useDirichletPrior(*args)

Use the Dirichlet prior.

Parameters
  • source (str|pyAgrum.BayesNet) – the Dirichlet related source (filename of a database or a Bayesian network)

  • weight (float (optional)) – the weight of the prior (the ‘size’ of the corresponding ‘virtual database’)

Return type

BNLearner

useEM(epsilon)

Indicates if we use EM for parameter learning.

Parameters

epsilon (float) – if epsilon=0.0 then EM is not used if epsilon>0 then EM is used and stops when the sum of the cumulative squared error on parameters is les than epsilon.

Return type

BNLearner

useGreedyHillClimbing()

Indicate that we wish to use a greedy hill climbing algorithm.

Return type

BNLearner

useK2(*args)
Return type

BNLearner

useLocalSearchWithTabuList(tabu_size=100, nb_decrease=2)
Parameters
  • tabu_size (int) –

  • nb_decrease (int) –

Return type

BNLearner

useMDLCorrection()

Indicate that we wish to use the MDL correction for 3off2 or MIIC

Return type

BNLearner

useMIIC()

Indicate that we wish to use MIIC.

Return type

BNLearner

useNMLCorrection()

Indicate that we wish to use the NML correction for 3off2 or MIIC

Return type

BNLearner

useNoApriori()

Deprecated methods in BNLearner for pyAgrum>1.1.1

useNoCorrection()

Indicate that we wish to use the NoCorr correction for 3off2 or MIIC

Return type

BNLearner

useNoPrior()

Use no prior.

Return type

BNLearner

useScoreAIC()

Indicate that we wish to use an AIC score.

Return type

BNLearner

useScoreBD()

Indicate that we wish to use a BD score.

Return type

BNLearner

useScoreBDeu()

Indicate that we wish to use a BDeu score.

Return type

BNLearner

useScoreBIC()

Indicate that we wish to use a BIC score.

Return type

BNLearner

useScoreK2()

Indicate that we wish to use a K2 score.

Return type

BNLearner

useScoreLog2Likelihood()

Indicate that we wish to use a Log2Likelihood score.

Return type

BNLearner

useSmoothingPrior(weight=1)

Use the prior smoothing.

Parameters

weight (float) – pass in argument a weight if you wish to assign a weight to the smoothing, otherwise the current weight of the learner will be used.

Return type

BNLearner

verbosity()
Returns

True if the verbosity is enabled

Return type

bool