forked from issamemari/DenStream
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMicroCluster.py
More file actions
54 lines (44 loc) · 1.73 KB
/
MicroCluster.py
File metadata and controls
54 lines (44 loc) · 1.73 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
import numpy as np
class MicroCluster:
def __init__(self, lambd, creation_time):
self.lambd = lambd
self.decay_factor = 2 ** (-lambd)
self.mean = 0
self.variance = 0
self.sum_of_weights = 0
self.creation_time = creation_time
def insert_sample(self, sample, weight):
if self.sum_of_weights != 0:
# Update sum of weights
old_sum_of_weights = self.sum_of_weights
new_sum_of_weights = old_sum_of_weights * self.decay_factor + weight
# Update mean
old_mean = self.mean
new_mean = old_mean + \
(weight / new_sum_of_weights) * (sample - old_mean)
# Update variance
old_variance = self.variance
new_variance = old_variance * ((new_sum_of_weights - weight)
/ old_sum_of_weights) \
+ weight * (sample - new_mean) * (sample - old_mean)
self.mean = new_mean
self.variance = new_variance
self.sum_of_weights = new_sum_of_weights
else:
self.mean = sample
self.sum_of_weights = weight
def radius(self):
if self.sum_of_weights > 0:
return np.linalg.norm(np.sqrt(self.variance / self.sum_of_weights))
else:
return float('nan')
def center(self):
return self.mean
def weight(self):
return self.sum_of_weights
def __copy__(self):
new_micro_cluster = MicroCluster(self.lambd, self.creation_time)
new_micro_cluster.sum_of_weights = self.sum_of_weights
new_micro_cluster.variance = self.variance
new_micro_cluster.mean = self.mean
return new_micro_cluster