Class: Newral::Classifier::Node
- Inherits:
-
Object
- Object
- Newral::Classifier::Node
- Defined in:
- lib/newral/classifier/node.rb
Instance Attribute Summary collapse
-
#center ⇒ Object
Returns the value of attribute center.
-
#parent_node ⇒ Object
writeonly
Sets the attribute parent_node.
-
#sub_nodes ⇒ Object
readonly
Returns the value of attribute sub_nodes.
Instance Method Summary collapse
- #flatten_points ⇒ Object
-
#initialize(sub_nodes, from_point: false) ⇒ Node
constructor
A new instance of Node.
- #to_cluster ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(sub_nodes, from_point: false) ⇒ Node
Returns a new instance of Node.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/newral/classifier/node.rb', line 11 def initialize( sub_nodes, from_point: false ) if from_point @sub_nodes = [Vector.elements( sub_nodes )] @center = Vector.elements sub_nodes else @sub_nodes = sub_nodes @center = Vector.elements( [0]*sub_nodes.first.center.size ) sub_nodes.each do |node| @center = @center + node.center end @center = @center/@sub_nodes.size.to_f @sub_nodes.each do |node| node.parent_node = self end end @parent_node = nil end |
Instance Attribute Details
#center ⇒ Object
Returns the value of attribute center.
9 10 11 |
# File 'lib/newral/classifier/node.rb', line 9 def center @center end |
#parent_node=(value) ⇒ Object (writeonly)
Sets the attribute parent_node
8 9 10 |
# File 'lib/newral/classifier/node.rb', line 8 def parent_node=(value) @parent_node = value end |
#sub_nodes ⇒ Object (readonly)
Returns the value of attribute sub_nodes.
10 11 12 |
# File 'lib/newral/classifier/node.rb', line 10 def sub_nodes @sub_nodes end |
Instance Method Details
#flatten_points ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/newral/classifier/node.rb', line 38 def flatten_points @sub_nodes.collect do |node| if !node.kind_of?( Node ) [node] elsif node.sub_nodes.size == 1 node.center else node.flatten_points end end.flatten end |
#to_cluster ⇒ Object
50 51 52 53 |
# File 'lib/newral/classifier/node.rb', line 50 def to_cluster points = flatten_points Data::Cluster.new( points: points.collect{|p| p.to_a } ) end |
#to_s ⇒ Object
30 31 32 33 34 35 36 |
# File 'lib/newral/classifier/node.rb', line 30 def to_s if @sub_nodes.size == 1 @sub_nodes.first.to_s else "=>(#{@sub_nodes.collect{|node| node.to_s }.join(',')})" end end |