BNLearner.learnParameters

BNLearner.learnParameters(*args)

Creates a Bayes net whose structure corresponds to that passed in argument or to the last structure learnt by Method learnDAG(), and whose parameters are learnt from the BNLearner’s database.

Usage:
  1. learnParameters(dag, take_into_account_score=True)

  2. learnParameters(bn, take_into_account_score=True)

  3. learnParameters(take_into_account_score=True)

When the first argument of Method learnParameters() is a DAG or a Bayes net (usages 1. and 2.), this one specifies the graphical structure of the returned Bayes net. Otherwise (usage 3.), Method learnParameters() is called implicitly with the last DAG learnt by the BNLearner.

The difference between calling this method with a DAG (usages 1. and 3.) or a Bayes net (usage 2.) arises when the database contains missing values and EM is used to learn the parameters. EM needs to initialize the conditional probability distributions (CPT) before iterating the expectation/maximimzation steps. When a DAG is passed in argument, these initializations are performed using a specific estimator that does not take into account the missing values in the database. The resulting CPTs are then perturbed randomly (see the noise in method useEM()). When a Bayes net is passed in argument, its CPT for a node A can be either filled exclusively with zeroes or not. In the first case, the initialization is performed as described above. In the second case, the value of A’s CPT is used as is, and a subsequent perturbation controlled by the noise level is applied.

Parameters:
  • dag (pyagrum.DAG) – specifies the graphical structure of the returned Bayes net.

  • bn (pyagrum.BayesNet) – specifies the graphical structure of the returned Bayes net and, when the database contains missing values and EM is used for learning, force EM to initialize the CPTs of the resulting Bayes net to the values of those passed in argument (when they are not fully filled with zeroes) before iterating over the expectation/maximization steps.

  • take_into_account_score (bool, default True) – The graphical structure 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 (take_into_account_score=True), we will learn parameters by taking into account the prior specified by methods usePriorXXX() + the implicit prior of the score (if any), but only if the learner has already used its score, i.e. after a structure learning (e.g. pyagrum.BNLearner.learnBN) or a call to pyagrum.BNLearner.score; on a learner that has not, only the prior specified by usePriorXXX() is used. If take_into_account_score=False, we just take into account the prior specified by usePriorXXX().

Returns:

the learnt BayesNet

Return type:

pyagrum.BayesNet

Raises:
  • pyagrum.MissingVariableInDatabase – If a variable of the Bayes net is not found in the database

  • pyagrum.MissingValueInDatabase – If the database contains some missing values and EM is not used for the learning

  • pyagrum.OperationNotAllowed – If EM is used but no stopping criterion has been selected

  • pyagrum.SizeError – If a CPT would have more than pyagrum.config['learning', 'maxCPTSize'] parameters (read when the learner is created).

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

Warning

When using a pyagrum.DAG as input parameter, the NodeIds in the dag and index of rows in the database must fit in order to coherently fix the structure of the BN. Generally, it is safer to use a pyagrum.BayesNet as input or even to use pyagrum.BNLearner.fitParameters.

Example

import pyagrum as gum

with open("data.csv", "w") as f:  # C is mostly "A or B"
  f.write("A,B,C\n" + "0,0,0\n0,1,1\n1,0,1\n1,1,1\n" * 40 + "0,0,1\n0,1,0\n1,0,0\n1,1,0\n" * 5)
learner = gum.BNLearner("data.csv")
dag = gum.fastDAG("A;B;C;A->C<-B")  # the ids of the nodes are the ids of the columns
bn = learner.learnParameters(dag)
round(bn.cpt("C")[{"A": 0, "B": 0, "C": 0}], 4)
# 0.8889

Class

BNLearner