Class: Rumale::Preprocessing::L2Normalizer
- Inherits:
-
Base::Estimator
- Object
- Base::Estimator
- Rumale::Preprocessing::L2Normalizer
- Includes:
- Base::Transformer
- Defined in:
- rumale-preprocessing/lib/rumale/preprocessing/l2_normalizer.rb
Overview
Normalize samples to unit L2-norm.
Instance Attribute Summary collapse
-
#norm_vec ⇒ Numo::DFloat
readonly
Return the vector consists of L2-norm for each sample.
Attributes inherited from Base::Estimator
Instance Method Summary collapse
-
#fit(x) ⇒ L2Normalizer
Calculate L2-norms of each sample.
-
#fit_transform(x) ⇒ Numo::DFloat
Calculate L2-norms of each sample, and then normalize samples to unit L2-norm.
-
#initialize ⇒ L2Normalizer
constructor
Create a new normalizer for normaliing to unit L2-norm.
-
#transform(x) ⇒ Numo::DFloat
Calculate L2-norms of each sample, and then normalize samples to unit L2-norm.
Constructor Details
#initialize ⇒ L2Normalizer
Create a new normalizer for normaliing to unit L2-norm.
25 26 27 |
# File 'rumale-preprocessing/lib/rumale/preprocessing/l2_normalizer.rb', line 25 def initialize # rubocop:disable Lint/UselessMethodDefinition super() end |
Instance Attribute Details
#norm_vec ⇒ Numo::DFloat (readonly)
Return the vector consists of L2-norm for each sample.
22 23 24 |
# File 'rumale-preprocessing/lib/rumale/preprocessing/l2_normalizer.rb', line 22 def norm_vec @norm_vec end |
Instance Method Details
#fit(x) ⇒ L2Normalizer
Calculate L2-norms of each sample.
35 36 37 38 39 40 41 |
# File 'rumale-preprocessing/lib/rumale/preprocessing/l2_normalizer.rb', line 35 def fit(x, _y = nil) x = ::Rumale::Validation.check_convert_sample_array(x) @norm_vec = Numo::NMath.sqrt((x**2).sum(axis: 1)) @norm_vec[@norm_vec.eq(0)] = 1 self end |
#fit_transform(x) ⇒ Numo::DFloat
Calculate L2-norms of each sample, and then normalize samples to unit L2-norm.
49 50 51 52 53 54 |
# File 'rumale-preprocessing/lib/rumale/preprocessing/l2_normalizer.rb', line 49 def fit_transform(x, _y = nil) x = ::Rumale::Validation.check_convert_sample_array(x) fit(x) x / @norm_vec.(1) end |
#transform(x) ⇒ Numo::DFloat
Calculate L2-norms of each sample, and then normalize samples to unit L2-norm. This method calls the fit_transform method. This method exists for the Pipeline class.
61 62 63 64 65 |
# File 'rumale-preprocessing/lib/rumale/preprocessing/l2_normalizer.rb', line 61 def transform(x) x = ::Rumale::Validation.check_convert_sample_array(x) fit_transform(x) end |