class KMeansPP::Centroid

Centroid of a cluster.

Attributes

counter[RW]

How many points are in this cluster?

@return [Fixnum]

Public Class Methods

new(point) click to toggle source

Create a new centroid point.

@param point [Point] Copy point’s X and Y coords.

# File lib/k_means_pp/point.rb, line 66
def initialize(point)
  self.x = point.x
  self.y = point.y
end

Public Instance Methods

add(point) click to toggle source

Add this point’s X and Y coords into the sum (for later average).

@param point [Point]

# 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() click to toggle source

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.

# File lib/k_means_pp/point.rb, line 90
def average
  self.x /= counter
  self.y /= counter
end
reset() click to toggle source

Prepare centroid for a new iteration, zero-ing everything.

# File lib/k_means_pp/point.rb, line 72
def reset
  self.x       = 0.0
  self.y       = 0.0
  self.counter = 0
end