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) BNLearner ¶
- Parameters:
filename (str) – the file to learn from
inducedTypes (Bool) – whether BNLearner should try to automatically find the type of each variable
- BNLearner(filename,src) -> BNLearner
- Parameters:
filename (str) – the file to learn from
src (pyAgrum.BayesNet) – the Bayesian network used to find those modalities
- 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)¶
The arc in parameters won’t be added.
- Parameters
arc (pyAgrum.Arc) – an arc
head – a variable’s id (int)
tail – a variable’s id (int)
head – a variable’s name (str)
tail – a variable’s name (str)
- Return type
None
- addMandatoryArc(*args)¶
Allow to add prior structural knowledge.
- Parameters
arc (pyAgrum.Arc) – an arc
head – a variable’s id (int)
tail – a variable’s id (int)
head – a variable’s name (str)
tail – a variable’s name (str)
- Raises
pyAgrum.InvalidDirectedCycle – If the added arc creates a directed cycle in the DAG
- Return type
None
- addPossibleEdge(*args)¶
- Return type
None
- 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)¶
Allow the arc to be added if necessary.
- Parameters
arc (pyAgrum) – an arc
head – a variable’s id (int)
tail – a variable’s id (int)
head – a variable’s name (str)
tail – a variable’s name (str)
- Return type
None
- eraseMandatoryArc(*args)¶
- Parameters
arc (pyAgrum) – an arc
head – a variable’s id (int)
tail – a variable’s id (int)
head – a variable’s name (str)
tail – a variable’s name (str)
- Return type
None
- erasePossibleEdge(*args)¶
Allow the 2 arcs to be added if necessary.
- Parameters
arc (pyAgrum) – an arc
head – a variable’s id (int)
tail – a variable’s id (int)
head – a variable’s name (str)
tail – a variable’s name (str)
- Return type
None
- hasMissingValues()¶
Indicates whether there are missing values in the database.
- Returns
True if there are some missing values in the database.
- 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_names (str) – a variable’s name
var_name (
str
) –
- Returns
the column id corresponding to a variable name
- Return type
int
- Raises
pyAgrum.MissingVariableInDatabase – If a variable of the BN is not found in the database.
- latentVariables(*args)¶
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
- learnDAG()¶
learn a structure from a file
- Returns
the learned DAG
- Return type
- learnMixedStructure()¶
Warning
learner must be using 3off2 or MIIC algorithm
- Returns
the learned structure as an EssentialGraph
- Return type
- 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 apriori (like K2 which has a 1-smoothing apriori), it is important to also take into account this implicit apriori for parameter learning. By default, if a score exists, we will learn parameters by taking into account the apriori specified by methods useAprioriXXX () + the implicit apriori of the score, else we just take into account the apriori specified by useAprioriXXX ()
- Returns
the learned BayesNet
- Return type
- 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)¶
logLikelihood computes the log-likelihood for the columns in vars, given the columns in the list knowing (optional)
- Parameters
vars (List[str]) – the name of the columns of interest
knowing (List[str]) – the (optional) list of names of conditioning columns
- Returns
the log-likelihood (base 2)
- 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
) – a node id- Returns
the variable’s name
- Return type
str
- names()¶
- Returns
the names of the variables in the database
- Return type
List[str]
- nbCols()¶
Return the nimber of columns in the database
- Returns
the number of columns in the database
- Return type
int
- nbRows()¶
Return the number of row in the database
- Returns
the number of rows in the database
- 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
- setAprioriWeight(weight)¶
Deprecated methods in BNLearner for pyAgrum>0.14.0
- setDatabaseWeight(new_weight)¶
Set the database weight which is given as an equivalent sample size.
- Parameters
weight (float) – the database weight
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
- setInitialDAG(g)¶
- Parameters
dag (pyAgrum.DAG) – an initial DAG structure
g (
DAG
) –
- Return type
None
- setMaxIndegree(max_indegree)¶
- Parameters
max_indegree (
int
) –- Return type
None
- 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
- setPeriodSize(p)¶
- Parameters
p (int) – number of samples between 2 stopping
- Raises
pyAgrum.OutOfBounds – If p<1
- Return type
None
- setRecordWeight(i, weight)¶
- Parameters
i (
int
) –weight (
float
) –
- Return type
None
- setSliceOrder(*args)¶
Set a partial order on the nodes.
- Parameters
l (list) – a list of sequences (composed of ids of rows or string)
- Return type
None
- setVerbosity(v)¶
- Parameters
v (bool) – verbosity
- Return type
None
- state()¶
- Return type
object
- use3off2()¶
Indicate that we wish to use 3off2.
- Return type
None
- useAprioriBDeu(*args)¶
The BDeu apriori adds weight to all the cells of the counting tables. In other words, it adds weight rows in the database with equally probable values.
- Parameters
weight (float) – the apriori weight
- Return type
None
- useAprioriDirichlet(*args)¶
- Return type
None
- useAprioriSmoothing(*args)¶
- Return type
None
- 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
None
- useGreedyHillClimbing()¶
- Return type
None
- useK2(*args)¶
Indicate to use the K2 algorithm (which needs a total ordering of the variables).
- Parameters
order (list[int or str]) – sequences of (ids or name)
- Return type
None
- useLocalSearchWithTabuList(*args)¶
Indicate that we wish to use a local search with tabu list
- Parameters
tabu_size (int) – The size of the tabu list
nb_decrease (int) – The max number of changes decreasing the score consecutively that we allow to apply
- Return type
None
- useMDLCorrection()¶
Indicate that we wish to use the MDL correction for 3off2 or MIIC
- Return type
None
- useMIIC()¶
Indicate that we wish to use MIIC.
- Return type
None
- useNMLCorrection()¶
Indicate that we wish to use the NML correction for 3off2 or MIIC
- Return type
None
- useNoApriori()¶
- Return type
None
- useNoCorrection()¶
Indicate that we wish to use the NoCorr correction for 3off2 or MIIC
- Return type
None
- useScoreAIC()¶
- Return type
None
- useScoreBD()¶
- Return type
None
- useScoreBDeu()¶
- Return type
None
- useScoreBIC()¶
- Return type
None
- useScoreK2()¶
- Return type
None
- useScoreLog2Likelihood()¶
- Return type
None
- verbosity()¶
- Returns
True if the verbosity is enabled
- Return type
bool