Class: Rumale::Preprocessing::MaxAbsScaler

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

Overview

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

Examples:

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 included from Base::BaseEstimator

#params

Instance Method Summary collapse

Constructor Details

#initializeMaxAbsScaler

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



23
24
25
26
# File 'lib/rumale/preprocessing/max_abs_scaler.rb', line 23

def initialize
  @params = {}
  @max_abs_vec = nil
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])



20
21
22
# File 'lib/rumale/preprocessing/max_abs_scaler.rb', line 20

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:



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

def fit(x, _y = nil)
  check_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.



46
47
48
49
# File 'lib/rumale/preprocessing/max_abs_scaler.rb', line 46

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 MaxAbsScaler.



62
63
64
65
# File 'lib/rumale/preprocessing/max_abs_scaler.rb', line 62

def marshal_dump
  { params: @params,
    max_abs_vec: @max_abs_vec }
end

#marshal_load(obj) ⇒ nil

Load marshal data.

Returns:

  • (nil)


69
70
71
72
73
# File 'lib/rumale/preprocessing/max_abs_scaler.rb', line 69

def marshal_load(obj)
  @params = obj[:params]
  @max_abs_vec = obj[:max_abs_vec]
  nil
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.



55
56
57
58
# File 'lib/rumale/preprocessing/max_abs_scaler.rb', line 55

def transform(x)
  check_sample_array(x)
  x / @max_abs_vec
end