Tuesday, November 30, 2010

naive bayes Algorithm

// NaiveBayes.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include < string >
#include < vector >
#include< iostream >
#include< fstream >
 #include < math.h >

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    vector< vector< float > > descriptor;
    vector< float > classes;
    ifstream f;
    f.open("c:\\bayes.txt");
    while(f)
    {
        string str = "";
        f > >str;
        vector< float > desc;
        size_t found = str.find(",");
        while(found< 100 && found >0)
        {
            string t = str.substr(0,found);
            desc.push_back(strtod(t.c_str(), NULL));
            str = str.substr(found+1);
            found = str.find(",");
        }
        if(desc.size() >0)
        {
            descriptor.push_back(desc);
            classes.push_back(strtod(str.c_str(), NULL));
        }
      
    }

    f.close();
   
    //0.51,0.52,0.54,0.51,0.53,0
    vector< float > v;
    v.push_back(0.51);
    v.push_back(0.52);
    v.push_back(0.54);
    v.push_back(0.51);
    v.push_back(0.53);
   
   
    vector< float > mean1;
    mean1.resize(descriptor[0].size());
    vector< float > mean2;
    mean2.resize(descriptor[0].size());
    int nr1 = 0;
    int nr2 = 0;
    vector< float > disp1;
    vector< float > disp2;
    disp1.resize(descriptor[0].size());
    disp2.resize(descriptor[0].size());
   
    // calcul medie
    for(int i=0;i< descriptor.size();i++)
    {
        if(classes[i] == 0)
        {
            for(int j=0;j< mean1.size();j++)
            {
                mean1[j] +=descriptor[i][j];
            }
            nr1++;
        }
        else
        {
            for(int j=0;j< mean1.size();j++)
            {
                mean2[j] +=descriptor[i][j];
            }
            nr2++;
        }
    }
    for(int j=0;j< mean1.size();j++)
            {
                mean1[j] /=nr1;
                mean2[j] /=nr2;
            }
   // calcul dispersie
   for(int i=0;i< descriptor.size();i++)
    {
        if(classes[i] == 0)
        {
            for(int j=0;j< mean1.size();j++)
            {
                disp1[j] += (mean1[j] - v[j])*(mean1[j] - v[j]);
            }
        }
        else
        {
            for(int j=0;j< mean1.size();j++)
            {
                disp2[j] += (mean2[j] - v[j])*(mean2[j] - v[j]);
            }
        }
    }
      for(int j=0;j< mean1.size();j++)
            {
                disp1[j] = sqrt(disp1[j])/nr1;
                disp2[j] = sqrt(disp2[j])/nr2;
            }
           
           
       float r1 = 1;
       float r2 = 1;
      
        for(int j=0;j< mean1.size();j++)
            {
                r1 = r1*((1/disp1[j])*exp( (-1) * (mean1[j] - v[j])*(mean1[j] - v[j])/disp1[j]  ));
                r2 = r2*((1/disp2[j])*exp( (-1) * (mean2[j] - v[j])*(mean2[j] - v[j])/disp2[j]  ));
            }
      
    //
   

}

No comments:

Post a Comment