Class: Ai4r::Clusterers::BisectingKMeans

Inherits:
KMeans show all
Defined in:
lib/ai4r/clusterers/bisecting_k_means.rb

Overview

The Bisecting k-means algorithm is a variation of the “k-means” algorithm, somewhat less sensitive to the initial election of centroids than the original.

More about K Means algorithm: en.wikipedia.org/wiki/K-means_algorithm

Instance Attribute Summary collapse

Attributes inherited from KMeans

#history, #iterations

Instance Method Summary collapse

Methods inherited from KMeans

#distance, #eval, #sse

Methods inherited from Clusterer

#eval, #supports_eval?

Methods included from Data::Parameterizable

#get_parameters, included, #set_parameters

Constructor Details

#initializeObject



43
44
45
46
# File 'lib/ai4r/clusterers/bisecting_k_means.rb', line 43

def initialize
  super
  @refine = true
end

Instance Attribute Details

#centroidsObject (readonly)

Returns the value of attribute centroids.



24
25
26
# File 'lib/ai4r/clusterers/bisecting_k_means.rb', line 24

def centroids
  @centroids
end

#clustersObject (readonly)

Returns the value of attribute clusters.



24
25
26
# File 'lib/ai4r/clusterers/bisecting_k_means.rb', line 24

def clusters
  @clusters
end

#data_setObject (readonly)

Returns the value of attribute data_set.



24
25
26
# File 'lib/ai4r/clusterers/bisecting_k_means.rb', line 24

def data_set
  @data_set
end

#number_of_clustersObject (readonly)

Returns the value of attribute number_of_clusters.



24
25
26
# File 'lib/ai4r/clusterers/bisecting_k_means.rb', line 24

def number_of_clusters
  @number_of_clusters
end

Instance Method Details

#build(data_set, number_of_clusters) ⇒ Object

Build a new clusterer, using data examples found in data_set. Items will be clustered in “number_of_clusters” different clusters.

Parameters:

  • data_set (Object)
  • number_of_clusters (Object)

Returns:

  • (Object)


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/ai4r/clusterers/bisecting_k_means.rb', line 54

def build(data_set, number_of_clusters)
  @data_set = data_set
  @number_of_clusters = number_of_clusters

  @clusters = [@data_set]
  @centroids = [@data_set.get_mean_or_mode]
  while @clusters.length < @number_of_clusters
    biggest_cluster_index = find_biggest_cluster_index(@clusters)
    clusterer = KMeans.new
                      .set_parameters(get_parameters)
                      .build(@clusters[biggest_cluster_index], 2)
    @clusters.delete_at(biggest_cluster_index)
    @centroids.delete_at(biggest_cluster_index)
    @clusters.concat(clusterer.clusters)
    @centroids.concat(clusterer.centroids)
  end

  super if @refine

  self
end