seriesClassification.cpp 2.27 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//
//  seriesClassification.cpp
//  RapidAPI
//
//  Created by mzed on 08/06/2017.
//  Copyright © 2017 Goldsmiths. All rights reserved.
//

#include <vector>
#include "seriesClassification.h"
#ifdef EMSCRIPTEN
#include "emscripten/seriesClassificationEmbindings.h"
#endif

seriesClassification::seriesClassification() {};

seriesClassification::~seriesClassification() {};

mzed's avatar
mzed committed
19
bool seriesClassification::addSeries(const std::vector<std::vector<double>> &newSeries) {
20
21
22
23
24
25
    dtw newDTW;
    newDTW.setSeries(newSeries);
    dtwClassifiers.push_back(newDTW);
    return true;
}

mzed's avatar
mzed committed
26
27
28
29
30
31
32
33
bool seriesClassification::addTrainingSet(const std::vector<trainingExample> &trainingSet) {
    std::vector<std::vector<double>> newSeries;
    for (int i = 0; i < trainingSet.size(); ++i) {
        newSeries.push_back(trainingSet[i].input);
    }
    return addSeries(newSeries);
};

mzed's avatar
mzed committed
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
bool seriesClassification::train(const std::vector<std::vector<std::vector<double> > > &newSeriesSet) {
    bool trained = true;
    reset();
    for (int i = 0; i < newSeriesSet.size(); ++i) {
        if (!addSeries(newSeriesSet[i])) {
            trained = false;
        };
    }
    return trained;
}

bool seriesClassification::trainTrainingSet(const std::vector<std::vector<trainingExample> > &seriesSet) {
    bool trained = true;
    reset();
    for (int i = 0; i < seriesSet.size(); ++i) {
        if (!addTrainingSet(seriesSet[i])) {
            trained = false;
        };
    }
    return trained;
}

mzed's avatar
mzed committed
56
void seriesClassification::reset() {
57
58
59
    dtwClassifiers.clear();
}

mzed's avatar
mzed committed
60
int seriesClassification::run(const std::vector<std::vector<double>> &inputSeries) {
61
62
    //TODO: check vector sizes and reject bad data
    int closestSeries = 0;
mzed's avatar
mzed committed
63
    double lowestCost = dtwClassifiers[0].run(inputSeries);
mzed's avatar
mzed committed
64
65
    for (int i = 0; i < dtwClassifiers.size(); ++i) {
        
mzed's avatar
mzed committed
66
        double currentCost = dtwClassifiers[i].run(inputSeries);
67
68
69
70
71
72
73
        if (currentCost < lowestCost) {
            lowestCost = currentCost;
            closestSeries = i;
        }
    }
    
    return closestSeries;
mzed's avatar
mzed committed
74
75
};

mzed's avatar
mzed committed
76
int seriesClassification::runTrainingSet(const std::vector<trainingExample> &trainingSet) {
mzed's avatar
mzed committed
77
78
79
80
    std::vector<std::vector<double>> newSeries;
    for (int i = 0; i < trainingSet.size(); ++i) {
        newSeries.push_back(trainingSet[i].input);
    }
mzed's avatar
mzed committed
81
    return run(newSeries);
82
};