Class: SVMKit::Preprocessing::StandardScaler
- Inherits:
-
Object
- Object
- SVMKit::Preprocessing::StandardScaler
- Includes:
- Base::BaseEstimator, Base::Transformer
- Defined in:
- lib/svmkit/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 included from Base::BaseEstimator
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.
-
#marshal_dump ⇒ Hash
Dump marshal data.
-
#marshal_load(obj) ⇒ nil
Load marshal data.
-
#transform(x) ⇒ Numo::DFloat
Perform standardization the given samples.
Constructor Details
#initialize ⇒ StandardScaler
Create a new normalizer for centering and scaling to unit variance.
26 27 28 29 30 |
# File 'lib/svmkit/preprocessing/standard_scaler.rb', line 26 def initialize @params = {} @mean_vec = nil @std_vec = nil end |
Instance Attribute Details
#mean_vec ⇒ Numo::DFloat (readonly)
Return the vector consists of the mean value for each feature.
19 20 21 |
# File 'lib/svmkit/preprocessing/standard_scaler.rb', line 19 def mean_vec @mean_vec end |
#std_vec ⇒ Numo::DFloat (readonly)
Return the vector consists of the standard deviation for each feature.
23 24 25 |
# File 'lib/svmkit/preprocessing/standard_scaler.rb', line 23 def std_vec @std_vec end |
Instance Method Details
#fit(x) ⇒ StandardScaler
Calculate the mean value and standard deviation of each feature for scaling.
39 40 41 42 43 |
# File 'lib/svmkit/preprocessing/standard_scaler.rb', line 39 def fit(x, _y = nil) @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.
52 53 54 |
# File 'lib/svmkit/preprocessing/standard_scaler.rb', line 52 def fit_transform(x, _y = nil) fit(x).transform(x) end |
#marshal_dump ⇒ Hash
Dump marshal data.
67 68 69 70 |
# File 'lib/svmkit/preprocessing/standard_scaler.rb', line 67 def marshal_dump { mean_vec: @mean_vec, std_vec: @std_vec } end |
#marshal_load(obj) ⇒ nil
Load marshal data.
74 75 76 77 78 |
# File 'lib/svmkit/preprocessing/standard_scaler.rb', line 74 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.
60 61 62 63 |
# File 'lib/svmkit/preprocessing/standard_scaler.rb', line 60 def transform(x) n_samples, = x.shape (x - @mean_vec.tile(n_samples, 1)) / @std_vec.tile(n_samples, 1) end |