Class: KMeansPP::Centroid

Inherits:
BasePoint show all
Defined in:
lib/k_means_pp/point.rb

Overview

Centroid of a cluster.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(point) ⇒ Centroid

Create a new centroid point.

Parameters:

  • point (Point)

    Copy point’s X and Y coords.



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

#counterFixnum

How many points are in this cluster?

Returns:

  • (Fixnum)


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).

Parameters:



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

#averageObject

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

#resetObject

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