33 #ifndef DOXYGEN_SHOULD_SKIP_THIS 43 template <
typename GUM_SCALAR >
45 const std::string& filename,
46 const std::vector< std::string >& missing_symbols) :
47 genericBNLearner(filename, missing_symbols) {
51 template <
typename GUM_SCALAR >
57 template <
typename GUM_SCALAR >
59 const std::string& filename,
61 const std::vector< std::string >& missing_symbols) :
67 template <
typename GUM_SCALAR >
74 template <
typename GUM_SCALAR >
81 template <
typename GUM_SCALAR >
94 template <
typename GUM_SCALAR >
96 operator=(
const BNLearner< GUM_SCALAR >& src) {
102 template <
typename GUM_SCALAR >
104 operator=(BNLearner< GUM_SCALAR >&& src) {
110 template <
typename GUM_SCALAR >
114 if (notification !=
"") {
115 std::cout <<
"[aGrUM notification] " << notification << std::endl;
120 std::unique_ptr< ParamEstimator<> > param_estimator(
128 template <
typename GUM_SCALAR >
129 BayesNet< GUM_SCALAR >
131 bool take_into_account_score) {
133 if (dag.size() == 0)
return BayesNet< GUM_SCALAR >();
136 std::vector< NodeId > ids;
137 ids.reserve(dag.sizeNodes());
138 for (
const auto node : dag)
140 std::sort(ids.begin(), ids.end());
143 std::stringstream str;
144 str <<
"Learning parameters corresponding to the dag is impossible " 145 <<
"because the database does not contain the following nodeID";
146 std::vector< NodeId > bad_ids;
147 for (
const auto node : ids) {
150 if (bad_ids.size() > 1) str <<
's';
153 for (
const auto node : bad_ids) {
160 GUM_ERROR(MissingVariableInDatabase, str.str());
173 "In general, the BNLearner is unable to cope with " 174 <<
"missing values in databases. To learn parameters in " 175 <<
"such situations, you should first use method " 181 DBRowGeneratorSet<>());
182 std::unique_ptr< ParamEstimator<> > param_estimator(
188 BNLearnerListener listener(
this,
__Dag2BN);
193 const std::vector< gum::learning::DBTranslatedValueType > col_types(
197 DBRowGenerator4CompleteRows<> generator_bootstrap(col_types);
198 DBRowGeneratorSet<> genset_bootstrap;
199 genset_bootstrap.insertGenerator(generator_bootstrap);
202 std::unique_ptr< ParamEstimator<> > param_estimator_bootstrap(
206 BayesNet< GUM_SCALAR > dummy_bn;
207 DBRowGeneratorEM< GUM_SCALAR > generator_EM(col_types, dummy_bn);
208 DBRowGenerator<>& gen_EM = generator_EM;
209 DBRowGeneratorSet<> genset_EM;
210 genset_EM.insertGenerator(gen_EM);
212 std::unique_ptr< ParamEstimator<> > param_estimator_EM(
217 *(param_estimator_bootstrap.get()), *(param_estimator_EM.get()), dag);
223 template <
typename GUM_SCALAR >
224 BayesNet< GUM_SCALAR >
230 template <
typename GUM_SCALAR >
231 NodeProperty< Sequence< std::string > >
233 const BayesNet< GUM_SCALAR >& src) {
234 std::ifstream in(filename, std::ifstream::in);
236 if ((in.rdstate() & std::ifstream::failbit) != 0) {
240 CSVParser<> parser(in);
242 auto names = parser.current();
244 NodeProperty< Sequence< std::string > > modals;
251 for (
gum::Size i = 0; i < src.variable(graphId).domainSize(); ++i)
252 modals[col].insert(src.variable(graphId).label(i));
Class representing a Bayesian Network.
static BayesNet< GUM_SCALAR > createBN(ParamEstimator< ALLOC > &estimator, const DAG &dag)
create a BN from a DAG using a one pass generator (typically ML)
Database __score_database
the database to be used by the scores and parameter estimators
double __EMepsilon
epsilon for EM. if espilon=0.0 : no EM
void __createScore()
create the score used for learning
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
const std::vector< std::string > & names() const
returns the names of the variables in the database
AprioriType __apriori_type
the a priori selected for the score and parameters
BayesNet< GUM_SCALAR > learnParameters(const DAG &dag, bool take_into_account_score=true)
learns a BN (its parameters) when its structure is known
NodeProperty< Sequence< std::string > > __labelsFromBN(const std::string &filename, const BayesNet< GUM_SCALAR > &src)
read the first line of a file to find column names
DAG __initial_dag
an initial DAG given to learners
std::size_t nbVariables() const noexcept
returns the number of variables (columns) of the database
genericBNLearner(const std::string &filename, const std::vector< std::string > &missing_symbols)
default constructor
Database * __apriori_database
the database used by the Dirichlet a priori
bool hasMissingValues() const
indicates whether the database contains some missing values
ParamEstimator * __createParamEstimator(DBRowGeneratorParser<> &parser, bool take_into_account_score=true)
create the parameter estimator used for learning
DAG __learnDAG()
returns the DAG learnt
genericBNLearner & operator=(const genericBNLearner &)
copy operator
virtual ~BNLearner()
destructor
const DatabaseTable & databaseTable() const
returns the internal database table
std::string checkScoreAprioriCompatibility()
checks whether the current score and apriori are compatible
DAG2BNLearner __Dag2BN
the parametric EM
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
DBRowGeneratorParser & parser()
returns the parser for the database
BayesNet< GUM_SCALAR > learnBN()
learn a Bayes Net from a file (must have read the db before)
void __createApriori()
create the apriori used for learning
BNLearner & operator=(const BNLearner &)
copy operator
void setEpsilon(double eps)
Given that we approximate f(t), stopping criterion on |f(t+1)-f(t)|.
Size Idx
Type for indexes.
BNLearner(const std::string &filename, const std::vector< std::string > &missing_symbols={"?"})
default constructor
std::size_t Size
In aGrUM, hashed values are unsigned long int.
const DatabaseTable & database() const
returns the database used by the BNLearner
const std::vector< std::string > & names() const
returns the names of the variables in the database
Size NodeId
Type for node ids.
iterator handler() const
returns a new unsafe handler pointing to the 1st record of the database
#define GUM_ERROR(type, msg)