FAIF - naive Bayesian classifier

Simple using naive Bayesian classifier from faif.

Fast start

The example find the weather, based on some train examples.
        #include <faif/learning/NaiveBayesian.hpp>
        #include <faif/learning/Validator.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

        string ET[] = { "overcast", "hot", "high", "weak"}; ExampleTest et = nb.createExample( ET, ET + 4);

        //the classifier shoulde return the 'bad' category
        nb.getCategory(et) == nb.getCategoryIdd("good") ); //true

		std::cout << checkCross(ex, 14, nb) << std::endl;
      
return to the main page