seriesClassification.cpp 1.66 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
//
//  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() {};

bool seriesClassification::addSeries(std::vector<std::vector<double>> newSeries) {
    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
void seriesClassification::reset() {
35
36
37
38
    dtwClassifiers.clear();
}


mzed's avatar
mzed committed
39
int seriesClassification::run(std::vector<std::vector<double>> inputSeries) {
40
41
    //TODO: check vector sizes and reject bad data
    int closestSeries = 0;
mzed's avatar
mzed committed
42
    double lowestCost = dtwClassifiers[0].run(inputSeries);
mzed's avatar
mzed committed
43
44
    for (int i = 0; i < dtwClassifiers.size(); ++i) {
        
mzed's avatar
mzed committed
45
        double currentCost = dtwClassifiers[i].run(inputSeries);
46
47
48
49
50
51
52
        if (currentCost < lowestCost) {
            lowestCost = currentCost;
            closestSeries = i;
        }
    }
    
    return closestSeries;
mzed's avatar
mzed committed
53
54
};

mzed's avatar
mzed committed
55
int seriesClassification::runTrainingSet(const std::vector<trainingExample> &trainingSet) {
mzed's avatar
mzed committed
56
57
58
59
    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
60
    return run(newSeries);
61
};