Class: Rumale::Preprocessing::StandardScaler
- Inherits:
-
Base::Estimator
- Object
- Base::Estimator
- Rumale::Preprocessing::StandardScaler
- Includes:
- Base::Transformer
- Defined in:
- rumale-preprocessing/lib/rumale/preprocessing/standard_scaler.rb
Overview
Normalize samples by centering and scaling to unit variance.
Instance Attribute Summary collapse
-
#mean_vec ⇒ Numo::DFloat
readonly
Return the vector consists of the mean value for each feature.
-
#std_vec ⇒ Numo::DFloat
readonly
Return the vector consists of the standard deviation for each feature.
Attributes inherited from Base::Estimator
Instance Method Summary collapse
-
#fit(x) ⇒ StandardScaler
Calculate the mean value and standard deviation of each feature for scaling.
-
#fit_transform(x) ⇒ Numo::DFloat
Calculate the mean values and standard deviations, and then normalize samples using them.
-
#initialize ⇒ StandardScaler
constructor
Create a new normalizer for centering and scaling to unit variance.
-
#transform(x) ⇒ Numo::DFloat
Perform standardization the given samples.
Constructor Details
#initialize ⇒ StandardScaler
Create a new normalizer for centering and scaling to unit variance.
30 31 32 |
# File 'rumale-preprocessing/lib/rumale/preprocessing/standard_scaler.rb', line 30 def initialize # rubocop:disable Lint/UselessMethodDefinition super() end |
Instance Attribute Details
#mean_vec ⇒ Numo::DFloat (readonly)
Return the vector consists of the mean value for each feature.
23 24 25 |
# File 'rumale-preprocessing/lib/rumale/preprocessing/standard_scaler.rb', line 23 def mean_vec @mean_vec end |
#std_vec ⇒ Numo::DFloat (readonly)
Return the vector consists of the standard deviation for each feature.
27 28 29 |
# File 'rumale-preprocessing/lib/rumale/preprocessing/standard_scaler.rb', line 27 def std_vec @std_vec end |
Instance Method Details
#fit(x) ⇒ StandardScaler
Calculate the mean value and standard deviation of each feature for scaling.
41 42 43 44 45 46 47 |
# File 'rumale-preprocessing/lib/rumale/preprocessing/standard_scaler.rb', line 41 def fit(x, _y = nil) x = ::Rumale::Validation.check_convert_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.
56 57 58 59 60 |
# File 'rumale-preprocessing/lib/rumale/preprocessing/standard_scaler.rb', line 56 def fit_transform(x, _y = nil) x = ::Rumale::Validation.check_convert_sample_array(x) fit(x).transform(x) end |
#transform(x) ⇒ Numo::DFloat
Perform standardization the given samples.
66 67 68 69 70 71 |
# File 'rumale-preprocessing/lib/rumale/preprocessing/standard_scaler.rb', line 66 def transform(x) x = ::Rumale::Validation.check_convert_sample_array(x) n_samples, = x.shape (x - @mean_vec.tile(n_samples, 1)) / @std_vec.tile(n_samples, 1) end |