java.lang.Object
es.uam.eps.ir.relison.sna.community.clustering.KMeans

public class KMeans
extends java.lang.Object
Implementation of the k-means clustering algorithm.

Reference: D. MacKay. An example inference task: clustering. Information Theory, Inference and Learning Algorithms, Chapter 20, pp. 284-292 (2003)

  • Field Summary

    Fields 
    Modifier and Type Field Description
    private int k
    The number of desired clusters.
    private static double THRESHOLD
    Variation threshold.
  • Constructor Summary

    Constructors 
    Constructor Description
    KMeans​(int k)
    Constructor.
  • Method Summary

    Modifier and Type Method Description
    Communities<java.lang.Integer> cluster​(java.util.List<cern.colt.matrix.DoubleMatrix1D> vectors, int length, boolean normalize)
    Obtains a partition of a group of examples in k groups.
    java.util.List<cern.colt.matrix.DoubleMatrix1D> normalize​(java.util.List<cern.colt.matrix.DoubleMatrix1D> vectors, int length)
    Given a group of vectors, normalizes them, so each coordinate has mean equal to 0 and variance equal to 1.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • THRESHOLD

      private static final double THRESHOLD
      Variation threshold.
      See Also:
      Constant Field Values
    • k

      private final int k
      The number of desired clusters.
  • Constructor Details

    • KMeans

      public KMeans​(int k)
      Constructor.
      Parameters:
      k - The number of desired clusters.
  • Method Details

    • cluster

      public Communities<java.lang.Integer> cluster​(java.util.List<cern.colt.matrix.DoubleMatrix1D> vectors, int length, boolean normalize)
      Obtains a partition of a group of examples in k groups.
      Parameters:
      vectors - The examples.
      length - Length of the vectors.
      normalize - True if the vectors have to be normalized, false if not.
      Returns:
      The partition of the examples in groups. If k is smaller than 1, an empty set of communities is returned.
    • normalize

      public java.util.List<cern.colt.matrix.DoubleMatrix1D> normalize​(java.util.List<cern.colt.matrix.DoubleMatrix1D> vectors, int length)
      Given a group of vectors, normalizes them, so each coordinate has mean equal to 0 and variance equal to 1.
      Parameters:
      vectors - The vectors to normalize.
      length - The length of the vectors.
      Returns:
      the normalized vectors.