Class: SVMKit::Preprocessing::MinMaxScaler
- Inherits:
-
Object
- Object
- SVMKit::Preprocessing::MinMaxScaler
- Includes:
- Base::BaseEstimator, Base::Transformer
- Defined in:
- lib/svmkit/preprocessing/min_max_scaler.rb
Overview
Normalize samples by scaling each feature to a given range.
Instance Attribute Summary collapse
-
#max_vec ⇒ Numo::DFloat
readonly
Return the vector consists of the maximum value for each feature.
-
#min_vec ⇒ Numo::DFloat
readonly
Return the vector consists of the minimum value for each feature.
Attributes included from Base::BaseEstimator
Instance Method Summary collapse
-
#fit(x) ⇒ MinMaxScaler
Calculate the minimum and maximum value of each feature for scaling.
-
#fit_transform(x) ⇒ Numo::DFloat
Calculate the minimum and maximum values, and then normalize samples to feature_range.
-
#initialize(feature_range: [0.0, 1.0]) ⇒ MinMaxScaler
constructor
Creates a new normalizer for scaling each feature to a given range.
-
#marshal_dump ⇒ Hash
Dump marshal data.
-
#marshal_load(obj) ⇒ nil
Load marshal data.
-
#transform(x) ⇒ Numo::DFloat
Perform scaling the given samples according to feature_range.
Constructor Details
#initialize(feature_range: [0.0, 1.0]) ⇒ MinMaxScaler
Creates a new normalizer for scaling each feature to a given range.
30 31 32 33 34 35 |
# File 'lib/svmkit/preprocessing/min_max_scaler.rb', line 30 def initialize(feature_range: [0.0, 1.0]) @params = {} @params[:feature_range] = feature_range @min_vec = nil @max_vec = nil end |
Instance Attribute Details
#max_vec ⇒ Numo::DFloat (readonly)
Return the vector consists of the maximum value for each feature.
25 26 27 |
# File 'lib/svmkit/preprocessing/min_max_scaler.rb', line 25 def max_vec @max_vec end |
#min_vec ⇒ Numo::DFloat (readonly)
Return the vector consists of the minimum value for each feature.
21 22 23 |
# File 'lib/svmkit/preprocessing/min_max_scaler.rb', line 21 def min_vec @min_vec end |
Instance Method Details
#fit(x) ⇒ MinMaxScaler
Calculate the minimum and maximum value of each feature for scaling.
43 44 45 46 47 |
# File 'lib/svmkit/preprocessing/min_max_scaler.rb', line 43 def fit(x, _y = nil) @min_vec = x.min(0) @max_vec = x.max(0) self end |
#fit_transform(x) ⇒ Numo::DFloat
Calculate the minimum and maximum values, and then normalize samples to feature_range.
55 56 57 |
# File 'lib/svmkit/preprocessing/min_max_scaler.rb', line 55 def fit_transform(x, _y = nil) fit(x).transform(x) end |
#marshal_dump ⇒ Hash
Dump marshal data.
72 73 74 75 76 |
# File 'lib/svmkit/preprocessing/min_max_scaler.rb', line 72 def marshal_dump { params: @params, min_vec: @min_vec, max_vec: @max_vec } end |
#marshal_load(obj) ⇒ nil
Load marshal data.
80 81 82 83 84 85 |
# File 'lib/svmkit/preprocessing/min_max_scaler.rb', line 80 def marshal_load(obj) @params = obj[:params] @min_vec = obj[:min_vec] @max_vec = obj[:max_vec] nil end |
#transform(x) ⇒ Numo::DFloat
Perform scaling the given samples according to feature_range.
63 64 65 66 67 68 |
# File 'lib/svmkit/preprocessing/min_max_scaler.rb', line 63 def transform(x) n_samples, = x.shape dif_vec = @max_vec - @min_vec nx = (x - @min_vec.tile(n_samples, 1)) / dif_vec.tile(n_samples, 1) nx * (@params[:feature_range][1] - @params[:feature_range][0]) + @params[:feature_range][0] end |