summaryrefslogtreecommitdiff
path: root/elki/src/main/java/de/lmu/ifi/dbs/elki/algorithm/clustering/kmeans/KMeansElkan.java
blob: e2dcf88c52e1e717156c86925b2e43688bddc990 (plain)
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
package de.lmu.ifi.dbs.elki.algorithm.clustering.kmeans;

/*
 This file is part of ELKI:
 Environment for Developing KDD-Applications Supported by Index-Structures

 Copyright (C) 2015
 Ludwig-Maximilians-Universität München
 Lehr- und Forschungseinheit für Datenbanksysteme
 ELKI Development Team

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU Affero General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU Affero General Public License for more details.

 You should have received a copy of the GNU Affero General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import de.lmu.ifi.dbs.elki.algorithm.clustering.kmeans.initialization.KMeansInitialization;
import de.lmu.ifi.dbs.elki.data.Cluster;
import de.lmu.ifi.dbs.elki.data.Clustering;
import de.lmu.ifi.dbs.elki.data.NumberVector;
import de.lmu.ifi.dbs.elki.data.model.KMeansModel;
import de.lmu.ifi.dbs.elki.database.Database;
import de.lmu.ifi.dbs.elki.database.datastore.DataStoreFactory;
import de.lmu.ifi.dbs.elki.database.datastore.DataStoreUtil;
import de.lmu.ifi.dbs.elki.database.datastore.WritableDataStore;
import de.lmu.ifi.dbs.elki.database.datastore.WritableDoubleDataStore;
import de.lmu.ifi.dbs.elki.database.datastore.WritableIntegerDataStore;
import de.lmu.ifi.dbs.elki.database.ids.DBIDIter;
import de.lmu.ifi.dbs.elki.database.ids.DBIDUtil;
import de.lmu.ifi.dbs.elki.database.ids.DBIDs;
import de.lmu.ifi.dbs.elki.database.ids.ModifiableDBIDs;
import de.lmu.ifi.dbs.elki.database.relation.Relation;
import de.lmu.ifi.dbs.elki.distance.distancefunction.NumberVectorDistanceFunction;
import de.lmu.ifi.dbs.elki.distance.distancefunction.minkowski.SquaredEuclideanDistanceFunction;
import de.lmu.ifi.dbs.elki.logging.Logging;
import de.lmu.ifi.dbs.elki.logging.progress.IndefiniteProgress;
import de.lmu.ifi.dbs.elki.logging.statistics.LongStatistic;
import de.lmu.ifi.dbs.elki.logging.statistics.StringStatistic;
import de.lmu.ifi.dbs.elki.math.linearalgebra.Vector;
import de.lmu.ifi.dbs.elki.utilities.documentation.Reference;
import de.lmu.ifi.dbs.elki.utilities.optionhandling.parameterization.Parameterization;

/**
 * Elkan's fast k-means by exploiting the triangle inequality.
 *
 * This variant needs O(n*k) additional memory to store bounds.
 *
 * See {@link KMeansHamerly} for a close variant that only uses O(n*2)
 * additional memory for bounds.
 *
 * <p>
 * Reference:<br />
 * C. Elkan<br/>
 * Using the triangle inequality to accelerate k-means<br/>
 * Proc. 20th International Conference on Machine Learning, ICML 2003
 * </p>
 *
 * @author Erich Schubert
 *
 * @apiviz.has KMeansModel
 *
 * @param <V> vector datatype
 */
@Reference(authors = "C. Elkan", //
title = "Using the triangle inequality to accelerate k-means", //
booktitle = "Proc. 20th International Conference on Machine Learning, ICML 2003", //
url = "http://www.aaai.org/Library/ICML/2003/icml03-022.php")
public class KMeansElkan<V extends NumberVector> extends AbstractKMeans<V, KMeansModel> {
  /**
   * The logger for this class.
   */
  private static final Logging LOG = Logging.getLogger(KMeansElkan.class);

  /**
   * Key for statistics logging.
   */
  private static final String KEY = KMeansElkan.class.getName();

  /**
   * Constructor.
   *
   * @param distanceFunction distance function
   * @param k k parameter
   * @param maxiter Maxiter parameter
   * @param initializer Initialization method
   */
  public KMeansElkan(NumberVectorDistanceFunction<? super V> distanceFunction, int k, int maxiter, KMeansInitialization<? super V> initializer) {
    super(distanceFunction, k, maxiter, initializer);
  }

  @Override
  public Clustering<KMeansModel> run(Database database, Relation<V> relation) {
    if(relation.size() <= 0) {
      return new Clustering<>("k-Means Clustering", "kmeans-clustering");
    }
    // Choose initial means
    if(LOG.isStatistics()) {
      LOG.statistics(new StringStatistic(KEY + ".initializer", initializer.toString()));
    }
    List<Vector> means = initializer.chooseInitialMeans(database, relation, k, getDistanceFunction(), Vector.FACTORY);
    // Setup cluster assignment store
    List<ModifiableDBIDs> clusters = new ArrayList<>();
    for(int i = 0; i < k; i++) {
      clusters.add(DBIDUtil.newHashSet((int) (relation.size() * 2. / k)));
    }
    WritableIntegerDataStore assignment = DataStoreUtil.makeIntegerStorage(relation.getDBIDs(), DataStoreFactory.HINT_TEMP | DataStoreFactory.HINT_HOT, -1);
    // Elkan bounds
    WritableDoubleDataStore upper = DataStoreUtil.makeDoubleStorage(relation.getDBIDs(), DataStoreFactory.HINT_TEMP | DataStoreFactory.HINT_HOT, Double.POSITIVE_INFINITY);
    WritableDataStore<double[]> lower = DataStoreUtil.makeStorage(relation.getDBIDs(), DataStoreFactory.HINT_TEMP | DataStoreFactory.HINT_HOT, double[].class);
    for(DBIDIter it = relation.iterDBIDs(); it.valid(); it.advance()) {
      lower.put(it, new double[k]); // Filled with 0.
    }
    // Storage for updated means:
    final int dim = means.get(0).getDimensionality();
    List<Vector> sums = new ArrayList<>(k);
    for(int i = 0; i < k; i++) {
      sums.add(new Vector(dim));
    }

    // Cluster separation
    double[] sep = new double[k];
    // Cluster distances
    double[][] cdist = new double[k][k];

    IndefiniteProgress prog = LOG.isVerbose() ? new IndefiniteProgress("K-Means iteration", LOG) : null;
    LongStatistic varstat = LOG.isStatistics() ? new LongStatistic(this.getClass().getName() + ".reassignments") : null;
    int iteration = 0;
    for(; maxiter <= 0 || iteration < maxiter; iteration++) {
      LOG.incrementProcessed(prog);
      int changed;
      if(iteration == 0) {
        changed = initialAssignToNearestCluster(relation, means, sums, clusters, assignment, upper, lower);
      }
      else {
        recomputeSeperation(means, sep, cdist); // #1
        changed = assignToNearestCluster(relation, means, sums, clusters, assignment, sep, cdist, upper, lower);
      }
      if(varstat != null) {
        varstat.setLong(changed);
        LOG.statistics(varstat);
      }
      // Stop if no cluster assignment changed.
      if(changed == 0) {
        break;
      }
      // Recompute means.
      for(int i = 0; i < k; i++) {
        final int s = clusters.get(i).size();
        sums.get(i).timesEquals(s > 0 ? 1. / s : 1.);
      }
      maxMoved(means, sums, sep); // Overwrites sep
      updateBounds(relation, assignment, upper, lower, sep);
      for(int i = 0; i < k; i++) {
        final int s = clusters.get(i).size();
        means.get(i).set(sums.get(i));
        // Restore to sum for next iteration
        sums.get(i).timesEquals(s > 0 ? s : 1.);
      }
    }
    LOG.setCompleted(prog);
    if(LOG.isStatistics()) {
      LOG.statistics(new LongStatistic(KEY + ".iterations", iteration));
    }
    upper.destroy();
    lower.destroy();

    // Wrap result
    Clustering<KMeansModel> result = new Clustering<>("k-Means Clustering", "kmeans-clustering");
    for(int i = 0; i < clusters.size(); i++) {
      DBIDs ids = clusters.get(i);
      if(ids.size() == 0) {
        continue;
      }
      double varsum = 0;
      Vector mean = means.get(i);
      for(DBIDIter it = ids.iter(); it.valid(); it.advance()) {
        varsum += distanceFunction.distance(mean, relation.get(it));
      }
      KMeansModel model = new KMeansModel(mean, varsum);
      result.addToplevelCluster(new Cluster<>(ids, model));
    }
    return result;
  }

  /**
   * Recompute the separation of cluster means.
   *
   * @param means Means
   * @param sep Output array of separation
   * @param cdist Center-to-Center distances
   */
  private void recomputeSeperation(List<Vector> means, double[] sep, double[][] cdist) {
    final int k = means.size();
    assert (sep.length == k);
    boolean issquared = (distanceFunction instanceof SquaredEuclideanDistanceFunction);
    Arrays.fill(sep, Double.POSITIVE_INFINITY);
    for(int i = 1; i < k; i++) {
      Vector mi = means.get(i);
      for(int j = 0; j < i; j++) {
        double d = distanceFunction.distance(mi, means.get(j));
        d = issquared ? Math.sqrt(d) : d;
        d *= .5;
        cdist[i][j] = d;
        cdist[j][i] = d;
        sep[i] = (d < sep[i]) ? d : sep[i];
        sep[j] = (d < sep[j]) ? d : sep[j];
      }
    }
  }

  /**
   * Reassign objects, but only if their bounds indicate it is necessary to do
   * so.
   *
   * @param relation Data
   * @param means Current means
   * @param sums New means
   * @param clusters Current clusters
   * @param assignment Cluster assignment
   * @param upper Upper bounds
   * @param lower Lower bounds
   * @return Number of changes (i.e. relation size)
   */
  private int initialAssignToNearestCluster(Relation<V> relation, List<Vector> means, List<Vector> sums, List<ModifiableDBIDs> clusters, WritableIntegerDataStore assignment, WritableDoubleDataStore upper, WritableDataStore<double[]> lower) {
    assert (k == means.size());
    final NumberVectorDistanceFunction<? super V> df = getDistanceFunction();
    final boolean issquared = (df instanceof SquaredEuclideanDistanceFunction);
    for(DBIDIter it = relation.iterDBIDs(); it.valid(); it.advance()) {
      V fv = relation.get(it);
      double[] l = lower.get(it);
      // Check all (other) means:
      double best = Double.POSITIVE_INFINITY;
      int cur = -1;
      for(int j = 0; j < k; j++) {
        double dist = df.distance(fv, means.get(j));
        dist = issquared ? Math.sqrt(dist) : dist;
        l[j] = dist;
        if(dist < best) {
          cur = j;
          best = dist;
        }
      }
      // Assign to nearest cluster.
      ModifiableDBIDs newc = clusters.get(cur);
      newc.add(it);
      assignment.putInt(it, cur);
      upper.putDouble(it, best);
      double[] newmean = sums.get(cur).getArrayRef();
      for(int d = 0; d < fv.getDimensionality(); d++) {
        newmean[d] += fv.doubleValue(d);
      }
    }
    return relation.size();
  }

  /**
   * Reassign objects, but only if their bounds indicate it is necessary to do
   * so.
   *
   * @param relation Data
   * @param means Current means
   * @param sums New means
   * @param clusters Current clusters
   * @param assignment Cluster assignment
   * @param sep Separation of means
   * @param cdist Center-to-center distances
   * @param upper Upper bounds
   * @param lower Lower bounds
   * @return true when the object was reassigned
   */
  private int assignToNearestCluster(Relation<V> relation, List<Vector> means, List<Vector> sums, List<ModifiableDBIDs> clusters, WritableIntegerDataStore assignment, double[] sep, double[][] cdist, WritableDoubleDataStore upper, WritableDataStore<double[]> lower) {
    assert (k == means.size());
    final NumberVectorDistanceFunction<? super V> df = getDistanceFunction();
    final boolean issquared = (df instanceof SquaredEuclideanDistanceFunction);
    int changed = 0;
    for(DBIDIter it = relation.iterDBIDs(); it.valid(); it.advance()) {
      final int orig = assignment.intValue(it);
      double u = upper.doubleValue(it);
      // Upper bound check (#2):
      if(u <= sep[orig]) {
        continue;
      }
      boolean recompute_u = true; // Elkan's r(x)
      V fv = relation.get(it);
      double[] l = lower.get(it);
      // Check all (other) means:
      int cur = orig;
      for(int j = 0; j < k; j++) {
        if(orig == j || u <= l[j] || u <= cdist[cur][j]) {
          continue; // Condition #3 i-iii not satisfied
        }
        if(recompute_u) { // Need to update bound? #3a
          u = df.distance(fv, means.get(cur));
          u = issquared ? Math.sqrt(u) : u;
          upper.putDouble(it, u);
          recompute_u = false; // Once only
          if(u <= l[j] || u <= cdist[cur][j]) { // #3b
            continue;
          }
        }
        double dist = df.distance(fv, means.get(j));
        dist = issquared ? Math.sqrt(dist) : dist;
        l[j] = dist;
        if(dist < u) {
          cur = j;
          u = dist;
        }
      }
      // Object is to be reassigned.
      if(cur != orig) {
        upper.putDouble(it, u); // Remember bound.
        ModifiableDBIDs newc = clusters.get(cur);
        newc.add(it);
        assignment.putInt(it, cur);
        double[] newmean = sums.get(cur).getArrayRef();
        ModifiableDBIDs oldc = clusters.get(orig);
        oldc.remove(it);
        double[] oldmean = sums.get(orig).getArrayRef();
        for(int d = 0; d < fv.getDimensionality(); d++) {
          final double v = fv.doubleValue(d);
          newmean[d] += v;
          oldmean[d] -= v;
        }
        ++changed;
      }
    }
    return changed;
  }

  /**
   * Maximum distance moved.
   *
   * @param means Old means
   * @param newmeans New means
   * @param dists Distances moved
   * @return Maximum distance moved
   */
  private double maxMoved(List<Vector> means, List<Vector> newmeans, double[] dists) {
    assert (means.size() == k);
    assert (newmeans.size() == k);
    assert (dists.length == k);
    boolean issquared = (distanceFunction instanceof SquaredEuclideanDistanceFunction);
    double max = 0.;
    for(int i = 0; i < k; i++) {
      double d = distanceFunction.distance(means.get(i), newmeans.get(i));
      d = issquared ? Math.sqrt(d) : d;
      dists[i] = d;
      max = (d > max) ? d : max;
    }
    return max;
  }

  /**
   * Update the bounds for k-means.
   *
   * @param relation Relation
   * @param assignment Cluster assignment
   * @param upper Upper bounds
   * @param lower Lower bounds
   * @param move Movement of centers
   */
  private void updateBounds(Relation<V> relation, WritableIntegerDataStore assignment, WritableDoubleDataStore upper, WritableDataStore<double[]> lower, double[] move) {
    for(DBIDIter it = relation.iterDBIDs(); it.valid(); it.advance()) {
      upper.increment(it, move[assignment.intValue(it)]);
      double[] l = lower.get(it);
      for(int i = 0; i < k; i++) {
        l[i] -= move[i];
      }
    }
  }

  @Override
  protected Logging getLogger() {
    return LOG;
  }

  /**
   * Parameterization class.
   *
   * @author Erich Schubert
   *
   * @apiviz.exclude
   */
  public static class Parameterizer<V extends NumberVector> extends AbstractKMeans.Parameterizer<V> {
    @Override
    protected Logging getLogger() {
      return LOG;
    }

    @Override
    protected void getParameterDistanceFunction(Parameterization config) {
      super.getParameterDistanceFunction(config);
      if(distanceFunction instanceof SquaredEuclideanDistanceFunction) {
        return; // Proper choice.
      }
      if(distanceFunction != null && !distanceFunction.isMetric()) {
        LOG.warning("Elkan k-means requires a metric distance, and k-means should only be used with squared Euclidean distance!");
      }
    }

    @Override
    protected KMeansElkan<V> makeInstance() {
      return new KMeansElkan<>(distanceFunction, k, maxiter, initializer);
    }
  }
}