Class: Newral::Data::ClusterSet

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cluster_labels: [], clusters: nil) ⇒ ClusterSet

Returns a new instance of ClusterSet.



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

def initialize( cluster_labels: [], clusters: nil )
  if clusters 
    idx = 0
    @clusters = clusters.inject({}){ |h,cluster| cluster.label = "cluster_#{ idx }";h[cluster.label] = cluster;idx=idx+1; h }
  else 
    @clusters = cluster_labels.inject({}){ |h,label| h[label] = Cluster.new(label: label); h }
  end
end

Instance Attribute Details

#clustersObject

Returns the value of attribute clusters.



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

def clusters
  @clusters
end

Instance Method Details

#[](label) ⇒ Object



14
15
16
# File 'lib/newral/data/cluster_set.rb', line 14

def []( label )
  label.kind_of?(String) || label.kind_of?(Symbol)  ? @clusters[ label ] : cluster_array[ label ]
end

#cluster_arrayObject



18
19
20
# File 'lib/newral/data/cluster_set.rb', line 18

def cluster_array
  @clusters.values
end

#clusters_countObject



28
29
30
31
32
33
# File 'lib/newral/data/cluster_set.rb', line 28

def clusters_count
  @clusters.inject({}) do |h,cluster|
    h[cluster[0]] = cluster[1].points.size
    h 
  end 
end

#update_centersObject



22
23
24
25
26
# File 'lib/newral/data/cluster_set.rb', line 22

def update_centers
  @clusters.each do |key,cluster| 
    cluster.update_center
  end 
end