Commit 2b71ef42 authored by Adrien Dorsaz's avatar Adrien Dorsaz

Fix some part about classifying and probability

parent db2f5570
......@@ -34,13 +34,15 @@ QString NaiveBayesClassifier::classify(QMap<double, double> *featureSet){
QMapIterator<double, double> featureIt(*featureSet);
while (featureIt.hasNext()) {
featureIt.next();
foreach(QString klass, _trainedClasses->keys()){
if(classProbability->value(klass)!=0)
classProbability->insert(klass,
classProbability->value(klass)
+ (featureIt.value() * log(probability(featureIt.key(), klass))));
int prob = probability(featureIt.key(), klass);
if(prob!=0){
classProbability->insert(klass,
classProbability->value(klass)
+ (featureIt.value() * log(prob)));
}
}
featureIt.next();
}
foreach(QString klass, classProbability->keys()){
......@@ -63,9 +65,14 @@ QString NaiveBayesClassifier::classify(QMap<double, double> *featureSet){
}
double NaiveBayesClassifier::probability(double feature, QString klass){
double num = _trainedClasses->value(klass)->value(feature)+1.0;
double den = _totalFeatureOccurrences->value(feature)+_trainedClasses->size();
return num/den;
int prob = 0;
if(_trainedClasses->value(klass)->contains(feature) && _totalFeatureOccurrences->contains(feature)){
double num = _trainedClasses->value(klass)->value(feature)+1.0;
double den = _totalFeatureOccurrences->value(feature)+_trainedClasses->size();
prob=num/den;
}
qDebug()<<"Probability for klass : "<<klass<<" and feature : "<<feature<<" is "<<prob;
return prob;
}
QMap<QString,QMap<double, double>*>& NaiveBayesClassifier::getTrainedClasses() const{
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment