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

#iterations

Instance Method Summary collapse

Methods inherited from KMeans

#distance, #eval, #initialize

Methods inherited from Clusterer

#eval

Methods included from Data::Parameterizable

#get_parameters, included, #set_parameters

Constructor Details

This class inherits a constructor from Ai4r::Clusterers::KMeans

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

#distance_functionObject

Returns the value of attribute distance_function


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

def distance_function
  @distance_function
end

#max_iterationsObject

Returns the value of attribute max_iterations


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

def max_iterations
  @max_iterations
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

#refineObject

Returns the value of attribute refine


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

def refine
  @refine
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.


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

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
  
  return self
end

#intializeObject


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

def intialize
  @refine = true
end