-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathKMEANS.cpp
More file actions
62 lines (53 loc) · 1.9 KB
/
KMEANS.cpp
File metadata and controls
62 lines (53 loc) · 1.9 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
#include "KMEANS.h"
#include "alglib/dataanalysis.h"
#include "AdditionalMethods.h"
#include <iostream>
KMEANS::KMEANS()
{
//ctor
}
KMEANS::~KMEANS()
{
//dtor
}
KMEANS::KMEANS(int noOfClusters, int maxIter):ClusterizationMethods(noOfClusters)
{
this->maxIter = maxIter;
ClusterizationMethods::setYMatrixDimensions(X.getObjectCount(), X.getObjectAt(0).getFeatureCount()); // set row feature count to 2
ClusterizationMethods::initializeYMatrix(); //fill matrix with random data
}
ObjectMatrix KMEANS::getProjection()
{
alglib::clusterizerstate s;
alglib::kmeansreport rep;
alglib::real_2d_array input;
int rowsX = X.getObjectCount(), colsX = X.getObjectAt(0).getFeatureCount();
input.setlength(rowsX, colsX);
for ( int i = 0; i < rowsX; i++ ) // convert X matrix to alglib 2d array of reals
{
DataObject tmp = X.getObjectAt(i);
for ( int j = 0; j < colsX; j++ )
{
input(i,j) = tmp.getFeatureAt(j);
}
}
alglib::clusterizercreate(s);
alglib::clusterizersetpoints(s, input, 2); //2 means Euclidean distances
alglib::clusterizersetkmeanslimits(s, 1, this->maxIter); //1 means quantity of restarts, if maxIter = 0 then unlimited number of iterations are performed
alglib::clusterizerrunkmeans(s, ClusterizationMethods::getNoOfClusters(), rep);
Y = X;
std::vector <std::string> classes; classes.reserve(0);
for (int i = 0; i < ClusterizationMethods::getNoOfClusters(); i++)
classes.push_back(std::to_string(static_cast<long long>(i)));
Y.setPrintClass(classes);
if (int(rep.terminationtype) == 1) //if success
{
for ( int i = 0; i < ClusterizationMethods::getNoOfReturnRows(); i++ ) // updates return Y matrix class values (sets to those returned by kmeans)
Y.updateDataObjectClass(i, rep.cidx(i));
}
return Y;
}
double KMEANS::getStress()
{
return 0.0;
}