Class: KMeansClusterer::Cluster
- Inherits:
-
Object
- Object
- KMeansClusterer::Cluster
- Defined in:
- lib/kmeans-clusterer.rb
Instance Attribute Summary collapse
-
#centroid ⇒ Object
readonly
Returns the value of attribute centroid.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#label ⇒ Object
Returns the value of attribute label.
-
#points ⇒ Object
readonly
Returns the value of attribute points.
Instance Method Summary collapse
- #<<(point) ⇒ Object
-
#initialize(id, centroid) ⇒ Cluster
constructor
A new instance of Cluster.
- #sorted_points(point = @centroid) ⇒ Object
Constructor Details
#initialize(id, centroid) ⇒ Cluster
Returns a new instance of Cluster.
97 98 99 100 101 |
# File 'lib/kmeans-clusterer.rb', line 97 def initialize id, centroid @id = id @centroid = centroid @points = [] end |
Instance Attribute Details
#centroid ⇒ Object (readonly)
Returns the value of attribute centroid.
94 95 96 |
# File 'lib/kmeans-clusterer.rb', line 94 def centroid @centroid end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
94 95 96 |
# File 'lib/kmeans-clusterer.rb', line 94 def id @id end |
#label ⇒ Object
Returns the value of attribute label.
95 96 97 |
# File 'lib/kmeans-clusterer.rb', line 95 def label @label end |
#points ⇒ Object (readonly)
Returns the value of attribute points.
94 95 96 |
# File 'lib/kmeans-clusterer.rb', line 94 def points @points end |
Instance Method Details
#<<(point) ⇒ Object
103 104 105 106 |
# File 'lib/kmeans-clusterer.rb', line 103 def << point point.cluster = self @points << point end |
#sorted_points(point = @centroid) ⇒ Object
108 109 110 111 112 113 114 |
# File 'lib/kmeans-clusterer.rb', line 108 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 |