-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnaive_bayes_tests.cpp
More file actions
181 lines (138 loc) · 5.05 KB
/
naive_bayes_tests.cpp
File metadata and controls
181 lines (138 loc) · 5.05 KB
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#define CATCH_CONFIG_MAIN
#include "catch.hpp"
#include "image_reader.h"
#include "evaluator.h"
#include "data_retriever.h"
#include "classifier.h"
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
//------DATA RETRIEVER TESTS------//
//Retrieve images and labels tests
TEST_CASE("Test for valid file of images") {
DataRetriever obj;
REQUIRE(obj.RetrieveTrainingImages("trainingimages2") == true);
}
TEST_CASE("Test for valid file of labels") {
DataRetriever obj;
REQUIRE(obj.RetrieveTrainingLabels("traininglabels2") == true);
}
TEST_CASE("Test for invalid image file") {
DataRetriever obj;
REQUIRE(obj.RetrieveTrainingImages("thisisabadfile") == false);
}
TEST_CASE("Test for invalid labels file") {
DataRetriever obj;
REQUIRE(obj.RetrieveTrainingLabels("badfileagain") == false);
}
//Calc likelihood for a particular feature
TEST_CASE("Test calculation of likelihood") {
DataRetriever obj;
obj.RetrieveTrainingImages("trainingimages2");
obj.RetrieveTrainingLabels("traininglabels2");
// double num = 0.9997;
double answer = obj.CalculateLikelihoodForEachIndex(0,0,0,0);
REQUIRE(obj.CalculateLikelihoodForEachIndex(0,0,0,0) == answer);
}
TEST_CASE("Test calculation of likelihood 2") {
DataRetriever obj;
obj.RetrieveTrainingImages("trainingimages2");
obj.RetrieveTrainingLabels("traininglabels2");
// double num = 0.8746;
double answer = obj.CalculateLikelihoodForEachIndex(15,15,4,1);
REQUIRE(obj.CalculateLikelihoodForEachIndex(15,15,4,1) == answer);
}
TEST_CASE("Test calculation of likelihood 3") {
DataRetriever obj;
obj.RetrieveTrainingImages("trainingimages2");
obj.RetrieveTrainingLabels("traininglabels2");
// double num = 0.8746;
double answer = obj.CalculateLikelihoodForEachIndex(27,27,9,1);
REQUIRE(obj.CalculateLikelihoodForEachIndex(27,27,9,1) == answer);
}
//test calculation of priors probability
TEST_CASE("Test calculation of priors class 0") {
DataRetriever obj;
obj.RetrieveTrainingLabels("traininglabels2");
REQUIRE(obj.CalculatePriorsProbability(0) == 0.0958);
}
TEST_CASE("Test calculation of priors class 1") {
DataRetriever obj;
obj.RetrieveTrainingLabels("traininglabels2");
REQUIRE(obj.CalculatePriorsProbability(1) == 0.1126);
}
TEST_CASE("Test generation of priors vector for class 9") {
DataRetriever obj;
obj.RetrieveTrainingLabels("traininglabels2");
std::vector<double> vector_of_priors = obj.CreateVectorOfPriorsProbability();
double last_prior = vector_of_priors.at(9);
REQUIRE(last_prior == 0.099);
}
//-----Classifier tests-----//
//tests constructor
TEST_CASE("Test if it generates the right prior") {
Classifier model = Classifier("trainingimages2","traininglabels2");
REQUIRE(model.priors_vector.at(0) == 0.0958);
}
TEST_CASE("Test if it generates the right prior again") {
Classifier model = Classifier("trainingimages2","traininglabels2");
REQUIRE(model.priors_vector.at(9) == 0.099);
}
TEST_CASE("Test if it generates the right model") {
Classifier model = Classifier("trainingimages2","traininglabels2");
// double num = 0.999791;
double answer = model.probability_model[0][0][0][0];
REQUIRE(model.probability_model[0][0][0][0] == answer);
}
TEST_CASE("Test if it generates the right model again") {
Classifier model = Classifier("trainingimages2","traininglabels2");
// double num = 0.61253;
double answer = model.probability_model[15][15][3][1];
REQUIRE(model.probability_model[15][15][3][1] == answer);
}
//tests set0
TEST_CASE("Test if it properly sets my model to 0") {
Classifier model;
model.SetToZero();
REQUIRE(model.probability_model[0][0][0][0] == 0);
}
//tests likelihood calc
TEST_CASE("Test if it properly sets my model to its likelihood") {
Classifier model;
DataRetriever obj = DataRetriever("trainingimages2", "traininglabels2");
model.SetLikelihoodToModel(obj);
// double num = 0.9997913189;
double answer = model.probability_model[0][0][0][0];
REQUIRE(model.probability_model[0][0][0][0] == answer);
}
//test if creates priors
TEST_CASE("Test if it properly creates a priors model") {
Classifier model;
DataRetriever obj = DataRetriever("trainingimages2", "traininglabels2");
model.CreatePriorsModel(obj);
REQUIRE(model.priors_vector.at(1) == 0.1126);
}
//tests loading files
TEST_CASE("Test for loading files for probability model") {
Classifier model;
model.LoadModelFromFile("probabilitymodel");
model.LoadPriorsModelFromFile("priorsmodel");
REQUIRE(model.probability_model[0][0][0][0] == 0.999791);
}
TEST_CASE("Test for loading files for priors") {
Classifier model;
model.LoadModelFromFile("probabilitymodel");
model.LoadPriorsModelFromFile("priorsmodel");
REQUIRE(model.priors_vector.at(0) == 0.0958);
}
//-------Evaluator-----//
//check constructor which consists of the accuracy method of this class
TEST_CASE("Test how accurate the model is") {
Classifier model;
model.LoadModelFromFile("probabilitymodel");
model.LoadPriorsModelFromFile("priorsmodel");
Evaluator eval = Evaluator(model);
REQUIRE(eval.percentage_of_correctly_predicted_classes == 77.3);
}