Class: Newral::Classifier::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/newral/classifier/node.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#centerObject

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

Parameters:

  • value

    the value to set the attribute parent_node to.



8
9
10
# File 'lib/newral/classifier/node.rb', line 8

def parent_node=(value)
  @parent_node = value
end

#sub_nodesObject (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_pointsObject



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_clusterObject



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_sObject



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