The load/store the internal classifier state, uses boost::serializable library. The internal state for example presented below is here. The example find the weather, based on some train examples.
#include <iostream> #include <sstream> #include <string> #include <boost/serialization/nvp.hpp> #include <boost/archive/xml_oarchive.hpp> #include <boost/archive/xml_iarchive.hpp> #include <faif/learning/NaiveBayesian.hpp> using namespace std; using namespace faif; using namespace faif::ml; Domains attribs; string A1[] = {"sunny", "overcast", "rain" }; attribs.push_back( createDomain("outlook", A1, A1 + 3) ); string A2[] = {"hot", "mild", "cold"}; attribs.push_back( createDomain("temperature", A2, A2 + 3) ); string A3[] = {"normal", "high"}; attribs.push_back( createDomain("humidity", A3, A3 + 2) ); string A4[] = {"strong", "weak"}; attribs.push_back( createDomain("wind", A4, A4 + 2) ); string C[] = {"good","bad"}; AttrDomain cat = createDomain("", C, C+2); NaiveBayesian nb( attribs, cat ); //create the 'empty' classifier ExamplesTrain ex; string E01[] = { "sunny", "hot", "high", "weak"}; ex.push_back( nb.createExample( E01, E01 + 4, "bad" ) ); string E02[] = { "sunny", "hot", "high", "strong"}; ex.push_back( nb.createExample( E02, E02 + 4, "bad" ) ); string E03[] = { "overcast", "hot", "high", "weak"}; ex.push_back( nb.createExample( E03, E03 + 4, "good" ) ); string E04[] = { "rain", "mild", "high", "weak"}; ex.push_back( nb.createExample( E04, E04 + 4, "good" ) ); string E05[] = { "rain", "cold", "normal", "weak"}; ex.push_back( nb.createExample( E05, E05 + 4, "good" ) ); string E06[] = { "rain", "cold", "normal", "strong"}; ex.push_back( nb.createExample( E06, E06 + 4, "bad" ) ); string E07[] = { "overcast", "cold", "normal", "strong"}; ex.push_back( nb.createExample( E07, E07 + 4, "good" ) ); string E08[] = { "sunny", "mild", "high", "weak"}; ex.push_back( nb.createExample( E08, E08 + 4, "bad" ) ); string E09[] = { "sunny", "cold", "normal", "weak"}; ex.push_back( nb.createExample( E09, E09 + 4, "good" ) ); string E10[] = { "rain", "mild", "normal", "weak"}; ex.push_back( nb.createExample( E10, E10 + 4, "good" ) ); string E11[] = { "sunny", "mild", "normal", "strong"}; ex.push_back( nb.createExample( E11, E11 + 4, "good" ) ); string E12[] = { "overcast", "mild", "high", "strong"}; ex.push_back( nb.createExample( E12, E12 + 4, "good" ) ); string E13[] = { "overcast", "hot", "normal", "weak"}; ex.push_back( nb.createExample( E13, E13 + 4, "good" ) ); string E14[] = { "rain", "mild", "high", "strong"}; ex.push_back( nb.createExample( E14, E14 + 4, "bad" ) ); nb.train( ex ); //train ostringstream oss; boost::archive::xml_oarchive oa(oss); oa << boost::serialization::make_nvp("NBC", nb ); NaiveBayesian emptyNb( attribs, cat ); //crete the 'empty' classifier std::istringstream iss(oss.str()); boost::archive::xml_iarchive ia(iss); ia >> boost::serialization::make_nvp("NBC", emptyNb); //de-serialize the stored object string ET[] = { "overcast", "hot", "high", "weak"}; ExampleTest et = nb.createExample( ET, ET + 4); //the classifier shoulde return the 'bad' category emptyNb.getCategory(et) == nb.getCategoryIdd("good") ); //truereturn to the main page