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

Returns a new instance of Cluster.



101
102
103
104
105
# File 'lib/kmeans-clusterer.rb', line 101

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

Instance Attribute Details

#centroidObject (readonly)

Returns the value of attribute centroid.



98
99
100
# File 'lib/kmeans-clusterer.rb', line 98

def centroid
  @centroid
end

#idObject (readonly)

Returns the value of attribute id.



98
99
100
# File 'lib/kmeans-clusterer.rb', line 98

def id
  @id
end

#labelObject

Returns the value of attribute label.



99
100
101
# File 'lib/kmeans-clusterer.rb', line 99

def label
  @label
end

#pointsObject (readonly)

Returns the value of attribute points.



98
99
100
# File 'lib/kmeans-clusterer.rb', line 98

def points
  @points
end

Instance Method Details

#<<(point) ⇒ Object



107
108
109
110
# File 'lib/kmeans-clusterer.rb', line 107

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

#sorted_points(point = @centroid) ⇒ Object



112
113
114
115
116
117
118
# File 'lib/kmeans-clusterer.rb', line 112

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