Module: Enumerable

Defined in:
lib/everyday-cli-utils/kmeans.rb,
lib/everyday-cli-utils/maputil.rb,
lib/everyday-cli-utils/histogram.rb

Instance Method Summary collapse

Instance Method Details

#averageObject



18
19
20
# File 'lib/everyday-cli-utils/maputil.rb', line 18

def average
  sum.to_f / count.to_f
end

#chompallObject



41
42
43
# File 'lib/everyday-cli-utils/maputil.rb', line 41

def chompall
  map(&:chomp)
end

#filtermap(&block) ⇒ Object



6
7
8
# File 'lib/everyday-cli-utils/maputil.rb', line 6

def filtermap(&block)
  map(&block).removefalse
end

#find_outliers(avg, cs, i, sensitivity) ⇒ Object



110
111
112
113
114
115
# File 'lib/everyday-cli-utils/kmeans.rb', line 110

def find_outliers(avg, cs, i, sensitivity)
  csi = cs[i]
  std = csi.std_dev
  cnt = csi.count
  csi.select { |c| (EverydayCliUtils::Kmeans.normal(c, avg, std) * cnt) < sensitivity }
end

#floatsObject



29
30
31
# File 'lib/everyday-cli-utils/maputil.rb', line 29

def floats
  map(&:to_f)
end

#get_clusters(means) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/everyday-cli-utils/kmeans.rb', line 138

def get_clusters(means)
  clusters = Array.new(means.count) { Array.new }
  each { |item|
    cluster  = false
    distance = false
    (0...means.count).each { |i|
      diff = (means[i] - item).abs
      if distance == false || diff < distance
        cluster  = i
        distance = diff
      end
    }
    clusters[cluster][clusters[cluster].count] = item
  }
  clusters
end

#histogram(ks = nil, width = 100, height = 50) ⇒ Object



37
38
39
40
41
42
# File 'lib/everyday-cli-utils/histogram.rb', line 37

def histogram(ks = nil, width = 100, height = 50)
  counts, lines, max_y, mi, step = EverydayCliUtils::Histogram.setup(self, height, width)
  EverydayCliUtils::Histogram.add_graph(counts, height, lines, max_y, width)
  EverydayCliUtils::Histogram.add_averages(height, ks, lines, mi, step, width) unless ks.nil?
  lines
end

#join(join_str) ⇒ Object



45
46
47
# File 'lib/everyday-cli-utils/maputil.rb', line 45

def join(join_str)
  map(&:to_s).reduce { |a, b| a << join_str << b }
end

#kmeans(k) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/everyday-cli-utils/kmeans.rb', line 125

def kmeans(k)
  mi   = min
  ma   = max
  diff = ma - mi
  ks   = []
  (1..k).each { |i| ks[i - 1] = mi + (i * (diff / (k + 1.0))) }
  kso = false
  while ks != kso
    kso, ks = EverydayCliUtils::Kmeans.run_kmean(self, ks)
  end
  ks
end

#nmeans(max_k = 10, threshold = 0.05) ⇒ Object



117
118
119
120
121
122
123
# File 'lib/everyday-cli-utils/kmeans.rb', line 117

def nmeans(max_k = 10, threshold = 0.05)
  collection    = self.floats
  avg, cnt, ks1 = EverydayCliUtils::Kmeans.nmeans_setup_1(collection)
  return ks1 if cnt == 1
  ft, ft1, ft2, ks = EverydayCliUtils::Kmeans.nmeans_setup_2(collection, avg, cnt, ks1)
  EverydayCliUtils::Kmeans.run_nmeans(avg, cnt, collection, ft, ft1, ft2, ks, ks1, max_k, threshold)
end

#outliers(sensitivity = 0.5, k = nil) ⇒ Object



100
101
102
103
104
105
106
107
108
# File 'lib/everyday-cli-utils/kmeans.rb', line 100

def outliers(sensitivity = 0.5, k = nil)
  ks = k.nil? ? nmeans : kmeans(k)
  cs = get_clusters(ks)

  outliers = []

  ks.each_with_index { |avg, i| outliers += find_outliers(avg, cs, i, sensitivity) }
  outliers
end

#prodObject



14
15
16
# File 'lib/everyday-cli-utils/maputil.rb', line 14

def prod
  reduce(:*)
end

#productmap(&block) ⇒ Object



37
38
39
# File 'lib/everyday-cli-utils/maputil.rb', line 37

def productmap(&block)
  map(&block).prod
end

#removefalseObject



2
3
4
# File 'lib/everyday-cli-utils/maputil.rb', line 2

def removefalse
  select { |i| i }
end

#std_devObject



22
23
24
25
26
27
# File 'lib/everyday-cli-utils/maputil.rb', line 22

def std_dev
  avg = average
  cnt = count.to_f
  su  = summap { |v| (v.to_f - avg.to_f) ** 2 }
  Math.sqrt(su / cnt)
end

#sumObject



10
11
12
# File 'lib/everyday-cli-utils/maputil.rb', line 10

def sum
  reduce(:+)
end

#summap(&block) ⇒ Object



33
34
35
# File 'lib/everyday-cli-utils/maputil.rb', line 33

def summap(&block)
  map(&block).sum
end