Class: Newral::Data::Cluster

Inherits:
Object
  • Object
show all
Defined in:
lib/newral/data/cluster.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(label: nil, points: [], center: nil, moved: true) ⇒ Cluster

Returns a new instance of Cluster.



5
6
7
8
9
10
11
# File 'lib/newral/data/cluster.rb', line 5

def initialize( label: nil, points: [], center: nil, moved: true )
  @label = label
  @points = points
  @point_size = points.size > 0 ? points.first.size : 1
  @moved = moved # did center move when updating it
  @center = center
end

Instance Attribute Details

#centerObject

Returns the value of attribute center.



4
5
6
# File 'lib/newral/data/cluster.rb', line 4

def center
  @center
end

#labelObject

Returns the value of attribute label.



4
5
6
# File 'lib/newral/data/cluster.rb', line 4

def label
  @label
end

#movedObject

Returns the value of attribute moved.



4
5
6
# File 'lib/newral/data/cluster.rb', line 4

def moved
  @moved
end

#pointsObject

Returns the value of attribute points.



4
5
6
# File 'lib/newral/data/cluster.rb', line 4

def points
  @points
end

Instance Method Details

#add_point(point) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/newral/data/cluster.rb', line 13

def add_point( point )
  if @points.size == 0
    @point_size = point.size
    @center ||= point
  else
    # all points must be of same dimension
    raise Errors::WrongPointDimension unless point.size == @point_size 
  end
  @points << point 
  self
end

#update_centerObject



25
26
27
28
29
30
31
32
33
34
# File 'lib/newral/data/cluster.rb', line 25

def update_center
  return unless @points.size > 0
  new_center = Vector.elements( [0]*points.first.size )
  @points.each do |point| 
    new_center = new_center + Vector.elements( point )
  end 
  new_center = ( new_center/@points.size.to_f).to_a
  @moved = new_center != @center
  @center = new_center
end