Class: Rumale::Preprocessing::MaxAbsScaler

Inherits:
Base::Estimator show all
Includes:
Base::Transformer
Defined in:
rumale-preprocessing/lib/rumale/preprocessing/max_abs_scaler.rb

Overview

Normalize samples by scaling each feature with its maximum absolute value.

Examples:

require 'rumale/preprocessing/max_abs_scaler'

normalizer = Rumale::Preprocessing::MaxAbsScaler.new
new_training_samples = normalizer.fit_transform(training_samples)
new_testing_samples = normalizer.transform(testing_samples)

Instance Attribute Summary collapse

Attributes inherited from Base::Estimator

#params

Instance Method Summary collapse

Constructor Details

#initializeMaxAbsScaler

Creates a new normalizer for scaling each feature with its maximum absolute value.



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

def initialize # rubocop:disable Lint/UselessMethodDefinition
  super()
end

Instance Attribute Details

#max_abs_vecNumo::DFloat (readonly)

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

Returns:

  • (Numo::DFloat)

    (shape: [n_features])



22
23
24
# File 'rumale-preprocessing/lib/rumale/preprocessing/max_abs_scaler.rb', line 22

def max_abs_vec
  @max_abs_vec
end

Instance Method Details

#fit(x) ⇒ MaxAbsScaler

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

Parameters:

  • x (Numo::DFloat)

    (shape: [n_samples, n_features]) The samples to calculate maximum absolute value for each feature.

Returns:



35
36
37
38
39
40
# File 'rumale-preprocessing/lib/rumale/preprocessing/max_abs_scaler.rb', line 35

def fit(x, _y = nil)
  x = ::Rumale::Validation.check_convert_sample_array(x)

  @max_abs_vec = x.abs.max(0)
  self
end

#fit_transform(x) ⇒ Numo::DFloat

Calculate the maximum absolute value for each feature, and then normalize samples.

Parameters:

  • x (Numo::DFloat)

    (shape: [n_samples, n_features]) The samples to calculate maximum absolute value for each feature.

Returns:

  • (Numo::DFloat)

    The scaled samples.



48
49
50
51
52
# File 'rumale-preprocessing/lib/rumale/preprocessing/max_abs_scaler.rb', line 48

def fit_transform(x, _y = nil)
  x = ::Rumale::Validation.check_convert_sample_array(x)

  fit(x).transform(x)
end

#transform(x) ⇒ Numo::DFloat

Perform scaling the given samples with maximum absolute value for each feature.

Parameters:

  • x (Numo::DFloat)

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

Returns:

  • (Numo::DFloat)

    The scaled samples.



58
59
60
61
62
# File 'rumale-preprocessing/lib/rumale/preprocessing/max_abs_scaler.rb', line 58

def transform(x)
  x = ::Rumale::Validation.check_convert_sample_array(x)

  x / @max_abs_vec
end