Created
October 15, 2010 07:20
-
-
Save volkansalma/627767 to your computer and use it in GitHub Desktop.
Naive Bayes Sınıflandırıcı. http://volkansalma.blogspot.com
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
#include <fstream> | |
#include <cmath> | |
using namespace std; | |
struct FeatureVector | |
{ | |
int label; //0 for male 1 for female | |
double height; | |
double weight; | |
double footSize; | |
}; | |
void readFileAndFillFeatureVectors(vector<FeatureVector>&, | |
vector<FeatureVector>&); | |
void calculateMeansForFeatures(const vector<FeatureVector> &males,const vector<FeatureVector> &females, | |
double &maleFootSizeMean,double &maleWeightMean,double &maleHeightMean, | |
double &femaleFootSizeMean,double &femaleWeightMean,double &femaleHeightMean); | |
void calculateVariancesForFeatures(const vector<FeatureVector> &males,const vector<FeatureVector> &females, | |
double maleFootSizeMean,double maleWeightMean,double maleHeightMean, | |
double femaleFootSizeMean,double femaleWeightMean,double femaleHeightMean, | |
1.5789 double &maleFootSizeVariance,double &maleWeightVariance,double &maleHeightVariance, | |
double &femaleFootSizeVariance,double &femaleWeightVariance,double &femaleHeightVariance); | |
double calculateGaussProbabilityValueForX(double mean, double variance, double x); | |
int main() | |
{ | |
vector<FeatureVector>females; | |
vector<FeatureVector>males; | |
double priorProbabiltyFemale = 0; | |
double priorProbabiltyMale = 0; | |
double maleFootSizeMean = 0,maleWeightMean = 0,maleHeightMean = 0; | |
double femaleFootSizeMean = 0,femaleWeightMean = 0,femaleHeightMean = 0; | |
double maleFootSizeVariance = 0,maleWeightVariance = 0,maleHeightVariance = 0; | |
double femaleFootSizeVariance = 0,femaleWeightVariance = 0,femaleHeightVariance = 0; | |
readFileAndFillFeatureVectors(males,females); | |
int allDataCount = females.size()+males.size(); | |
int femalesCount = females.size(); | |
int malesCount = males.size(); | |
priorProbabiltyFemale = (double)femalesCount/allDataCount; | |
priorProbabiltyMale = (double)malesCount/allDataCount; | |
//posteriorProbabiltyFemale = priorFemaleProbabilty * P(height|female) * P(weight|female) * P(foot size|female) * evidence | |
//posteriorProbabilityMale = priorMaleProbabilty * P(height|male) * P(weight|male) * P(foot size|male) * evidence | |
// evidence ları sadeleştiriyoruz. | |
//hangi posterior olasılık büyük ise gelen vektörü o sınıftan kabul ediyoruz. | |
calculateMeansForFeatures(males,females, maleFootSizeMean,maleWeightMean,maleHeightMean, | |
femaleFootSizeMean,femaleWeightMean,femaleHeightMean); | |
calculateVariancesForFeatures(males,females,maleFootSizeMean,maleWeightMean,maleHeightMean, | |
femaleFootSizeMean,femaleWeightMean,femaleHeightMean, | |
maleFootSizeVariance,maleWeightVariance,maleHeightVariance, | |
femaleFootSizeVariance,femaleWeightVariance,femaleHeightVariance); | |
//elimize bir test verisi geldi. | |
// gelen featurelar aşağıdaki gibi. | |
// bu verilere sahip bireyin cinsiyeti tahmin etmek istiyoruz. | |
double testHeight = 6; | |
double testWeight = 130; | |
double testFootSize = 8; | |
double maleProbabilityAccordingToHeight = calculateGaussProbabilityValueForX(maleHeightMean,maleHeightVariance,testHeight); | |
double femaleProbabilityAccordingToHeight = calculateGaussProbabilityValueForX(femaleHeightMean,femaleHeightVariance,testHeight); | |
double maleProbabilityAccordingToWeight = calculateGaussProbabilityValueForX(maleWeightMean,maleWeightVariance,testWeight); | |
double femaleProbabilityAccordingToWeight = calculateGaussProbabilityValueForX(femaleWeightMean,femaleWeightVariance,testWeight); | |
double maleProbabilityAccordingToFootSize = calculateGaussProbabilityValueForX(maleFootSizeMean,maleFootSizeVariance,testFootSize); | |
double femaleProbabilityAccordingToFootSize = calculateGaussProbabilityValueForX(femaleWeightMean,femaleWeightVariance,testFootSize); | |
double totalMaleProbablity = priorProbabiltyMale * maleProbabilityAccordingToFootSize * maleProbabilityAccordingToWeight * maleProbabilityAccordingToHeight; | |
double totalFemaleProbablity = priorProbabiltyFemale * femaleProbabilityAccordingToFootSize * femaleProbabilityAccordingToWeight * femaleProbabilityAccordingToHeight; | |
if(totalMaleProbablity > totalFemaleProbablity) | |
{ | |
cout<<"Erkek"<<endl; | |
} | |
else | |
{ | |
cout<<"Bayan"<<endl; | |
} | |
return 0; | |
} | |
double calculateGaussProbabilityValueForX(double mean, double variance, double x) | |
{ | |
//Gauss olasılık yoğunluk fonksiyonu. | |
//Fena ekilde hassasiyet bozulması olabilir. | |
double payda (sqrt(2*3.14) * variance); | |
double pay = pow( 2.718 , (-1* (pow((x-mean),2)/(2*pow(variance,2)))) ); | |
return (pay / payda); | |
} | |
void calculateMeansForFeatures(const vector<FeatureVector> &males,const vector<FeatureVector> &females, | |
double &maleFootSizeMean,double &maleWeightMean,double &maleHeightMean, | |
double &femaleFootSizeMean,double &femaleWeightMean,double &femaleHeightMean) | |
{ | |
int malesCount = males.size(); | |
int femalesCount = females.size(); | |
for (int i = 0; i < malesCount; i++) | |
{ | |
maleFootSizeMean += males[i].footSize; | |
maleWeightMean += males[i].weight; | |
maleHeightMean += males[i].height; | |
} | |
for (int i = 0; i < femalesCount; i++) | |
{ | |
femaleFootSizeMean += females[i].footSize; | |
femaleWeightMean += females[i].weight; | |
femaleHeightMean += females[i].height; | |
} | |
maleFootSizeMean /= malesCount*1.0; | |
maleHeightMean /= malesCount*1.0; | |
maleWeightMean /= malesCount*1.0; | |
femaleFootSizeMean /= femalesCount*1.0; | |
femaleHeightMean /= femalesCount*1.0; | |
femaleWeightMean /= femalesCount*1.0; | |
} | |
void calculateVariancesForFeatures(const vector<FeatureVector> &males,const vector<FeatureVector> &females, | |
double maleFootSizeMean,double maleWeightMean,double maleHeightMean, | |
double femaleFootSizeMean,double femaleWeightMean,double femaleHeightMean, | |
double &maleFootSizeVariance,double &maleWeightVariance,double &maleHeightVariance, | |
double &femaleFootSizeVariance,double &femaleWeightVariance,double &femaleHeightVariance) | |
{ | |
int malesCount = males.size(); | |
int femalesCount = females.size(); | |
for (int i = 0; i < malesCount; i++) | |
{ | |
maleFootSizeVariance += pow((maleFootSizeMean - males[i].footSize),2); | |
maleWeightVariance += pow((maleWeightMean - males[i].weight),2); | |
maleHeightVariance += pow(maleHeightMean - males[i].height,2); | |
} | |
for (int i = 0; i < femalesCount; i++) | |
{ | |
femaleFootSizeVariance += pow((femaleFootSizeMean - females[i].footSize),2); | |
femaleWeightVariance += pow((femaleWeightMean - females[i].weight),2); | |
femaleHeightVariance += pow((femaleHeightMean - females[i].height),2); | |
} | |
femaleFootSizeVariance /= (double)femalesCount; | |
femaleWeightVariance /= (double)femalesCount; | |
femaleHeightVariance /= (double)femalesCount; | |
maleFootSizeVariance /= (double)malesCount; | |
maleWeightVariance /= (double)malesCount; | |
maleHeightVariance /= (double)malesCount; | |
} | |
void readFileAndFillFeatureVectors(vector<FeatureVector> &males, | |
vector<FeatureVector> &females) | |
{ | |
FeatureVector oneVector; | |
fstream vectorfile; | |
vectorfile.open("data",ios::in); | |
while(true) | |
{ | |
oneVector.label = -1; | |
vectorfile>>oneVector.label>>oneVector.height | |
>>oneVector.weight | |
>>oneVector.footSize; | |
if(oneVector.label == -1) break; | |
if (oneVector.label == 0) | |
{ | |
males.push_back(oneVector); | |
} | |
else | |
{ | |
females.push_back(oneVector); | |
} | |
} | |
vectorfile.close(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment