Class: SVMKit::Preprocessing::MinMaxScaler

Inherits:
Object
  • Object
show all
Includes:
Base::BaseEstimator, Base::Transformer
Defined in:
lib/svmkit/preprocessing/min_max_scaler.rb

Overview

Normalize samples by scaling each feature to a given range.

normalizer = SVMKit::Preprocessing::MinMaxScaler.new(feature_range: [0.0, 1.0])
new_training_samples = normalizer.fit_transform(training_samples)
new_testing_samples = normalizer.transform(testing_samples)

Constant Summary collapse

DEFAULT_PARAMS =

:nodoc:

{ # :nodoc:
  feature_range: [0.0, 1.0]
}.freeze

Instance Attribute Summary collapse

Attributes included from Base::BaseEstimator

#params

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ MinMaxScaler

Creates a new normalizer for scaling each feature to a given range.

call-seq:

new(feature_range: [0.0, 1.0]) -> MinMaxScaler
  • Arguments :

    • :feature_range (Array) (defaults to: [0.0, 1.0]) – The desired range of samples.



33
34
35
36
37
# File 'lib/svmkit/preprocessing/min_max_scaler.rb', line 33

def initialize(params = {})
  @params = DEFAULT_PARAMS.merge(Hash[params.map { |k, v| [k.to_sym, v] }])
  @min_vec = nil
  @max_vec = nil
end

Instance Attribute Details

#max_vecObject (readonly)

The vector consists of the maximum value for each feature.



24
25
26
# File 'lib/svmkit/preprocessing/min_max_scaler.rb', line 24

def max_vec
  @max_vec
end

#min_vecObject (readonly)

The vector consists of the minimum value for each feature.



21
22
23
# File 'lib/svmkit/preprocessing/min_max_scaler.rb', line 21

def min_vec
  @min_vec
end

Instance Method Details

#fit(x, _y = nil) ⇒ Object

Calculate the minimum and maximum value of each feature for scaling.

:call-seq:

fit(x) -> MinMaxScaler
  • Arguments :

    • x (NMatrix, shape: [n_samples, n_features]) – The samples to calculate the minimum and maximum values.

  • Returns :

    • MinMaxScaler



48
49
50
51
52
# File 'lib/svmkit/preprocessing/min_max_scaler.rb', line 48

def fit(x, _y = nil)
  @min_vec = x.min(0)
  @max_vec = x.max(0)
  self
end

#fit_transform(x, _y = nil) ⇒ Object

Calculate the minimum and maximum values, and then normalize samples to feature_range.

:call-seq:

fit_transform(x) -> NMatrix
  • Arguments :

    • x (NMatrix, shape: [n_samples, n_features]) – The samples to calculate the minimum and maximum values.

  • Returns :

    • The scaled samples (NMatrix)



63
64
65
# File 'lib/svmkit/preprocessing/min_max_scaler.rb', line 63

def fit_transform(x, _y = nil)
  fit(x).transform(x)
end

#marshal_dumpObject

Serializes object through Marshal#dump.



84
85
86
87
88
# File 'lib/svmkit/preprocessing/min_max_scaler.rb', line 84

def marshal_dump # :nodoc:
  { params: @params,
    min_vec: Utils.dump_nmatrix(@min_vec),
    max_vec: Utils.dump_nmatrix(@max_vec) }
end

#marshal_load(obj) ⇒ Object

Deserialize object through Marshal#load.



91
92
93
94
95
96
# File 'lib/svmkit/preprocessing/min_max_scaler.rb', line 91

def marshal_load(obj) # :nodoc:
  @params = obj[:params]
  @min_vec = Utils.restore_nmatrix(obj[:min_vec])
  @max_vec = Utils.restore_nmatrix(obj[:max_vec])
  nil
end

#transform(x) ⇒ Object

Perform scaling the given samples according to feature_range.

call-seq:

transform(x) -> NMatrix
  • Arguments :

    • x (NMatrix, shape: [n_samples, n_features]) – The samples to be scaled.

  • Returns :

    • The scaled samples (NMatrix)



76
77
78
79
80
81
# File 'lib/svmkit/preprocessing/min_max_scaler.rb', line 76

def transform(x)
  n_samples, = x.shape
  dif_vec = @max_vec - @min_vec
  nx = (x - @min_vec.repeat(n_samples, 0)) / dif_vec.repeat(n_samples, 0)
  nx * (@params[:feature_range][1] - @params[:feature_range][0]) + @params[:feature_range][0]
end