Class: KMeansPP::Centroid
- Defined in:
- lib/k_means_pp/point.rb
Overview
Centroid of a cluster.
Instance Attribute Summary collapse
-
#counter ⇒ Fixnum
How many points are in this cluster?.
Instance Method Summary collapse
-
#add(point) ⇒ Object
Add this point’s X and Y coords into the sum (for later average).
-
#average ⇒ Object
At this point X and Y properties will contain sums of all the point coords, counter will contain number of those points.
-
#initialize(point) ⇒ Centroid
constructor
Create a new centroid point.
-
#reset ⇒ Object
Prepare centroid for a new iteration, zero-ing everything.
Constructor Details
#initialize(point) ⇒ Centroid
Create a new centroid point.
66 67 68 69 |
# File 'lib/k_means_pp/point.rb', line 66 def initialize(point) self.x = point.x self.y = point.y end |
Instance Attribute Details
#counter ⇒ Fixnum
How many points are in this cluster?
61 62 63 |
# File 'lib/k_means_pp/point.rb', line 61 def counter @counter end |
Instance Method Details
#add(point) ⇒ Object
Add this point’s X and Y coords into the sum (for later average).
81 82 83 84 85 |
# File 'lib/k_means_pp/point.rb', line 81 def add(point) self.counter += 1 self.x += point.x self.y += point.y end |
#average ⇒ Object
At this point X and Y properties will contain sums of all the point coords, counter will contain number of those points. By averaging the coords we find a new center.
90 91 92 93 |
# File 'lib/k_means_pp/point.rb', line 90 def average self.x /= counter self.y /= counter end |
#reset ⇒ Object
Prepare centroid for a new iteration, zero-ing everything.
72 73 74 75 76 |
# File 'lib/k_means_pp/point.rb', line 72 def reset self.x = 0.0 self.y = 0.0 self.counter = 0 end |