Class: Ai4r::Clusterers::BisectingKMeans
- 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
-
#centroids ⇒ Object
readonly
Returns the value of attribute centroids.
-
#clusters ⇒ Object
readonly
Returns the value of attribute clusters.
-
#data_set ⇒ Object
readonly
Returns the value of attribute data_set.
-
#distance_function ⇒ Object
Returns the value of attribute distance_function.
-
#max_iterations ⇒ Object
Returns the value of attribute max_iterations.
-
#number_of_clusters ⇒ Object
readonly
Returns the value of attribute number_of_clusters.
-
#refine ⇒ Object
Returns the value of attribute refine.
Attributes inherited from KMeans
Instance Method Summary collapse
-
#build(data_set, number_of_clusters) ⇒ Object
Build a new clusterer, using data examples found in data_set.
- #intialize ⇒ Object
Methods inherited from KMeans
Methods inherited from Clusterer
Methods included from Data::Parameterizable
#get_parameters, included, #set_parameters
Constructor Details
This class inherits a constructor from Ai4r::Clusterers::KMeans
Instance Attribute Details
#centroids ⇒ Object (readonly)
Returns the value of attribute centroids.
24 25 26 |
# File 'lib/ai4r/clusterers/bisecting_k_means.rb', line 24 def centroids @centroids end |
#clusters ⇒ Object (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_set ⇒ Object (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_function ⇒ Object
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_iterations ⇒ Object
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_clusters ⇒ Object (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 |
#refine ⇒ Object
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 |
#intialize ⇒ Object
44 45 46 |
# File 'lib/ai4r/clusterers/bisecting_k_means.rb', line 44 def intialize @refine = true end |