Sha256: c7388fca7d7c2a2f2fe5218d7e5a515e9dcc1fff5a8be45cd0b7601d756364a7

Contents?: true

Size: 1.76 KB

Versions: 16

Compression:

Stored size: 1.76 KB

Contents

# frozen_string_literal: true

require 'rumale/base/base_estimator'
require 'rumale/base/transformer'

module Rumale
  module Preprocessing
    # Binarize samples according to a threshold
    #
    # @example
    #   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]]
    class Binarizer
      include Base::BaseEstimator
      include Base::Transformer

      # Create a new transformer for binarization.
      # @param threshold [Float] The threshold value for binarization.
      def initialize(threshold: 0.0)
        check_params_numeric(threshold: threshold)
        @params = { threshold: threshold }
      end

      # This method does nothing and returns the object itself.
      # For compatibility with other transformer, this method exists.
      #
      # @overload fit() -> Binarizer
      #
      # @return [Binarizer]
      def fit(_x = nil, _y = nil)
        self
      end

      # Binarize each sample.
      #
      # @param x [Numo::DFloat] (shape: [n_samples, n_features]) The samples to be binarized.
      # @return [Numo::DFloat] The binarized samples.
      def transform(x)
        x = check_convert_sample_array(x)
        x.class.cast(x.gt(@params[:threshold]))
      end

      # The output of this method is the same as that of the transform method.
      # For compatibility with other transformer, this method exists.
      #
      # @param x [Numo::DFloat] (shape: [n_samples, n_features]) The samples to be binarized.
      # @return [Numo::DFloat] The binarized samples.
      def fit_transform(x, _y = nil)
        fit(x).transform(x)
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
rumale-0.23.3 lib/rumale/preprocessing/binarizer.rb
rumale-0.23.2 lib/rumale/preprocessing/binarizer.rb
rumale-0.23.1 lib/rumale/preprocessing/binarizer.rb
rumale-0.23.0 lib/rumale/preprocessing/binarizer.rb
rumale-0.22.5 lib/rumale/preprocessing/binarizer.rb
rumale-0.22.4 lib/rumale/preprocessing/binarizer.rb
rumale-0.22.3 lib/rumale/preprocessing/binarizer.rb
rumale-0.22.2 lib/rumale/preprocessing/binarizer.rb
rumale-0.22.1 lib/rumale/preprocessing/binarizer.rb
rumale-0.22.0 lib/rumale/preprocessing/binarizer.rb
rumale-0.21.0 lib/rumale/preprocessing/binarizer.rb
rumale-0.20.3 lib/rumale/preprocessing/binarizer.rb
rumale-0.20.2 lib/rumale/preprocessing/binarizer.rb
rumale-0.20.1 lib/rumale/preprocessing/binarizer.rb
rumale-0.20.0 lib/rumale/preprocessing/binarizer.rb
rumale-0.19.3 lib/rumale/preprocessing/binarizer.rb