-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathDenStream.py
221 lines (180 loc) · 8.06 KB
/
DenStream.py
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import sys
import numpy as np
from sklearn.utils import check_array
from copy import copy
from MicroCluster import MicroCluster
from math import ceil
from sklearn.cluster import DBSCAN
class DenStream:
def __init__(self, lambd=1, eps=1, beta=2, mu=2):
"""
DenStream - Density-Based Clustering over an Evolving Data Stream with
Noise.
Parameters
----------
lambd: float, optional
The forgetting factor. The higher the value of lambda, the lower
importance of the historical data compared to more recent data.
eps : float, optional
The maximum distance between two samples for them to be considered
as in the same neighborhood.
Attributes
----------
labels_ : array, shape = [n_samples]
Cluster labels for each point in the dataset given to fit().
Noisy samples are given the label -1.
Notes
-----
References
----------
Feng Cao, Martin Estert, Weining Qian, and Aoying Zhou. Density-Based
Clustering over an Evolving Data Stream with Noise.
"""
self.lambd = lambd
self.eps = eps
self.beta = beta
self.mu = mu
self.t = 0
self.p_micro_clusters = []
self.o_micro_clusters = []
if lambd > 0:
self.tp = ceil((1 / lambd) * np.log((beta * mu) / (beta * mu - 1)))
else:
self.tp = sys.maxsize
def partial_fit(self, X, y=None, sample_weight=None):
"""
Online learning.
Parameters
----------
X : {array-like, sparse matrix}, shape (n_samples, n_features)
Subset of training data
y : Ignored
sample_weight : array-like, shape (n_samples,), optional
Weights applied to individual samples.
If not provided, uniform weights are assumed.
Returns
-------
self : returns an instance of self.
"""
#X = check_array(X, dtype=np.float64, order="C")
n_samples, _ = X.shape
sample_weight = self._validate_sample_weight(sample_weight, n_samples)
# if not hasattr(self, "potential_micro_clusters"):
# if n_features != :
# raise ValueError("Number of features %d does not match previous "
# "data %d." % (n_features, self.coef_.shape[-1]))
for sample, weight in zip(X, sample_weight):
self._partial_fit(sample, weight)
return self
def fit_predict(self, X, y=None, sample_weight=None):
"""
Lorem ipsum dolor sit amet
Parameters
----------
X : {array-like, sparse matrix}, shape (n_samples, n_features)
Subset of training data
y : Ignored
sample_weight : array-like, shape (n_samples,), optional
Weights applied to individual samples.
If not provided, uniform weights are assumed.
Returns
-------
y : ndarray, shape (n_samples,)
Cluster labels
"""
#X = check_array(X, dtype=np.float64, order="C")
n_samples, _ = X.shape
sample_weight = self._validate_sample_weight(sample_weight, n_samples)
# if not hasattr(self, "potential_micro_clusters"):
# if n_features != :
# raise ValueError("Number of features %d does not match previous "
# "data %d." % (n_features, self.coef_.shape[-1]))
for sample, weight in zip(X, sample_weight):
self._partial_fit(sample, weight)
p_micro_cluster_centers = np.array([p_micro_cluster.center() for
p_micro_cluster in
self.p_micro_clusters])
p_micro_cluster_weights = [p_micro_cluster.weight() for p_micro_cluster in
self.p_micro_clusters]
dbscan = DBSCAN(eps=0.3, algorithm='brute')
dbscan.fit(p_micro_cluster_centers,
sample_weight=p_micro_cluster_weights)
y = []
for sample in X:
index, _ = self._get_nearest_micro_cluster(sample,
self.p_micro_clusters)
y.append(dbscan.labels_[index])
return y
def _get_nearest_micro_cluster(self, sample, micro_clusters):
smallest_distance = sys.float_info.max
nearest_micro_cluster = None
nearest_micro_cluster_index = -1
for i, micro_cluster in enumerate(micro_clusters):
current_distance = np.linalg.norm(micro_cluster.center() - sample)
if current_distance < smallest_distance:
smallest_distance = current_distance
nearest_micro_cluster = micro_cluster
nearest_micro_cluster_index = i
return nearest_micro_cluster_index, nearest_micro_cluster
def _try_merge(self, sample, weight, micro_cluster):
if micro_cluster is not None:
micro_cluster_copy = copy(micro_cluster)
micro_cluster_copy.insert_sample(sample, weight)
if micro_cluster_copy.radius() <= self.eps:
micro_cluster.insert_sample(sample, weight)
return True
return False
def _merging(self, sample, weight):
# Try to merge the sample with its nearest p_micro_cluster
_, nearest_p_micro_cluster = \
self._get_nearest_micro_cluster(sample, self.p_micro_clusters)
success = self._try_merge(sample, weight, nearest_p_micro_cluster)
if not success:
# Try to merge the sample into its nearest o_micro_cluster
index, nearest_o_micro_cluster = \
self._get_nearest_micro_cluster(sample, self.o_micro_clusters)
success = self._try_merge(sample, weight, nearest_o_micro_cluster)
if success:
if nearest_o_micro_cluster.weight() > self.beta * self.mu:
del self.o_micro_clusters[index]
self.p_micro_clusters.append(nearest_o_micro_cluster)
else:
# Create new o_micro_cluster
micro_cluster = MicroCluster(self.lambd, self.t)
micro_cluster.insert_sample(sample, weight)
self.o_micro_clusters.append(micro_cluster)
def _decay_function(self, t):
return 2 ** ((-self.lambd) * (t))
def _partial_fit(self, sample, weight):
self._merging(sample, weight)
if self.t % self.tp == 0:
self.p_micro_clusters = [p_micro_cluster for p_micro_cluster
in self.p_micro_clusters if
p_micro_cluster.weight() >= self.beta *
self.mu]
Xis = [((self._decay_function(self.t - o_micro_cluster.creation_time
+ self.tp) - 1) /
(self._decay_function(self.tp) - 1)) for o_micro_cluster in
self.o_micro_clusters]
self.o_micro_clusters = [o_micro_cluster for Xi, o_micro_cluster in
zip(Xis, self.o_micro_clusters) if
o_micro_cluster.weight() >= Xi]
self.t += 1
def _validate_sample_weight(self, sample_weight, n_samples):
"""Set the sample weight array."""
if sample_weight is None:
# uniform sample weights
sample_weight = np.ones(n_samples, dtype=np.float64, order='C')
else:
# user-provided array
sample_weight = np.asarray(sample_weight, dtype=np.float64,
order="C")
if sample_weight.shape[0] != n_samples:
raise ValueError("Shapes of X and sample_weight do not match.")
return sample_weight
#data = np.random.random([1000, 5]) * 1000
#clusterer = DenStream(lambd=0.1, eps=100, beta=0.5, mu=3)
# for row in data:
#clusterer.partial_fit([row], 1)
#print(f"Number of p_micro_clusters is {len(clusterer.p_micro_clusters)}")
#print(f"Number of o_micro_clusters is {len(clusterer.o_micro_clusters)}")