Class: Normalizer

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Normalizer

Returns a new instance of Normalizer.



98
99
100
101
102
# File 'lib/normalizer.rb', line 98

def initialize(opts={})
  @max = opts[:max].map {|x| x.to_f}
  @min = opts[:min].map {|x| x.to_f}
  @ranges = calculate_ranges
end

Instance Attribute Details

#maxObject (readonly)

Returns the value of attribute max.



96
97
98
# File 'lib/normalizer.rb', line 96

def max
  @max
end

#minObject (readonly)

Returns the value of attribute min.



96
97
98
# File 'lib/normalizer.rb', line 96

def min
  @min
end

#rangesObject (readonly)

Returns the value of attribute ranges.



96
97
98
# File 'lib/normalizer.rb', line 96

def ranges
  @ranges
end

Class Method Details

.find_min_and_max(data, options = {}) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/normalizer.rb', line 4

def find_min_and_max(data, options={})
  @data = data
  @std = options[:std] || 0
  @cat_data = Array.new(@data[0].size) { Array.new }
  
  # Along with finding max and min this also fills @cat_data like so:
  # [[1,2,3], [1,2,3]] turns into [[1,1], [2,2], [3,3]] so we can calculate
  @max, @min = find_max, find_min
  
  unless @std > 0
    [@min, @max]
  else
    mean = find_mean
    std = calculate_std(mean)
    
    @max.each_with_index do |n, i|
      @max[i] = n + (std[i] * @std)
    end
    
    @min.each_with_index do |n, i|
      @min[i] = n - (std[i] * @std)
    end
    
    [@min, @max]
  end
end

Instance Method Details

#breaks_boundary?Boolean

Returns:

  • (Boolean)


116
117
118
# File 'lib/normalizer.rb', line 116

def breaks_boundary?
  @breaks_boundary
end

#normalize(data) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
# File 'lib/normalizer.rb', line 104

def normalize(data)
  normalized = Array.new(data.size)

  data.each_with_index do |n, index|
    normalized[index] = (n.to_f - @min[index]) / @ranges[index]
  end
  
  @breaks_boundary = normalized.any? {|x| x > 1 || x < 0}
  
  round_to_boundaries(normalized)
end