naivebaiseclassifier.cpp 3.89 KB
Newer Older
1 2
#include "naivebaiseclassifier.h"

Adrien Dorsaz's avatar
Adrien Dorsaz committed
3 4 5 6 7
NaiveBaiseClassifier::NaiveBaiseClassifier(){
    _trainedClasses=new QMap<QString,QMap<double, double>*>();
    _totalFeatureOccurrences = new QMap<double,double>();
}

8 9
NaiveBaiseClassifier::NaiveBaiseClassifier(QMap<QString, QMap<double, double> *> *trainedClasses,
                                           QMap<double, double> *totalFeatureOccurrences){
Adrien Dorsaz's avatar
Adrien Dorsaz committed
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
    _trainedClasses = new QMap<QString, QMap<double,double>*>(*trainedClasses);
    foreach(QString klass, _trainedClasses->keys()){
        _trainedClasses->insert(klass,new QMap<double,double>(*_trainedClasses->value(klass)));
    }

    _totalFeatureOccurrences=new QMap<double,double>(*totalFeatureOccurrences);
}

NaiveBaiseClassifier::NaiveBaiseClassifier(QMap<QString, QMap<double, double> *> &trainedClasses,
                                           QMap<double, double> &totalFeatureOccurrences){
    _trainedClasses = new QMap<QString, QMap<double,double>*>(trainedClasses);
    foreach(QString klass, _trainedClasses->keys()){
        _trainedClasses->insert(klass,new QMap<double,double>(*_trainedClasses->value(klass)));
    }

    _totalFeatureOccurrences=new QMap<double,double>(totalFeatureOccurrences);
26 27 28 29 30 31 32 33 34 35 36 37
}

QString NaiveBaiseClassifier::classify(QMap<double, double> *featureSet){
    QMap<QString, double> * classProbability = new QMap<QString, double>();

    foreach(QString klass, _trainedClasses->keys()){
        classProbability->insert(klass, 0);
    }

    QMapIterator<double, double> featureIt(*featureSet);
    while (featureIt.hasNext()) {
        foreach(QString klass, _trainedClasses->keys()){
Adrien Dorsaz's avatar
Adrien Dorsaz committed
38
            if(classProbability->value(klass)!=0)
39
            classProbability->insert(klass,
Adrien Dorsaz's avatar
Adrien Dorsaz committed
40 41
                                     classProbability->value(klass)
                                     + (featureIt.value() * log(probability(featureIt.key(), klass))));
42
        }
Adrien Dorsaz's avatar
Adrien Dorsaz committed
43
        featureIt.next();
44 45
    }

Adrien Dorsaz's avatar
Adrien Dorsaz committed
46 47 48 49
    foreach(QString klass, classProbability->keys()){
        qDebug()<<klass<<" has prob. "<<classProbability->value(klass)<< " and size "<<_trainedClasses->value(klass)->size();
    }

50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
    QString resultClass="";

    foreach(QString klass, classProbability->keys()){
        if(resultClass.isEmpty()){
            resultClass=klass;
        }

        if(classProbability->value(klass)>classProbability->value(resultClass)){
            resultClass=klass;
        }
    }

    return resultClass;
}

double NaiveBaiseClassifier::probability(double feature, QString klass){
    double num = _trainedClasses->value(klass)->value(feature)+1.0;
    double den = _totalFeatureOccurrences->value(feature)+_trainedClasses->size();
    return num/den;
}
Adrien Dorsaz's avatar
Adrien Dorsaz committed
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94

QMap<QString,QMap<double, double>*>& NaiveBaiseClassifier::getTrainedClasses() const{
    return *_trainedClasses;
}

QMap<double, double>& NaiveBaiseClassifier::getTotalFeatureOccurences() const{
    return *_totalFeatureOccurrences;
}

QDataStream &operator<<(QDataStream &out, const NaiveBaiseClassifier &naiveBaise){
    QMap<QString, QMap<double,double>*>* trainedClasses =
            new QMap<QString, QMap<double,double>*>(naiveBaise.getTrainedClasses());
    qDebug()<<"size  : "<<naiveBaise.getTrainedClasses().size();
    out<<naiveBaise.getTrainedClasses().size();

    foreach(QString klass, trainedClasses->keys()){
        out<<klass;
        out<<*(trainedClasses->value(klass));
    }
    out << naiveBaise.getTotalFeatureOccurences();
    return out;
}

QDataStream &operator>>(QDataStream &in, NaiveBaiseClassifier &naiveBaise){
    int sizeMap;
95 96
    naiveBaise._trainedClasses=new QMap<QString,QMap<double, double>*>();

Adrien Dorsaz's avatar
Adrien Dorsaz committed
97 98 99 100 101 102 103
    QMap<double, double> totalFeatureOccurences;

    in >> sizeMap;
    for(int i=0;i<sizeMap;++i){
        QMap<double, double> trainedValue;
        QString trainedKey;
        in >> trainedKey >> trainedValue;
104
        naiveBaise._trainedClasses->insert(trainedKey, &trainedValue);
Adrien Dorsaz's avatar
Adrien Dorsaz committed
105
    }
106
    in >> *(naiveBaise._totalFeatureOccurrences);
Adrien Dorsaz's avatar
Adrien Dorsaz committed
107 108
    return in;
}