Class: Cluster1D
- Inherits:
-
Object
- Object
- Cluster1D
- Defined in:
- lib/jkr/stat/kmeans-1d.rb
Instance Attribute Summary collapse
-
#center ⇒ Object
Returns the value of attribute center.
-
#points ⇒ Object
Returns the value of attribute points.
Instance Method Summary collapse
-
#initialize(center) ⇒ Cluster1D
constructor
Constructor with a starting centerpoint.
-
#recenter! ⇒ Object
Recenters the centroid point and removes all of the associated points.
Constructor Details
#initialize(center) ⇒ Cluster1D
Constructor with a starting centerpoint
6 7 8 9 |
# File 'lib/jkr/stat/kmeans-1d.rb', line 6 def initialize(center) @center = center @points = [] end |
Instance Attribute Details
#center ⇒ Object
Returns the value of attribute center.
3 4 5 |
# File 'lib/jkr/stat/kmeans-1d.rb', line 3 def center @center end |
#points ⇒ Object
Returns the value of attribute points.
3 4 5 |
# File 'lib/jkr/stat/kmeans-1d.rb', line 3 def points @points end |
Instance Method Details
#recenter! ⇒ Object
Recenters the centroid point and removes all of the associated points
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/jkr/stat/kmeans-1d.rb', line 12 def recenter! avg = 0 old_center = @center # Sum up all x/y coords @points.each do |point| avg += point end # Average out data if points.length == 0 avg = center else avg /= points.length end # Reset center and return distance moved @center = avg return (old_center - center).abs end |