Class: Cluster::AddC
- Inherits:
-
Object
- Object
- Cluster::AddC
- Defined in:
- lib/lite/cluster.rb
Instance Method Summary collapse
- #centroids(min_num_instances_in_cluster = 2) ⇒ Object
-
#initialize(upperBoundOnNumClusters) ⇒ AddC
constructor
A new instance of AddC.
- #observe!(instance) ⇒ Object
Constructor Details
#initialize(upperBoundOnNumClusters) ⇒ AddC
Returns a new instance of AddC.
5 6 7 8 |
# File 'lib/lite/cluster.rb', line 5 def initialize( upperBoundOnNumClusters ) @k_max = upperBoundOnNumClusters @centroids = [] end |
Instance Method Details
#centroids(min_num_instances_in_cluster = 2) ⇒ Object
45 46 47 48 49 50 51 52 53 54 |
# File 'lib/lite/cluster.rb', line 45 def centroids( min_num_instances_in_cluster = 2 ) @centroids.each do |c| next if c.n >= min_num_instances_in_cluster @centroids = @centroids - [ c ] next if c.n == 0 aux = @centroids.inject( {:min_c => @centroids.first, :d => @centroids.first.x.dist( c.x )} ) {|a,cc| cc.nil? || cc.x.dist(c.x) > a[:d] ? a : { :min_c=>cc, :d=>cc.x.dist(c.x)} } aux[:min_c].merge! c end @centroids end |
#observe!(instance) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/lite/cluster.rb', line 10 def observe!( instance ) u = SparseVector.new instance if @centroids.size == 0 @centroids << Centroid.new( u ) return self end @centroids.sort! {|c1, c2| u.dist(c1.x) <=> u.dist(c2.x) } closest_centroid = @centroids.first closest_centroid.update!( u ) if( @centroids.size >= @k_max ) pairs = [] @centroids.each_index do |i| min_d = 10**20 min_c = 0 @centroids.each_index do |j| next if i==j d = @centroids[i].x.dist( @centroids[j].x ) min_c = j if d < min_d min_d = d if d < min_d end pairs[i] = [ min_d, i, min_c] end pairs.sort! {|x,y| x[0]<=>y[0]} merge_info = pairs.first @centroids[merge_info[1]].merge!( @centroids[merge_info[2]] ) @centroids = @centroids - [ @centroids[merge_info[2]] ] end @centroids << Centroid.new( u ) [] end |