Class: Rumale::Preprocessing::L1Normalizer

Inherits:
Base::Estimator show all
Includes:
Base::Transformer
Defined in:
rumale-preprocessing/lib/rumale/preprocessing/l1_normalizer.rb

Overview

Normalize samples to unit L1-norm.

Examples:

require 'rumale/preprocessing/l1_normalizer'

normalizer = Rumale::Preprocessing::L1Normalizer.new
new_samples = normalizer.fit_transform(samples)

Instance Attribute Summary collapse

Attributes inherited from Base::Estimator

#params

Instance Method Summary collapse

Constructor Details

#initializeL1Normalizer

Create a new normalizer for normaliing to L1-norm.



24
25
26
# File 'rumale-preprocessing/lib/rumale/preprocessing/l1_normalizer.rb', line 24

def initialize # rubocop:disable Lint/UselessMethodDefinition
  super()
end

Instance Attribute Details

#norm_vecNumo::DFloat (readonly)

Return the vector consists of L1-norm for each sample.

Returns:

  • (Numo::DFloat)

    (shape: [n_samples])



21
22
23
# File 'rumale-preprocessing/lib/rumale/preprocessing/l1_normalizer.rb', line 21

def norm_vec
  @norm_vec
end

Instance Method Details

#fit(x) ⇒ L1Normalizer

Calculate L1-norms of each sample.

Parameters:

  • x (Numo::DFloat)

    (shape: [n_samples, n_features]) The samples to calculate L1-norms.

Returns:



34
35
36
37
38
39
40
# File 'rumale-preprocessing/lib/rumale/preprocessing/l1_normalizer.rb', line 34

def fit(x, _y = nil)
  x = ::Rumale::Validation.check_convert_sample_array(x)

  @norm_vec = x.abs.sum(axis: 1)
  @norm_vec[@norm_vec.eq(0)] = 1
  self
end

#fit_transform(x) ⇒ Numo::DFloat

Calculate L1-norms of each sample, and then normalize samples to L1-norm.

Parameters:

  • x (Numo::DFloat)

    (shape: [n_samples, n_features]) The samples to calculate L1-norms.

Returns:

  • (Numo::DFloat)

    The normalized samples.



48
49
50
51
52
53
# File 'rumale-preprocessing/lib/rumale/preprocessing/l1_normalizer.rb', line 48

def fit_transform(x, _y = nil)
  x = ::Rumale::Validation.check_convert_sample_array(x)

  fit(x)
  x / @norm_vec.expand_dims(1)
end

#transform(x) ⇒ Numo::DFloat

Calculate L1-norms of each sample, and then normalize samples to L1-norm. This method calls the fit_transform method. This method exists for the Pipeline class.

Parameters:

  • x (Numo::DFloat)

    (shape: [n_samples, n_features]) The samples to calculate L1-norms.

Returns:

  • (Numo::DFloat)

    The normalized samples.



60
61
62
63
64
# File 'rumale-preprocessing/lib/rumale/preprocessing/l1_normalizer.rb', line 60

def transform(x)
  x = ::Rumale::Validation.check_convert_sample_array(x)

  fit_transform(x)
end