-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslkmeans.hpp
128 lines (113 loc) · 3.38 KB
/
slkmeans.hpp
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
/*
* Copyright 2024 IntelliStream team (https://github.com/intellistream)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef PDSC_SLKMEANS_HPP
#define PDSC_SLKMEANS_HPP
#include "algorithm.hpp"
#include "common.hpp"
#include <algorithm>
#include <cmath>
#include <deque>
#include <limits>
#include <random>
#include <vector>
const int WINDOW_SIZE = 1000;
class SLKMeans : public Algorithm {
public:
SLKMeans(int dimensions, int k) : dimensions(dimensions), k(k) {
centroids.resize(k, Point(dimensions));
}
void insert(const Point &point) {
window.push_back(point);
if (window.size() > WINDOW_SIZE) {
window.pop_front();
}
if (window.size() >= k) {
runKMeans();
}
}
void cluster(const std::vector<Point> &points) {
for (const auto &point : points) {
insert(point);
}
}
std::vector<Point> output_centers() { return centroids; }
private:
int dimensions;
int k;
std::vector<Point> centroids;
std::deque<Point> window;
void initializeCentroids() {
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> dis(0, window.size() - 1);
for (int i = 0; i < k; ++i) {
centroids[i] = window[dis(gen)];
}
}
void runKMeans() {
// Initialize centroids if the window has enough points
if (window.size() < k)
return;
initializeCentroids();
bool converged = false;
std::vector<int> assignments(window.size());
while (!converged) {
// Step 1: Assign points to the nearest centroid
converged = true;
for (size_t i = 0; i < window.size(); ++i) {
double minDist = std::numeric_limits<double>::max();
int bestCluster = -1;
for (int j = 0; j < k; ++j) {
double dist = calcDistance(window[i], centroids[j]);
if (dist < minDist) {
minDist = dist;
bestCluster = j;
}
}
if (assignments[i] != bestCluster) {
assignments[i] = bestCluster;
converged = false;
}
}
// Step 2: Update centroids
std::vector<Point> newCentroids(k, Point(dimensions));
std::vector<int> counts(k, 0);
for (size_t i = 0; i < window.size(); ++i) {
int cluster = assignments[i];
for (int d = 0; d < dimensions; ++d) {
newCentroids[cluster].features[d] += window[i].features[d];
}
counts[cluster]++;
}
for (int j = 0; j < k; ++j) {
if (counts[j] > 0) {
for (int d = 0; d < dimensions; ++d) {
newCentroids[j].features[d] /= counts[j];
}
}
}
centroids = newCentroids;
}
}
double calcDistance(const Point &a, const Point &b) const {
double dist = 0.0;
for (int i = 0; i < dimensions; ++i) {
dist += (a.features[i] - b.features[i]) * (a.features[i] - b.features[i]);
}
return sqrt(dist);
}
};
#endif // PDSC_SLKMEANS_HPP