-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathDimReductionMethod.cpp
More file actions
110 lines (91 loc) · 2.76 KB
/
DimReductionMethod.cpp
File metadata and controls
110 lines (91 loc) · 2.76 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
///////////////////////////////////////////////////////////
// DimReductionMethod.cpp
// Implementation of the Class DimReductionMethod
// Created on: 07-Lie-2013 20:07:29
// Original author: Povilas
///////////////////////////////////////////////////////////
/*! \class DimReductionMethod
\brief A class of methods and attributes for initialization of projection matrix.
*/
#include "DimReductionMethod.h"
#include "DistanceMetrics.h"
#include "AdditionalMethods.h"
#include "Statistics.h"
#include <cmath>
#include <iostream>
DimReductionMethod::DimReductionMethod()
{
d = 2;
}
DimReductionMethod::~DimReductionMethod()
{
}
int DimReductionMethod::getProjectionDimension()
{
return d;
}
void DimReductionMethod::initializeProjectionMatrix()
{
int n = X.getObjectCount();
Y = ObjectMatrix(n);
std::vector<double> DataObjectItem;
DataObjectItem.reserve(0);//push_back(0.0);
for (int i = 0; i < n; i++)
{
// DataObject tmp = X.getObjectAt(i);
for (int j = 0; j < d; j++)
{
double rnd = Statistics::getRandom(-5, 5);
DataObjectItem.push_back(rnd);
}
if (X.getClassCount() > 0)
{
Y.addObject(DataObject(DataObjectItem, X.getObjectAt(i).getClassLabel()));
// std::cout << X.getObjectAt(i).getClassLabel() <<std::endl;
}
else
Y.addObject(DataObject(DataObjectItem));
DataObjectItem.clear();
}
}
void DimReductionMethod::setProjectionDimension(int dimension)
{
d = dimension;
}
// TODO (Povilas#1#): Rewrite stress calculation without sqrt
double DimReductionMethod::getStress()
{
int m = X.getObjectCount(), n = Y.getObjectAt(0).getFeatureCount(),i, j, z;
double stress = 0.0, s, distY, distX, diff;
int noOfBytes = sizeof(double); //for file reading
std::vector<double> ob1, ob2;
// FILE *distFile;
/// fclose(AdditionalMethods::distFile);
AdditionalMethods::distFile = fopen(AdditionalMethods::tempFileSavePath, "rb");
double tmpDist;
// TODO (Povilas#1#): Check if file exists
for (i = 0; i < m - 1; i++)
{
ob1 = Y.getObjectAt(i).getFeatures();
for (j = i + 1; j < m; j++)
{
ob2 = Y.getObjectAt(j).getFeatures();
s = 0.0;
for (z = 0; z < n; z++)
{
diff = ob1.at(z) - ob2.at(z);
s += diff * diff;
}
distY = std::sqrt(s);
fread(&distX, noOfBytes, 1, AdditionalMethods::distFile);
tmpDist = distX - distY;
stress += tmpDist * tmpDist;
}
}
///
fclose(AdditionalMethods::distFile);
return stress * 1. / X.getWeight();
}
/*ObjectMatrix DimReductionMethod::getProjection(){
return Y;
}*/