-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathClusterizationMethods.cpp
More file actions
205 lines (176 loc) · 5.43 KB
/
ClusterizationMethods.cpp
File metadata and controls
205 lines (176 loc) · 5.43 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
///////////////////////////////////////////////////////////
// ClusterizationMethods.cpp
// Implementation of the Class ClusterizationMethod
// Created on: 27-Kov-2014 12:52:15
// Original author: Povilas
///////////////////////////////////////////////////////////
#include "ClusterizationMethods.h"
#include "AdditionalMethods.h"
#include "Statistics.h"
#include <vector>
ClusterizationMethods::ClusterizationMethods()
{
testObjQtty = 0;
learnObjQtty = 0;
uknownClassObjQtty = 0;
initializeXMatrix();
noOfClusters = X.getClassCount();
objectIndex.reserve(0);
/* for (int i = 0; i < X.getObjectCount(); i++)
objectIndex.push_back(i);*/
}
ClusterizationMethods::ClusterizationMethods(int clNo)
{
testObjQtty = 0;
learnObjQtty = 0;
uknownClassObjQtty = 0;
noOfClusters = clNo;
initializeXMatrix();
}
ClusterizationMethods::~ClusterizationMethods()
{
}
//overloaded constructor that accepts no of clusters and no of features
/*ClusterizationMethods::ClusterizationMethods(int noOfClusters, int noOfFeatures)
{
testObjQtty = 0;
learnObjQtty = 0;
uknownClassObjQtty = 0;
this->noOfClusters = noOfClusters;
this->noOfFeatures = noOfFeatures;
// this->initializeXMatrix();
}*/
//initializes X matrix
void ClusterizationMethods::initializeXMatrix()
{
X = ObjectMatrix(AdditionalMethods::inputDataFile);
X.loadDataMatrix();
}
//returns number of clusters
int ClusterizationMethods::getNoOfClusters()
{
//int m = noOfClusters;
return noOfClusters;
}
/**
* Returns number of clusters (Y matrix rows)
*/
int ClusterizationMethods::getNoOfReturnRows()
{
return noOfRows;
}
/**
* Returns number of Y matrix row features
*/
int ClusterizationMethods::getNoOfReturnRowFeatures()
{
return noOfFeatures;
}
void ClusterizationMethods::initializeYMatrix()
{
int r = getNoOfReturnRows(), c = getNoOfReturnRowFeatures();
Y = ObjectMatrix(r);
std::vector<double> dataObjectItem;
dataObjectItem.reserve(0);
//int r = getNoOfReturnRows(), c = getNoOfReturnRowFeatures();
for (int i = 0; i < r; i++)
{
for (int j = 0; j < c; j++)
dataObjectItem.push_back(Statistics::getRandom(-5, 5));
if (X.getClassCount() > 0)
Y.addObject(DataObject(dataObjectItem, X.getObjectAt(i).getClassLabel()));
else
Y.addObject(DataObject(dataObjectItem));
//Y.addObject(DataObject(dataObjectItem));
dataObjectItem.clear();
}
}
void ClusterizationMethods::initializeYMatrix(int rows, int cols)
{
setYMatrixDimensions(rows, cols);
initializeYMatrix();
}
/**
* Sets the number of found classes (used by kmeans ger_k)
*/
void ClusterizationMethods::setNoOfReturnRows(int r)
{
noOfRows = r;
}
void ClusterizationMethods::setNoOfReturnRowFeatures(int f)
{
noOfFeatures = f;
}
void ClusterizationMethods::setYMatrixDimensions(int rows, int cols)
{
ClusterizationMethods::setNoOfReturnRows(rows);
ClusterizationMethods::setNoOfReturnRowFeatures(cols);
}
void ClusterizationMethods::initializeData(double dL, double dT)
{
int objCount = X.getObjectCount();
int n = X.getObjectAt(0).getFeatureCount();
int i, j;
//count how many we have unknown classes
for (i = 0; i < objCount; i++ )
if (X.getObjectAt(i).getClassLabel() == -1)
uknownClassObjQtty++;
int available = objCount - uknownClassObjQtty; //set the number of available objects for learning and testing
learnObjQtty = ceil(available * (dL / 100.));
testObjQtty = ceil(available * (dT / 100.));
while (learnObjQtty + testObjQtty > available)
if (testObjQtty >= 2)
testObjQtty--;
else
learnObjQtty--;
//set dimmensions of the array n+1 = last element points to class
this->learnSet.setlength(learnObjQtty, n + 1);
this->testSet.setlength(testObjQtty, n + 1);
DataObject tmp;
int tmpL = 0, tmpT = 0;
for (i = 0; i < objCount; i++)
{
tmp = X.getObjectAt(i);
if ( tmpL < learnObjQtty && tmp.getClassLabel() != -1)
{
for (j = 0; j < n; j++)
learnSet(tmpL,j) = tmp.getFeatureAt(j);
learnSet(tmpL,j) = tmp.getClassLabel();
tmpL++;
}
else if ( tmpT < testObjQtty && tmp.getClassLabel() != -1)
{
for (j = 0; j < n; j++)
testSet(tmpT,j) = tmp.getFeatureAt(j);
testSet(tmpT,j) = tmp.getClassLabel();
tmpT++;
}
}
}
void ClusterizationMethods::initializeData()
{
int objCount = X.getObjectCount();
int n = X.getObjectAt(0).getFeatureCount();
int i, j;
//count how many we have unknown classes
// TODO (Povilas#1#): move to Object matrix
for (i = 0; i < objCount; i++ )
if (X.getObjectAt(i).getClassLabel() == -1)
uknownClassObjQtty++;
learnObjQtty = objCount - uknownClassObjQtty; //set the number of available objects for learning and testing
//set dimmensions of the array n+1 = last element points to class
this->learnSet.setlength(learnObjQtty, n + 1);
DataObject tmp;
int tmpL = 0;
for (i = 0; i < objCount; i++)
{
tmp = X.getObjectAt(i);
if ( tmpL < learnObjQtty && tmp.getClassLabel() != -1)
{
for (j = 0; j < n; j++)
learnSet(tmpL,j) = tmp.getFeatureAt(j);
learnSet(tmpL,j) = tmp.getClassLabel();
tmpL++;
}
}
}