forked from BobWang21/cluster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
feature_weighting_kmeans.py
336 lines (263 loc) · 10 KB
/
feature_weighting_kmeans.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Feature Weighted Kmeans
the weight is fixed!
More see
"""
import numpy as np
from k_init import _k_init
from sklearn.utils import check_random_state
from sklearn.utils.extmath import squared_norm
def _weightmatrix(k, m):
"""
`matrix`: array, [k, m]
"""
matrix = np.random.rand(k, m)
matrix = matrix / matrix.sum(axis=1)[:, None]
return matrix
def kmeans_to_center_distance(X, center, weights, m):
"""Squared Euclidean distance
Parameters
-----------
`X`: shape = [n_sample, m_features]
`center`: shape = [m_features, ]
`weights`: shape = [m_features]
`m`: float
Returns
-----------
distance: shape = [n_sample, ]
n sample's Squared Euclidean distance between certain cluster
"""
return np.dot((X - center) ** 2, (weights ** m).T)
def kmedian_to_center_distance(X, center, weights, m):
"""Manhattan distance
Parameters
-----------
X: [n_sample, m_features]
center: [m_features,]
weights: [m_features,]
m: float
Returns
distance: shape = [n_sample, ]
n sample's Manhattan distance between certain cluster
-----------
"""
return np.dot(np.abs(X - center), (weights ** m).T)
def kmeans_center(X):
"""
Parameters
-----------
X: [n_sample, m_features]
"""
return np.mean(X, axis=0)
def kmedian_center(X):
"""
Parameters
-----------
X: [n_sample, m_features]
"""
return np.median(X, axis=0)
class FeatureWeightingKMeans():
"""
Parameters
----------
n_clusters: int
The number of clusters
weights_: np.ndarray or list, [n_features]
weight parameter, m >=1 The closer to m is to 1, the closter to hard kmeans.
cluster_method: {'kmeans, kmedian'}
init_center: {'random', 'k-means++', narray}
init center method
init_weight: {'random', 'fixed', narray}
m: float, default m=2
weight parameter, m >=1 The closer to m is to 1, the closter to hard kmeans.
max_iter: int
Maximum number of iterations of the k-means algorithm for a single run.
tot: float
Relative tolerance with regards to inertia to declare convergence
random_state : integer or numpy.RandomState, optional
The generator used to initialize the centers. If an integer is
given, it fixes the seed. Defaults to the global numpy random
number generator.
verbose: True or False
if True, print processing infomation
Attributes
----------
cluster_centers_ : array, [n_clusters, n_features]
Coordinates of cluster centers
labels_ :
labels of each point
"""
def __init__(self, n_clusters, m=2,
cluster_method='k-median',
init_weight='random',
init_center='k-means++',
max_iter=300,
random_state=0,
tol=1e-6,
verbose=False):
self.n_clusters = n_clusters
assert m >= 1, 'm cannot be less than 1'
self.m = m
self.cluster_method = cluster_method
self.init_weight = init_weight
self.init_center = init_center
self.max_iter = max_iter
self.random_state = random_state
self.tol = tol
self.verbose = verbose
def _update_label(self, X):
"""
Fix Center, Weight, Update Label
"""
n_samples, m_features = X.shape
assert m_features == len(self.weights_)
# Choose cluster method
if self.cluster_method == 'k-means':
to_center = kmeans_to_center_distance
elif self.cluster_method == 'k-median':
to_center = kmedian_to_center_distance
else:
raise ValueError('cluster_method must be kmeans or kmedian')
# Distance between X and all cluster center
affiliation = np.zeros((n_samples, self.n_clusters))
# Calculate distance between data and center i
for i, center in enumerate(self.cluster_centers_):
center_i_dist = to_center(X, center, self.weights_, self.m)
affiliation[:, i] = center_i_dist.T
# Set label to closest cluster
self.labels_ = np.argmin(affiliation, axis=1)
def _update_center(self, X):
"""
Fix Label, Weight, Update Center
"""
centers_old = self.cluster_centers_.copy()
if self.cluster_method == 'k-means':
cluster_center = kmeans_center
elif self.cluster_method == 'k-median':
cluster_center = kmedian_center
else:
raise ValueError('cluster_method must be kmeans or kmedian')
# Choose data belong to cluster k and
# Update cluster center with it mean
for k in range(self.n_clusters):
mask = self.labels_ == k
self.cluster_centers_[k] = cluster_center(X[mask])
# check cluster is empty
if np.isnan(self.cluster_centers_).any():
raise ValueError('Cluster must have at least one member')
center_shift_total = squared_norm(self.cluster_centers_ - centers_old)
return center_shift_total
def _update_weight(self, X):
"""
Fix Label, Center, Update Weight
"""
n_samples, m_features = X.shape
D = np.zeros(m_features)
for feature_id in range(m_features):
feature_var = 0
for cluster_id in range(self.n_clusters):
mask = self.labels == cluster_id
feature_cluster_var = X[mask][:, feature_id] - self.cluster_centers_[cluster_id:, feature_id]
feature_var += feature_cluster_var
D[feature_id] = feature_var
beta = 1 / (self.m - 1)
mask = D != 0
C = D[mask]
# weights_ = 1 / (D ** beta * np.sum((1 / D) ** beta))
weights_ = 1 / (C ** beta * np.sum((1 / C) ** beta))
D[mask] = weights_
self.weights_ = D
def fit(self, X, y=None):
"""Predict the closest cluster each sample in X belongs to.
Parameters
----------
X : array-like, shape = [n_samples, n_features]
New data to predict.
Returns
-------
labels : array, shape = [n_samples,]
Index of the cluster each sample belongs to.
"""
n_samples, m_features = X.shape
# variance
variances = np.mean(np.var(X, 0))
self.tol *= variances
# Initialize weight matrix
if hasattr(self.init_weight, '__array__'):
print(self.init_weight)
self.weights_ = self.init_weight
elif self.init_weight == 'random':
self.weights_ = _weightmatrix(self.n_clusters, m_features)
elif self.init_weight == 'fixed':
self.weights_ = np.ones((self.n_clusters, m_features)) * (1 / m_features)
else:
raise Exception('init_weight_must be `random` , `fixed` or numpy.array')
# Initialize center
# need robust data check
if hasattr(self.init_center, '__array__'):
self.cluster_centers_ = self.init_center
elif self.init_center == 'k-means++':
random_state = check_random_state(self.random_state)
self.cluster_centers_ = _k_init(X=X, n_clusters=self.n_clusters, random_state=random_state)
elif self.init_center == 'random':
random_state = check_random_state(self.random_state)
chosen_ids = random_state.permutation(n_samples)[:self.n_clusters]
self.cluster_centers_ = X[chosen_ids]
else:
raise Exception('init_center must be `random`, `kmeans++` or np.array')
if self.verbose:
print('origin center_ \n', self.cluster_centers_)
# Iteration
for i in range(self.max_iter):
# update label
self._update_label(X)
# update center
center_shift_total = self._update_center(X)
# if weight is fixed continue
if self.init_weight in ['random', 'fixed']:
self._update_weight()
if self.verbose:
print('Iteration %i cluster_centers_\n' % i, self.cluster_centers_)
print('Iteration %i tolerance: ' % i, center_shift_total)
if center_shift_total < self.tol:
break
def predict(self, X):
"""Predict the closest cluster each sample in X belongs to.
Parameters
----------
X : array-like, shape = [n_samples, n_features]
New data to predict.
Returns
-------
labels : array, shape = [n_samples,]
Index of the cluster each sample belongs to.
"""
# Check array
n_samples, dim = X.shape
m_features = self.cluster_centers_.shape[1]
if self.cluster_method == 'kmeans':
to_center = kmeans_to_center_distance
elif self.cluster_method == 'kmedian':
to_center = kmedian_to_center_distance
else:
raise ValueError('cluster_method must be kmeans or kmedian')
if self.cluster_centers_ is None:
self.fit(X)
return self.labels_
elif m_features == dim:
affiliation = np.zeros((n_samples, self.n_clusters))
# Calculate distance between data and center i
for i, center in enumerate(self.cluster_centers_):
center_i_dist = to_center(X, center, self.weights_, self.m)
if self.verbose:
print(center_i_dist)
affiliation[:, i] = center_i_dist.T
# Set min distance () be the arbitrary cluster label
labels_ = np.argmin(affiliation, axis=1)
return labels_
else:
raise ValueError('The features of the X %i'
'does not match the number of clusters %i'
% (dim, m_features))