Class: Rumale::Preprocessing::Binarizer

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

Overview

Binarize samples according to a threshold

Examples:

require 'rumale/preprocessing/binarizer'

binarizer = Rumale::Preprocessing::Binarizer.new
x = Numo::DFloat[[-1.2, 3.2], [2.4, -0.5], [4.5, 0.8]]
b = binarizer.transform(x)
p b

# Numo::DFloat#shape=[3, 2]
# [[0, 1],
#  [1, 0],
#  [1, 1]]

Instance Attribute Summary

Attributes inherited from Base::Estimator

#params

Instance Method Summary collapse

Constructor Details

#initialize(threshold: 0.0) ⇒ Binarizer

Create a new transformer for binarization.

Parameters:

  • threshold (Float) (defaults to: 0.0)

    The threshold value for binarization.



28
29
30
31
# File 'rumale-preprocessing/lib/rumale/preprocessing/binarizer.rb', line 28

def initialize(threshold: 0.0)
  super()
  @params = { threshold: threshold }
end

Instance Method Details

#fitBinarizer

This method does nothing and returns the object itself. For compatibility with other transformer, this method exists.

Returns:



39
40
41
# File 'rumale-preprocessing/lib/rumale/preprocessing/binarizer.rb', line 39

def fit(_x = nil, _y = nil)
  self
end

#fit_transform(x, _y = nil) ⇒ Numo::DFloat

The output of this method is the same as that of the transform method. For compatibility with other transformer, this method exists.

Parameters:

  • x (Numo::DFloat)

    (shape: [n_samples, n_features]) The samples to be binarized.

Returns:

  • (Numo::DFloat)

    The binarized samples.



58
59
60
61
62
# File 'rumale-preprocessing/lib/rumale/preprocessing/binarizer.rb', line 58

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

  fit(x).transform(x)
end

#transform(x) ⇒ Numo::DFloat

Binarize each sample.

Parameters:

  • x (Numo::DFloat)

    (shape: [n_samples, n_features]) The samples to be binarized.

Returns:

  • (Numo::DFloat)

    The binarized samples.



47
48
49
50
51
# File 'rumale-preprocessing/lib/rumale/preprocessing/binarizer.rb', line 47

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

  x.class.cast(x.gt(@params[:threshold]))
end