class Uranai::FeatureNormalizer

Attributes

feature[R]

Public Class Methods

new(feature) click to toggle source
# File lib/uranai/feature_normalizer.rb, line 5
def initialize(feature)
  @feature = feature
end

Public Instance Methods

normalize() click to toggle source
# File lib/uranai/feature_normalizer.rb, line 9
def normalize
  (matrix_feature - matrix_mean)
    .transpose
    .to_a
    .zip(std_of_each_feature)
    .map do |arr|
      arr[0].map { |ele| ele / arr[1] }
    end
end

Private Instance Methods

feature_size() click to toggle source
# File lib/uranai/feature_normalizer.rb, line 40
def feature_size
  feature[0].length
end
matrix_feature() click to toggle source
# File lib/uranai/feature_normalizer.rb, line 23
def matrix_feature
  Matrix.columns(feature)
end
matrix_mean() click to toggle source
# File lib/uranai/feature_normalizer.rb, line 27
def matrix_mean
  mean = Array([mean_of_each_feature]) * feature_size
  Matrix.rows(mean)
end
mean(arr) click to toggle source
# File lib/uranai/feature_normalizer.rb, line 44
def mean(arr)
  arr.reduce(:+) / arr.length.to_f
end
mean_of_each_feature() click to toggle source
# File lib/uranai/feature_normalizer.rb, line 32
def mean_of_each_feature
  feature.map { |arr| mean(arr) }
end
std(arr) click to toggle source
# File lib/uranai/feature_normalizer.rb, line 54
def std(arr)
  Math.sqrt(variance(arr))
end
std_of_each_feature() click to toggle source
# File lib/uranai/feature_normalizer.rb, line 36
def std_of_each_feature
  feature.map { |arr| std(arr) }
end
variance(arr) click to toggle source
# File lib/uranai/feature_normalizer.rb, line 48
def variance(arr)
  mean = mean(arr)
  sum = arr.inject(0.0) { |acc,i| acc + (i - mean)**2 }
  sum / (arr.length - 1)
end