-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalgorithm2.cpp
More file actions
71 lines (65 loc) · 2.09 KB
/
Copy pathalgorithm2.cpp
File metadata and controls
71 lines (65 loc) · 2.09 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
#include "algorithm2.hpp"
#include <math.h>
//Default constructor
Algorithm2::Algorithm2() {
_nbSample2Use = 70;
_name = "Cosine";
_id = 02;
_percentage = 0;
}
//Constructor with a value for _nbSample2Use
Algorithm2::Algorithm2(int nbSample2Use) {
_nbSample2Use = nbSample2Use;
_name = "Cosine";
_id = 02;
_percentage = 0;
}
//Apply the specific method of the algorithm to the data
float Algorithm2::applyMethod(vector<float> vecTesting, vector<float> vecTraining){
vector<float> newVecTesting, newVecTraining;
//cout << "_nbSample2Use = " << _nbSample2Use << endl;
for(int i=0;i<_nbSample2Use;i++){
newVecTesting.push_back(vecTesting[i]);
newVecTraining.push_back(vecTraining[i]);
}
float prodScal = scalarProduct(newVecTesting,newVecTraining);
float normeTest = norm(newVecTesting);
float normeRef = norm(newVecTraining);
//cout << "prodScalaire = " << prodScal << " normeTest = " << normeTest << " normeRef"
return prodScal/(normeRef*normeTest);
}
//Add the result predicted to a vector
void Algorithm2::add2PredictedResult(multimap<int, vector<float>> training,vector<float> firstResult) {
multimap<int,vector<float>>::iterator itTraining;
int indMax = max_element(firstResult.begin(),firstResult.end()) - firstResult.begin();
int j=0;
for(itTraining=training.begin();itTraining!=training.end();itTraining++) {
if (j == indMax) {
_predictedResult.push_back((*itTraining).first);
itTraining = training.end();
}
j++;
}
}
//compute scalar product for 2 vectors
float Algorithm2::scalarProduct(vector<float> vec1, vector<float> vec2){
float res = 0;
if(vec1.size()!=vec2.size()){
cout << "error for produitScalaire" << endl;
}else{
for(int i=0;i<vec1.size();i++){
res += vec1[i]*vec2[i];
}
}
return res;
}
//compute the norm for a vector
float Algorithm2::norm(vector<float> vec){
float res = 0;
float sum = 0;
for(int i=0;i<vec.size();i++){
sum += vec[i] * vec[i];
}
res = sqrt(sum);
return res;
}