Class: Rumale::Preprocessing::MinMaxScaler

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

Overview

Normalize samples by scaling each feature to a given range.

Examples:

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

Instance Attribute Summary collapse

Attributes included from Base::BaseEstimator

#params

Instance Method Summary collapse

Constructor Details

#initialize(feature_range: [0.0, 1.0]) ⇒ MinMaxScaler

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

Parameters:

  • feature_range (Array<Float>) (defaults to: [0.0, 1.0])

    The desired range of samples.



30
31
32
33
34
35
36
# File 'lib/rumale/preprocessing/min_max_scaler.rb', line 30

def initialize(feature_range: [0.0, 1.0])
  check_params_type(Array, feature_range: feature_range)
  @params = {}
  @params[:feature_range] = feature_range
  @min_vec = nil
  @max_vec = nil
end

Instance Attribute Details

#max_vecNumo::DFloat (readonly)

Return the vector consists of the maximum value for each feature.

Returns:

  • (Numo::DFloat)

    (shape: [n_features])



25
26
27
# File 'lib/rumale/preprocessing/min_max_scaler.rb', line 25

def max_vec
  @max_vec
end

#min_vecNumo::DFloat (readonly)

Return the vector consists of the minimum value for each feature.

Returns:

  • (Numo::DFloat)

    (shape: [n_features])



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

def min_vec
  @min_vec
end

Instance Method Details

#fit(x) ⇒ MinMaxScaler

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

Parameters:

  • x (Numo::DFloat)

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

Returns:



44
45
46
47
48
49
# File 'lib/rumale/preprocessing/min_max_scaler.rb', line 44

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

#fit_transform(x) ⇒ Numo::DFloat

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

Parameters:

  • x (Numo::DFloat)

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

Returns:

  • (Numo::DFloat)

    The scaled samples.



57
58
59
60
# File 'lib/rumale/preprocessing/min_max_scaler.rb', line 57

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

#marshal_dumpHash

Dump marshal data.

Returns:

  • (Hash)

    The marshal data about MinMaxScaler.



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

def marshal_dump
  { params: @params,
    min_vec: @min_vec,
    max_vec: @max_vec }
end

#marshal_load(obj) ⇒ nil

Load marshal data.

Returns:

  • (nil)


85
86
87
88
89
90
# File 'lib/rumale/preprocessing/min_max_scaler.rb', line 85

def marshal_load(obj)
  @params = obj[:params]
  @min_vec = obj[:min_vec]
  @max_vec = obj[:max_vec]
  nil
end

#transform(x) ⇒ Numo::DFloat

Perform scaling the given samples according to feature_range.

Parameters:

  • x (Numo::DFloat)

    (shape: [n_samples, n_features]) The samples to be scaled.

Returns:

  • (Numo::DFloat)

    The scaled samples.



66
67
68
69
70
71
72
73
# File 'lib/rumale/preprocessing/min_max_scaler.rb', line 66

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