cluster.method.kmeans

class cluster.method.kmeans.KMeansClustering(data, distance=None, equality=None)

Bases: object

Implementation of the kmeans clustering method as explained in a tutorial by matteucc.

Example:

>>> from cluster import KMeansClustering
>>> cl = KMeansClustering([(1,1), (2,1), (5,3), ...])
>>> clusters = cl.getclusters(2)
Parameters:
  • data – A list of tuples or integers.
  • distance – A function determining the distance between two items. Default (if None is passed): It assumes the tuples contain numeric values and appiles a generalised form of the euclidian-distance algorithm on them.
  • equality – A function to test equality of items. By default the standard python equality operator (==) is applied.
Raises:

ValueError – if the list contains heterogeneous items or if the distance between items cannot be determined.

assign_item(item, origin)

Assigns an item from a given cluster to the closest located cluster.

Parameters:
  • item – the item to be moved.
  • origin – the originating cluster.
getclusters(count)

Generates count clusters.

Parameters:count – The amount of clusters that should be generated. count must be greater than 1.
Raises:ClusteringError – if count is out of bounds.
initialise_clusters(input_, clustercount)

Initialises the clusters by distributing the items from the data. evenly across n clusters

Parameters:
  • input – the data set (a list of tuples).
  • clustercount – the amount of clusters (n).
move_item(item, origin, destination)

Moves an item from one cluster to anoter cluster.

Parameters:
  • item – the item to be moved.
  • origin – the originating cluster.
  • destination – the target cluster.