Class: KMeansPP::Cluster

Inherits:
Object
  • Object
show all
Defined in:
lib/k_means_pp/cluster.rb

Overview

Cluster has a centroid and a group of related points.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(centroid, points = []) ⇒ Cluster

Create a new cluster with a centroid and points.

Parameters:

  • centroid (Centroid)

    Center point of the data set.

  • points (Array<Point>) (defaults to: [])

    Points in this cluster.



18
19
20
21
# File 'lib/k_means_pp/cluster.rb', line 18

def initialize(centroid, points = [])
  self.centroid = centroid
  self.points   = points
end

Instance Attribute Details

#centroidCentroid

Center of the data set (“centroid”).

Returns:



7
8
9
# File 'lib/k_means_pp/cluster.rb', line 7

def centroid
  @centroid
end

#pointsArray<Point>

Points in this cluster.

Returns:



12
13
14
# File 'lib/k_means_pp/cluster.rb', line 12

def points
  @points
end

Instance Method Details

#to_sObject

A string representation of the cluster.



24
25
26
27
28
29
30
# File 'lib/k_means_pp/cluster.rb', line 24

def to_s
  o = ''
  o << "Cluster #{centroid}: [\n"
  points.each { |p| o << "  #{p},\n" }
  o << "]\n"
  o
end