Module: MachineLearningWorkbench::Tools::Normalization

Defined in:
lib/machine_learning_workbench/tools/normalization.rb

Class Method Summary collapse

Class Method Details

.feature_scaling(nmat, from: nil, to: [0,1]) ⇒ Object



3
4
5
6
7
8
# File 'lib/machine_learning_workbench/tools/normalization.rb', line 3

def self.feature_scaling nmat, from: nil, to: [0,1]
  from ||= nmat.minmax
  old_min, old_max = from
  new_min, new_max = to
  res = (nmat-old_min)*(new_max-new_min)/(old_max-old_min)+new_min
end

.z_score(nmat, per_column: true) ⇒ Object

Parameters:

  • per_column (bool) (defaults to: true)

    wheather to compute stats per-column or matrix-wise

Raises:

  • (NotImplementedError)


11
12
13
14
15
16
17
18
19
20
# File 'lib/machine_learning_workbench/tools/normalization.rb', line 11

def self.z_score nmat, per_column: true
  raise NotImplementedError unless per_column
  means = nmat.mean
  stddevs = nmat.std
  # address edge case of zero variance
  stddevs.map! { |v| v.zero? ? 1 : v }
  mean_mat = means.repeat nmat.rows, 0
  stddev_mat = stddevs.repeat nmat.rows, 0
  ret = (nmat - mean_mat) / stddev_mat
end