Class: SVMKit::Preprocessing::L2Normalizer
- Inherits:
-
Object
- Object
- SVMKit::Preprocessing::L2Normalizer
- Includes:
- Base::BaseEstimator, Base::Transformer
- Defined in:
- lib/svmkit/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 included from Base::BaseEstimator
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.
Constructor Details
#initialize ⇒ L2Normalizer
Create a new normalizer for normaliing to unit L2-norm.
24 25 26 27 |
# File 'lib/svmkit/preprocessing/l2_normalizer.rb', line 24 def initialize @params = {} @norm_vec = nil end |
Instance Attribute Details
#norm_vec ⇒ Numo::DFloat (readonly)
Return the vector consists of L2-norm for each sample.
21 22 23 |
# File 'lib/svmkit/preprocessing/l2_normalizer.rb', line 21 def norm_vec @norm_vec end |
Instance Method Details
#fit(x) ⇒ L2Normalizer
Calculate L2-norms of each sample.
35 36 37 38 39 |
# File 'lib/svmkit/preprocessing/l2_normalizer.rb', line 35 def fit(x, _y = nil) SVMKit::Validation.check_sample_array(x) @norm_vec = Numo::NMath.sqrt((x**2).sum(1)) self end |
#fit_transform(x) ⇒ Numo::DFloat
Calculate L2-norms of each sample, and then normalize samples to unit L2-norm.
47 48 49 50 51 |
# File 'lib/svmkit/preprocessing/l2_normalizer.rb', line 47 def fit_transform(x, _y = nil) SVMKit::Validation.check_sample_array(x) fit(x) x / @norm_vec.tile(x.shape[1], 1).transpose end |