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 ⇒ NMatrix
readonly
Return the vector consists of the maximum value for each feature.
-
#min_vec ⇒ NMatrix
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) ⇒ NMatrix
Calculate the minimum and maximum values, and then normalize samples to feature_range.
-
#new(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) ⇒ NMatrix
Perform scaling the given samples according to feature_range.
Constructor Details
#new(feature_range: [0.0, 1.0]) ⇒ MinMaxScaler
Creates a new normalizer for scaling each feature to a given range.
35 36 37 38 39 |
# File 'lib/svmkit/preprocessing/min_max_scaler.rb', line 35 def initialize(params = {}) @params = DEFAULT_PARAMS.merge(Hash[params.map { |k, v| [k.to_sym, v] }]) @min_vec = nil @max_vec = nil end |
Instance Attribute Details
#max_vec ⇒ NMatrix (readonly)
Return the vector consists of the maximum value for each feature.
28 29 30 |
# File 'lib/svmkit/preprocessing/min_max_scaler.rb', line 28 def max_vec @max_vec end |
#min_vec ⇒ NMatrix (readonly)
Return the vector consists of the minimum value for each feature.
24 25 26 |
# File 'lib/svmkit/preprocessing/min_max_scaler.rb', line 24 def min_vec @min_vec end |
Instance Method Details
#fit(x) ⇒ MinMaxScaler
Calculate the minimum and maximum value of each feature for scaling.
47 48 49 50 51 |
# File 'lib/svmkit/preprocessing/min_max_scaler.rb', line 47 def fit(x, _y = nil) @min_vec = x.min(0) @max_vec = x.max(0) self end |
#fit_transform(x) ⇒ NMatrix
Calculate the minimum and maximum values, and then normalize samples to feature_range.
59 60 61 |
# File 'lib/svmkit/preprocessing/min_max_scaler.rb', line 59 def fit_transform(x, _y = nil) fit(x).transform(x) end |
#marshal_dump ⇒ Hash
Dump marshal data.
76 77 78 79 80 |
# File 'lib/svmkit/preprocessing/min_max_scaler.rb', line 76 def marshal_dump { params: @params, min_vec: Utils.dump_nmatrix(@min_vec), max_vec: Utils.dump_nmatrix(@max_vec) } end |
#marshal_load(obj) ⇒ nil
Load marshal data.
84 85 86 87 88 89 |
# File 'lib/svmkit/preprocessing/min_max_scaler.rb', line 84 def marshal_load(obj) @params = obj[:params] @min_vec = Utils.restore_nmatrix(obj[:min_vec]) @max_vec = Utils.restore_nmatrix(obj[:max_vec]) nil end |
#transform(x) ⇒ NMatrix
Perform scaling the given samples according to feature_range.
67 68 69 70 71 72 |
# File 'lib/svmkit/preprocessing/min_max_scaler.rb', line 67 def transform(x) n_samples, = x.shape dif_vec = @max_vec - @min_vec nx = (x - @min_vec.repeat(n_samples, 0)) / dif_vec.repeat(n_samples, 0) nx * (@params[:feature_range][1] - @params[:feature_range][0]) + @params[:feature_range][0] end |