// 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] ));
}
//
}
Tuesday, November 30, 2010
Friday, November 26, 2010
C++ read from CSV file
Simpe metod to read from a csv file:
#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();
}
#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();
}
Monday, November 15, 2010
Levestein Metric
CREATE FUNCTION edit_distance(@s1 nvarchar(3999), @s2 nvarchar(3999))
RETURNS int
AS
BEGIN
DECLARE @s1_len int, @s2_len int, @i int, @j int, @s1_char nchar, @c int, @c_temp int,
@cv0 varbinary(8000), @cv1 varbinary(8000)
SELECT @s1_len = LEN(@s1), @s2_len = LEN(@s2), @cv1 = 0x0000, @j = 1, @i = 1, @c = 0
WHILE @j <= @s2_len
SELECT @cv1 = @cv1 + CAST(@j AS binary(2)), @j = @j + 1
WHILE @i <= @s1_len
BEGIN
SELECT @s1_char = SUBSTRING(@s1, @i, 1), @c = @i, @cv0 = CAST(@i AS binary(2)), @j = 1
WHILE @j <= @s2_len
BEGIN
SET @c = @c + 1
SET @c_temp = CAST(SUBSTRING(@cv1, @j+@j-1, 2) AS int) +
CASE WHEN @s1_char = SUBSTRING(@s2, @j, 1) THEN 0 ELSE 1 END
IF @c > @c_temp SET @c = @c_temp
SET @c_temp = CAST(SUBSTRING(@cv1, @j+@j+1, 2) AS int)+1
IF @c > @c_temp SET @c = @c_temp
SELECT @cv0 = @cv0 + CAST(@c AS binary(2)), @j = @j + 1
END
SELECT @cv1 = @cv0, @i = @i + 1
END
RETURN @c
END
RETURNS int
AS
BEGIN
DECLARE @s1_len int, @s2_len int, @i int, @j int, @s1_char nchar, @c int, @c_temp int,
@cv0 varbinary(8000), @cv1 varbinary(8000)
SELECT @s1_len = LEN(@s1), @s2_len = LEN(@s2), @cv1 = 0x0000, @j = 1, @i = 1, @c = 0
WHILE @j <= @s2_len
SELECT @cv1 = @cv1 + CAST(@j AS binary(2)), @j = @j + 1
WHILE @i <= @s1_len
BEGIN
SELECT @s1_char = SUBSTRING(@s1, @i, 1), @c = @i, @cv0 = CAST(@i AS binary(2)), @j = 1
WHILE @j <= @s2_len
BEGIN
SET @c = @c + 1
SET @c_temp = CAST(SUBSTRING(@cv1, @j+@j-1, 2) AS int) +
CASE WHEN @s1_char = SUBSTRING(@s2, @j, 1) THEN 0 ELSE 1 END
IF @c > @c_temp SET @c = @c_temp
SET @c_temp = CAST(SUBSTRING(@cv1, @j+@j+1, 2) AS int)+1
IF @c > @c_temp SET @c = @c_temp
SELECT @cv0 = @cv0 + CAST(@c AS binary(2)), @j = @j + 1
END
SELECT @cv1 = @cv0, @i = @i + 1
END
RETURN @c
END
Monday, November 8, 2010
Install .NET Service
Run InstallUtil.exe from the command line with your project's output as a parameter. Enter the following code on the command line:
Subscribe to:
Posts (Atom)