Class: Cluster1D

Inherits:
Object
  • Object
show all
Defined in:
lib/jkr/stat/kmeans-1d.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#centerObject

Returns the value of attribute center.



3
4
5
# File 'lib/jkr/stat/kmeans-1d.rb', line 3

def center
  @center
end

#pointsObject

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