Class: Rumale::Preprocessing::StandardScaler

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

Overview

Normalize samples by centering and scaling to unit variance.

Examples:

normalizer = Rumale::Preprocessing::StandardScaler.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

#initializeStandardScaler

Create a new normalizer for centering and scaling to unit variance.



28
29
30
31
32
# File 'lib/rumale/preprocessing/standard_scaler.rb', line 28

def initialize
  @params = {}
  @mean_vec = nil
  @std_vec = nil
end

Instance Attribute Details

#mean_vecNumo::DFloat (readonly)

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

Returns:

  • (Numo::DFloat)

    (shape: [n_features])



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

def mean_vec
  @mean_vec
end

#std_vecNumo::DFloat (readonly)

Return the vector consists of the standard deviation for each feature.

Returns:

  • (Numo::DFloat)

    (shape: [n_features])



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

def std_vec
  @std_vec
end

Instance Method Details

#fit(x) ⇒ StandardScaler

Calculate the mean value and standard deviation of each feature for scaling.

Parameters:

  • x (Numo::DFloat)

    (shape: [n_samples, n_features]) The samples to calculate the mean values and standard deviations.

Returns:



41
42
43
44
45
46
# File 'lib/rumale/preprocessing/standard_scaler.rb', line 41

def fit(x, _y = nil)
  check_sample_array(x)
  @mean_vec = x.mean(0)
  @std_vec = x.stddev(0)
  self
end

#fit_transform(x) ⇒ Numo::DFloat

Calculate the mean values and standard deviations, and then normalize samples using them.

Parameters:

  • x (Numo::DFloat)

    (shape: [n_samples, n_features]) The samples to calculate the mean values and standard deviations.

Returns:

  • (Numo::DFloat)

    The scaled samples.



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

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



72
73
74
75
# File 'lib/rumale/preprocessing/standard_scaler.rb', line 72

def marshal_dump
  { mean_vec: @mean_vec,
    std_vec: @std_vec }
end

#marshal_load(obj) ⇒ nil

Load marshal data.

Returns:

  • (nil)


79
80
81
82
83
# File 'lib/rumale/preprocessing/standard_scaler.rb', line 79

def marshal_load(obj)
  @mean_vec = obj[:mean_vec]
  @std_vec = obj[:std_vec]
  nil
end

#transform(x) ⇒ Numo::DFloat

Perform standardization the given samples.

Parameters:

  • x (Numo::DFloat)

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

Returns:

  • (Numo::DFloat)

    The scaled samples.



64
65
66
67
68
# File 'lib/rumale/preprocessing/standard_scaler.rb', line 64

def transform(x)
  check_sample_array(x)
  n_samples, = x.shape
  (x - @mean_vec.tile(n_samples, 1)) / @std_vec.tile(n_samples, 1)
end