Class: KMeans::Centroid
Instance Attribute Summary collapse
-
#position ⇒ Object
readonly
The position of the centroid, or a value for every dimension.
Instance Method Summary collapse
- #add_nodes(*new_nodes) ⇒ Object (also: #add_node)
-
#initialize(position) ⇒ Centroid
constructor
A new instance of Centroid.
- #inspect ⇒ Object
- #nodes ⇒ Object
-
#rebalance ⇒ Object
Finds a new centroid based on the nodes currently attached to it.
- #remove_nodes(*nodes) ⇒ Object (also: #remove_node)
Constructor Details
#initialize(position) ⇒ Centroid
Returns a new instance of Centroid.
7 8 9 |
# File 'lib/kmeans/centroid.rb', line 7 def initialize(position) @position = position end |
Instance Attribute Details
#position ⇒ Object (readonly)
The position of the centroid, or a value for every dimension
5 6 7 |
# File 'lib/kmeans/centroid.rb', line 5 def position @position end |
Instance Method Details
#add_nodes(*new_nodes) ⇒ Object Also known as: add_node
15 16 17 18 19 20 |
# File 'lib/kmeans/centroid.rb', line 15 def add_nodes(*new_nodes) new_nodes.each do |node| self.nodes << node node.centroid = self end end |
#inspect ⇒ Object
40 41 42 |
# File 'lib/kmeans/centroid.rb', line 40 def inspect "KMeans::Centroid:#{self.position.inspect}" end |
#nodes ⇒ Object
11 12 13 |
# File 'lib/kmeans/centroid.rb', line 11 def nodes @nodes ||= [] end |
#rebalance ⇒ Object
Finds a new centroid based on the nodes currently attached to it
32 33 34 35 36 37 38 |
# File 'lib/kmeans/centroid.rb', line 32 def rebalance return true if nodes.empty? size = nodes.first. position.size @position = (0...size).map do |i| self.nodes.map { |e| e. position[i] }.mean end end |
#remove_nodes(*nodes) ⇒ Object Also known as: remove_node
23 24 25 26 27 28 |
# File 'lib/kmeans/centroid.rb', line 23 def remove_nodes(*nodes) nodes.each do |node| self.nodes.delete(node) node.centroid = nil end end |