Class: PairCluster

Inherits:
Object
  • Object
show all
Defined in:
lib/genevalidator/clusterization.rb

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (PairCluster) initialize(objects)

Returns a new instance of PairCluster



70
71
72
# File 'lib/genevalidator/clusterization.rb', line 70

def initialize(objects)
  @objects = objects
end

Instance Attribute Details

- (Object) objects

a hash map containing the pair (object, no_occurences)



68
69
70
# File 'lib/genevalidator/clusterization.rb', line 68

def objects
  @objects
end

Instance Method Details

- (Object) add(cluster)

Merges the current cluster with the one given as parameter clusters vector of Cluster objects



154
155
156
157
158
# File 'lib/genevalidator/clusterization.rb', line 154

def add(cluster)
  cluster.objects.each do |elem|
    objects[elem[0]] = elem[1]
  end
end

- (Object) density

Returns the density of the cluster: how many values it contains



99
100
101
102
103
104
105
106
# File 'lib/genevalidator/clusterization.rb', line 99

def density
  d = 0
  objects.each do |elem|
    d += elem[1]
  end
  d

end

- (Object) distance(cluster, method = 0)

Returns the euclidian distance between the current cluster and the one given as parameter Params: cluster: Cluster object method: 0 or 1 method = 0: do not into condseideration duplicate values method = 1: average linkage clusterization



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/genevalidator/clusterization.rb', line 114

def distance(cluster, method = 0)
  d = 0
  norm = 0

  cluster.objects.each do |elem1|
    objects.each do |elem2|
      if method == 1
        d += elem1[1] * elem2[1] * (elem1[0] - elem2[0]).abs
        norm += elem1[1] * elem2[1]
      else
        d += (elem1[0] - elem2[0]).abs
        norm = cluster.objects.length * objects.length
      end
    end
  end

  #group average distance
  d /= (norm + 0.0)

end

- (Object) mean

Returns the weighted mean value of the cluster



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/genevalidator/clusterization.rb', line 83

def mean
  mean = Pair.new(0,0)
  weight = 0

  objects.each do |object, n|
    object * n
    mean + object
    weight += n
  end    
  mean / weight
  return mean

end


74
75
76
77
78
79
# File 'lib/genevalidator/clusterization.rb', line 74

def print
  objects.each do |elem|
    puts "(#{elem[0].x},#{elem[0].y}): #{elem[1]}"
  end

end

- (Object) wss(objects = nil)

Returns within cluster sum of squares



137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/genevalidator/clusterization.rb', line 137

def wss(objects = nil)
  if objects == nil
    objects = @objects.map{|x| a = Array.new(x[1],x[0])}.flatten
  end

  cluster_mean = mean
  ss = 0
  objects.each do |object|
    ss += (cluster_mean - object) * (cluster_mean - object)
  end
  ss

end