Class: Kmeans::Cluster
- Inherits:
-
Object
- Object
- Kmeans::Cluster
- Defined in:
- lib/kmeans/cluster.rb
Instance Attribute Summary collapse
-
#cluster ⇒ Object
readonly
Returns the value of attribute cluster.
Instance Method Summary collapse
-
#initialize(word_counts, user_options = {}) ⇒ Cluster
constructor
A new instance of Cluster.
- #make_cluster ⇒ Object
Constructor Details
#initialize(word_counts, user_options = {}) ⇒ Cluster
Returns a new instance of Cluster.
5 6 7 8 9 10 11 12 13 14 |
# File 'lib/kmeans/cluster.rb', line 5 def initialize(word_counts, = {}) @word_counts = word_counts @min_and_max = {} @centroids = {} @cluster = Hash.new {|hash, key| hash[key] = []} @options = { :centroids => 4, :loop_max => 100 }.merge() end |
Instance Attribute Details
#cluster ⇒ Object (readonly)
Returns the value of attribute cluster.
16 17 18 |
# File 'lib/kmeans/cluster.rb', line 16 def cluster @cluster end |
Instance Method Details
#make_cluster ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/kmeans/cluster.rb', line 18 def make_cluster @min_and_max = min_and_max_in_word_counts @centroids = random_centroids loop_counter = 0 old_centroids = nil until (@centroids == old_centroids) or (@options[:loop_max] < loop_counter) loop_counter += 1 attach_keys_to_nearest_centroid old_centroids = Marshal.load(Marshal.dump(@centroids)) @centroids.each_key {|centroid| @centroids[centroid] = average_attached(centroid) if @cluster[centroid].any? } end end |