/
k_means.py
276 lines (201 loc) · 10.3 KB
/
k_means.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
# MIT License
#
# Copyright (C) IBM Corporation 2019
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
# documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
# persons to whom the Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
# Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
"""
K-means clustering algorithm satisfying differential privacy.
"""
import warnings
import numpy as np
from sklearn import cluster as skcluster
from diffprivlib.mechanisms import LaplaceBoundedDomain, GeometricFolded
from diffprivlib.models.utils import _check_bounds
from diffprivlib.utils import PrivacyLeakWarning, warn_unused_args
class KMeans(skcluster.KMeans):
r"""K-Means clustering with differential privacy.
Implements the DPLloyd approach presented in [SCL16]_, leveraging the :class:`sklearn.cluster.KMeans` class for full
integration with Scikit Learn.
Parameters
----------
epsilon : float, optional, default: 1.0
Privacy parameter :math:`\epsilon`.
bounds : list or None, optional, default: None
Bounds of the data, provided as a list of tuples, with one tuple per dimension. If not provided, the bounds
are computed on the data when ``.fit()`` is first called, resulting in a :class:`.PrivacyLeakWarning`.
n_clusters : int, optional, default: 8
The number of clusters to form as well as the number of centroids to generate.
**unused_args :
Placeholder for arguments used by :obj:`sklearn.cluster.KMeans`, but not used by `diffprivlib`. Specifying any
of these parameters will result in a :class:`.DiffprivlibCompatibilityWarning`.
Attributes
----------
cluster_centers_ : array, [n_clusters, n_features]
Coordinates of cluster centers. If the algorithm stops before fully converging, these will not be consistent
with ``labels_``.
labels_ :
Labels of each point
inertia_ : float
Sum of squared distances of samples to their closest cluster center.
n_iter_ : int
Number of iterations run.
References
----------
.. [SCL16] Su, Dong, Jianneng Cao, Ninghui Li, Elisa Bertino, and Hongxia Jin. "Differentially private k-means
clustering." In Proceedings of the sixth ACM conference on data and application security and privacy, pp. 26-37.
ACM, 2016.
"""
def __init__(self, epsilon=1.0, bounds=None, n_clusters=8, **unused_args):
super().__init__(n_clusters=n_clusters)
self.epsilon = epsilon
self.bounds = bounds
warn_unused_args(unused_args)
self.cluster_centers_ = None
self.bounds_processed = None
self.labels_ = None
self.inertia_ = None
self.n_iter_ = None
def fit(self, X, y=None, sample_weight=None):
"""Computes k-means clustering with differential privacy.
Parameters
----------
X : array-like, shape=(n_samples, n_features)
Training instances to cluster.
y : Ignored
not used, present here for API consistency by convention.
sample_weight : Ignored
Not used in diffprivlib, present here for consistency with :obj:`sklearn.cluster.KMeans`. Specifying this
parameter will result in a :class:`.DiffprivlibCompatibilityWarning`.
Returns
-------
self : class
"""
if sample_weight is not None:
warn_unused_args("sample_weight")
del y
if X.ndim != 2:
raise ValueError(
"Expected 2D array, got array with %d dimensions instead. Reshape your data using array.reshape(-1, 1),"
"or array.reshape(1, -1) if your data contains only one sample." % X.ndim)
n_samples, n_dims = X.shape
iters = self._calc_iters(n_dims, n_samples)
if self.bounds is None:
warnings.warn("Bounds have not been specified and will be calculated on the data provided. This will "
"result in additional privacy leakage. To ensure differential privacy and no additional "
"privacy leakage, specify `bounds` for each dimension.", PrivacyLeakWarning)
self.bounds = list(zip(np.min(X, axis=0), np.max(X, axis=0)))
self.bounds = _check_bounds(self.bounds, n_dims)
centers = self._init_centers(n_dims)
labels = None
distances = None
# Run _update_centers first to ensure consistency of `labels` and `centers`, since convergence unlikely
for _ in range(-1, iters):
if labels is not None:
centers = self._update_centers(X, centers=centers, labels=labels, dims=n_dims, total_iters=iters)
distances, labels = self._distances_labels(X, centers)
self.cluster_centers_ = centers
self.labels_ = labels
self.inertia_ = distances[np.arange(len(labels)), labels].sum()
self.n_iter_ = iters
return self
def _init_centers(self, dims):
if self.bounds_processed is None:
bounds_processed = np.zeros(shape=(dims, 2))
for dim in range(dims):
lower = self.bounds[dim][0]
upper = self.bounds[dim][1]
bounds_processed[dim, :] = [upper - lower, lower]
self.bounds_processed = bounds_processed
cluster_proximity = np.min(self.bounds_processed[:, 0]) / 2.0
while cluster_proximity > 0:
centers = np.zeros(shape=(self.n_clusters, dims))
cluster, retry = 0, 0
while retry < 100:
if cluster >= self.n_clusters:
break
temp_center = np.random.random(dims) * (self.bounds_processed[:, 0] - 2 * cluster_proximity) + \
self.bounds_processed[:, 1] + cluster_proximity
if cluster == 0:
centers[0, :] = temp_center
cluster += 1
continue
min_distance = ((centers[:cluster, :] - temp_center) ** 2).sum(axis=1).min()
if np.sqrt(min_distance) >= 2 * cluster_proximity:
centers[cluster, :] = temp_center
cluster += 1
retry = 0
else:
retry += 1
if cluster >= self.n_clusters:
return centers
cluster_proximity /= 2.0
return None
def _distances_labels(self, X, centers):
distances = np.zeros((X.shape[0], self.n_clusters))
for cluster in range(self.n_clusters):
distances[:, cluster] = ((X - centers[cluster, :]) ** 2).sum(axis=1)
labels = np.argmin(distances, axis=1)
return distances, labels
def _update_centers(self, X, centers, labels, dims, total_iters):
"""Updates the centers of the KMeans algorithm for the current iteration, while satisfying differential
privacy.
Differential privacy is satisfied by adding (integer-valued, using :class:`.GeometricFolded`) random noise to
the count of nearest neighbours to the previous cluster centers, and adding (real-valued, using
:class:`.LaplaceBoundedDomain`) random noise to the sum of values per dimension.
"""
epsilon_0, epsilon_i = self._split_epsilon(dims, total_iters)
geometric_mech = GeometricFolded().set_sensitivity(1).set_bounds(0.5, float("inf")).set_epsilon(epsilon_0)
laplace_mech = LaplaceBoundedDomain().set_epsilon(epsilon_i)
for cluster in range(self.n_clusters):
if cluster not in labels:
continue
cluster_count = sum(labels == cluster)
noisy_count = geometric_mech.randomise(cluster_count)
cluster_sum = np.sum(X[labels == cluster], axis=0)
# Extra np.array() a temporary fix for PyLint bug: https://github.com/PyCQA/pylint/issues/2747
noisy_sum = np.array(np.zeros_like(cluster_sum))
for i in range(dims):
laplace_mech.set_sensitivity(self.bounds[i][1] - self.bounds[i][0]) \
.set_bounds(noisy_count * self.bounds[i][0], noisy_count * self.bounds[i][1])
noisy_sum[i] = laplace_mech.randomise(cluster_sum[i])
centers[cluster, :] = noisy_sum / noisy_count
return centers
def _split_epsilon(self, dims, total_iters, rho=0.225):
"""Split epsilon between sum perturbation and count perturbation, as proposed by Su et al.
Parameters
----------
dims : int
Number of dimensions to split `epsilon` across.
total_iters : int
Total number of iterations to split `epsilon` across.
rho : float, default: 0.225
Coordinate normalisation factor.
Returns
-------
epsilon_0 : float
The epsilon value for satisfying differential privacy on the count of a cluster.
epsilon_i : float
The epsilon value for satisfying differential privacy on each dimension of the center of a cluster.
"""
epsilon_i = 1
epsilon_0 = np.cbrt(4 * dims * rho ** 2)
normaliser = self.epsilon / total_iters / (epsilon_i * dims + epsilon_0)
return epsilon_i * normaliser, epsilon_0 * normaliser
def _calc_iters(self, n_dims, n_samples, rho=0.225):
"""Calculate the number of iterations to allow for the KMeans algorithm."""
epsilon_m = np.sqrt(500 * (self.n_clusters ** 3) / (n_samples ** 2) *
(n_dims + np.cbrt(4 * n_dims * (rho ** 2))) ** 3)
iters = max(min(self.epsilon / epsilon_m, 7), 2)
return int(iters)