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.
normalizer = SVMKit::Preprocessing::StandardScaler.new
new_training_samples = normalizer.fit_transform(training_samples)
new_testing_samples = normalizer.transform(testing_samples)
Instance Attribute Summary collapse
-
#mean_vec ⇒ Object
readonly
The vector consists of the mean value for each feature.
-
#std_vec ⇒ Object
readonly
The vector consists of the standard deviation for each feature.
Attributes included from Base::BaseEstimator
Instance Method Summary collapse
-
#fit(x, _y = nil) ⇒ Object
Calculate the mean value and standard deviation of each feature for scaling.
-
#fit_transform(x, _y = nil) ⇒ Object
Calculate the mean values and standard deviations, and then normalize samples using them.
-
#initialize(_params = {}) ⇒ StandardScaler
constructor
Create a new normalizer for centering and scaling to unit variance.
-
#marshal_dump ⇒ Object
Serializes object through Marshal#dump.
-
#marshal_load(obj) ⇒ Object
Deserialize object through Marshal#load.
-
#transform(x) ⇒ Object
Perform standardization the given samples.
Constructor Details
#initialize(_params = {}) ⇒ StandardScaler
Create a new normalizer for centering and scaling to unit variance.
:call-seq:
new() -> StandardScaler
26 27 28 29 |
# File 'lib/svmkit/preprocessing/standard_scaler.rb', line 26 def initialize(_params = {}) @mean_vec = nil @std_vec = nil end |
Instance Attribute Details
#mean_vec ⇒ Object (readonly)
The vector consists of the mean value for each feature.
17 18 19 |
# File 'lib/svmkit/preprocessing/standard_scaler.rb', line 17 def mean_vec @mean_vec end |
#std_vec ⇒ Object (readonly)
The vector consists of the standard deviation for each feature.
20 21 22 |
# File 'lib/svmkit/preprocessing/standard_scaler.rb', line 20 def std_vec @std_vec end |
Instance Method Details
#fit(x, _y = nil) ⇒ Object
Calculate the mean value and standard deviation of each feature for scaling.
:call-seq:
fit(x) -> StandardScaler
-
Arguments :
-
x(NMatrix, shape: [n_samples, n_features]) – The samples to calculate the mean values and standard deviations.
-
-
Returns :
-
StandardScaler
-
40 41 42 43 44 |
# File 'lib/svmkit/preprocessing/standard_scaler.rb', line 40 def fit(x, _y = nil) @mean_vec = x.mean(0) @std_vec = x.std(0) self end |
#fit_transform(x, _y = nil) ⇒ Object
Calculate the mean values and standard deviations, and then normalize samples using them.
:call-seq:
fit_transform(x) -> NMatrix
-
Arguments :
-
x(NMatrix, shape: [n_samples, n_features]) – The samples to calculate the mean values and standard deviations.
-
-
Returns :
-
The scaled samples (NMatrix)
-
55 56 57 |
# File 'lib/svmkit/preprocessing/standard_scaler.rb', line 55 def fit_transform(x, _y = nil) fit(x).transform(x) end |
#marshal_dump ⇒ Object
Serializes object through Marshal#dump.
74 75 76 77 |
# File 'lib/svmkit/preprocessing/standard_scaler.rb', line 74 def marshal_dump # :nodoc: { mean_vec: Utils.dump_nmatrix(@mean_vec), std_vec: Utils.dump_nmatrix(@std_vec) } end |
#marshal_load(obj) ⇒ Object
Deserialize object through Marshal#load.
80 81 82 83 84 |
# File 'lib/svmkit/preprocessing/standard_scaler.rb', line 80 def marshal_load(obj) # :nodoc: @mean_vec = Utils.restore_nmatrix(obj[:mean_vec]) @std_vec = Utils.restore_nmatrix(obj[:std_vec]) nil end |
#transform(x) ⇒ Object
Perform standardization the given samples.
call-seq:
transform(x) -> NMatrix
-
Arguments :
-
x(NMatrix, shape: [n_samples, n_features]) – The samples to be scaled.
-
-
Returns :
-
The scaled samples (NMatrix)
-
68 69 70 71 |
# File 'lib/svmkit/preprocessing/standard_scaler.rb', line 68 def transform(x) n_samples, = x.shape (x - @mean_vec.repeat(n_samples, 0)) / @std_vec.repeat(n_samples, 0) end |