Class: KMeansClusterer::Cluster

Inherits:
Object
  • Object
show all
Defined in:
lib/kmeans-clusterer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, centroid) ⇒ Cluster



85
86
87
88
89
# File 'lib/kmeans-clusterer.rb', line 85

def initialize id, centroid
  @id = id
  @centroid = centroid
  @points = []
end

Instance Attribute Details

#centroidObject (readonly)

Returns the value of attribute centroid.



82
83
84
# File 'lib/kmeans-clusterer.rb', line 82

def centroid
  @centroid
end

#idObject (readonly)

Returns the value of attribute id.



82
83
84
# File 'lib/kmeans-clusterer.rb', line 82

def id
  @id
end

#labelObject

Returns the value of attribute label.



83
84
85
# File 'lib/kmeans-clusterer.rb', line 83

def label
  @label
end

#pointsObject (readonly)

Returns the value of attribute points.



82
83
84
# File 'lib/kmeans-clusterer.rb', line 82

def points
  @points
end

Instance Method Details

#<<(point) ⇒ Object



91
92
93
94
# File 'lib/kmeans-clusterer.rb', line 91

def << point
  point.cluster = self
  @points << point
end

#sorted_points(point = @centroid) ⇒ Object



96
97
98
99
100
101
102
# File 'lib/kmeans-clusterer.rb', line 96

def sorted_points point = @centroid
  point = point.data if point.is_a?(Point)
  point = NArray.cast(point, @centroid.typecode) unless point.is_a?(NArray)
  points_data = NArray.cast(@points.map(&:data))
  distances = Distance.euclidean(points_data, point)
  @points.sort_by.with_index {|p, i| distances[i] }
end