Class: Rumale::Pipeline::FeatureUnion

Inherits:
Base::Estimator show all
Defined in:
rumale-pipeline/lib/rumale/pipeline/feature_union.rb

Overview

FeatureUnion is a class that implements the function concatenating the multi-transformer results.

Examples:

require 'rumale/kernel_approximation/rbf'
require 'rumale/decomposition/pca'
require 'rumale/pipeline/feature_union'

fu = Rumale::Pipeline::FeatureUnion.new(
  transformers: {
    'rbf': Rumale::KernelApproximation::RBF.new(gamma: 1.0, n_components: 96, random_seed: 1),
    'pca': Rumale::Decomposition::PCA.new(n_components: 32)
  }
)
fu.fit(training_samples, traininig_labels)
results = fu.predict(testing_samples)

# > p results.shape[1]
# > 128

Instance Attribute Summary collapse

Attributes inherited from Base::Estimator

#params

Instance Method Summary collapse

Constructor Details

#initialize(transformers:) ⇒ FeatureUnion

Create a new feature union.

Parameters:

  • transformers (Hash)

    List of transformers. The order of transforms follows the insertion order of hash keys.



34
35
36
37
38
# File 'rumale-pipeline/lib/rumale/pipeline/feature_union.rb', line 34

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

Instance Attribute Details

#transformersHash (readonly)

Return the transformers

Returns:

  • (Hash)


29
30
31
# File 'rumale-pipeline/lib/rumale/pipeline/feature_union.rb', line 29

def transformers
  @transformers
end

Instance Method Details

#fit(x, y = nil) ⇒ FeatureUnion

Fit the model with given training data.

Parameters:

  • x (Numo::DFloat)

    (shape: [n_samples, n_features]) The training data to be used for fitting the transformers.

  • y (Numo::NArray/Nil) (defaults to: nil)

    (shape: [n_samples, n_outputs]) The target values or labels to be used for fitting the transformers.

Returns:



45
46
47
48
# File 'rumale-pipeline/lib/rumale/pipeline/feature_union.rb', line 45

def fit(x, y = nil)
  @transformers.each_value { |t| t.fit(x, y) }
  self
end

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

Fit the model with training data, and then transform them with the learned model.

Parameters:

  • x (Numo::DFloat)

    (shape: [n_samples, n_features]) The training data to be used for fitting the transformers.

  • y (Numo::NArray/Nil) (defaults to: nil)

    (shape: [n_samples, n_outputs]) The target values or labels to be used for fitting the transformers.

Returns:

  • (Numo::DFloat)

    (shape: [n_samples, sum_n_components]) The transformed and concatenated data.



55
56
57
# File 'rumale-pipeline/lib/rumale/pipeline/feature_union.rb', line 55

def fit_transform(x, y = nil)
  fit(x, y).transform(x)
end

#transform(x) ⇒ Numo::DFloat

Transform the given data with the learned model.

Parameters:

  • x (Numo::DFloat)

    (shape: [n_samples, n_features]) The data to be transformed with the learned transformers.

Returns:

  • (Numo::DFloat)

    (shape: [n_samples, sum_n_components]) The transformed and concatenated data.



63
64
65
66
# File 'rumale-pipeline/lib/rumale/pipeline/feature_union.rb', line 63

def transform(x)
  z = @transformers.values.map { |t| t.transform(x) }
  Numo::NArray.hstack(z)
end