Exemple #1
0
void exe_naive_bayes()
{
    NaiveBayes nb = NaiveBayes();
    nb.set_training_data_file(std::string("training.dat"));
    nb.add_training_data("Buy cheap viagra SPAM");
    nb.add_training_data("Buy cheap airlines airlines tickets HAM");
    nb.add_training_data("Dear friend I am the king of Persia king SPAM");
    nb.add_training_data("Hello friend I am from Persia you must be from New York HAM");
    nb.add_training_data("Hi friend how are you doing I love you HAM");
    nb.add_training_data("New York is a big city HAM");

    nb.get_training_data();

    nb.train();

    std::string class_ = nb.classify(std::string("Buy cheap viagra tickets"));
    std::cout << "Your message is " << class_ << std::endl;

    class_ = nb.classify(std::string("Hello friend how are you"));
    std::cout << "Your message is " << class_ << std::endl;

    class_ = nb.classify(std::string("Dhaka is a big city"));
    std::cout << "Your message is " << class_ << std::endl;

}
Exemple #2
0
 int mostLikely(Digit const & digit )
 {
     Vector<int> values(64);
     for(int i = 0; i < 8; i++)
         for(int j = 0; j < 8; j++)
             values.append(mapColor(digit.color[i][j]));
     return nb.classify(values);
 }
Exemple #3
0
int main(int argc, const char * argv[]) {
    DataSet train = createDataSet("/Users/Hacker/Desktop/vote_train.arff.txt");
    DataSet test = createDataSet("/Users/Hacker/Desktop/vote_test.arff.txt");
    vector<Instance> testData = test.instances;
    vector<string> className = test.labels;
    long size = testData.size();
    NaiveBayes *n = new NaiveBayes(train);
    n->training(train);
    //Learning by NaiveBayes
    long correct_count = 0;
    
    
    for(long i = 0; i < size; i ++)
    {
        double probability = 0.00;
        probability = n->classify(testData[i]);
        string actualclass = className[testData[i].getlabel()];
        if(probability > 0.5)
        {
            cout<<className[0]<<" "<<actualclass<<" ";
            if(className[0] == actualclass)
            {
                correct_count ++;
            }
            printf("%.16f",probability);
        }
        else
        {
            probability = 1 - probability;
            cout<<className[1]<<" "<<actualclass<<" ";
            if(className[1] == actualclass)
            {
                correct_count ++;
            }
            printf("%.16f",probability);
        }
        cout<<endl;
    }
    
    cout<<"Number Of Correct Classification:"<<correct_count<<endl;
    
    
    
    delete n;
    
    return 0;
}