Class: Rumale::Pipeline::Pipeline

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

Overview

Pipeline is a class that implements the function to perform the transformers and estimators sequencially.

Examples:

require 'rumale/kernel_approximation/rbf'
require 'rumale/linear_model/svc'
require 'rumale/pipeline/pipeline'

rbf = Rumale::KernelApproximation::RBF.new(gamma: 1.0, n_components: 128, random_seed: 1)
svc = Rumale::LinearModel::SVC.new(reg_param: 1.0, fit_bias: true, max_iter: 5000)
pipeline = Rumale::Pipeline::Pipeline.new(steps: { trs: rbf, est: svc })
pipeline.fit(training_samples, traininig_labels)
results = pipeline.predict(testing_samples)

Instance Attribute Summary collapse

Attributes inherited from Base::Estimator

#params

Instance Method Summary collapse

Constructor Details

#initialize(steps:) ⇒ Pipeline

Create a new pipeline.

Parameters:

  • steps (Hash) —

    List of transformers and estimators. The order of transforms follows the insertion order of hash keys. The last entry is considered an estimator.



30
31
32
33
34
35
# File 'rumale-pipeline/lib/rumale/pipeline/pipeline.rb', line 30

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

Instance Attribute Details

#steps ⇒ Hash (readonly)

Return the steps.

Returns:

  • (Hash)


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

def steps
  @steps
end

Instance Method Details

#decision_function(x) ⇒ Numo::DFloat

Call the decision_function method of last estimator after applying all transforms.

Parameters:

  • x (Numo::DFloat) —

    (shape: [n_samples, n_features]) The samples to compute the scores.

Returns:

  • (Numo::DFloat) —

    (shape: [n_samples]) Confidence score per sample.



72
73
74
75
# File 'rumale-pipeline/lib/rumale/pipeline/pipeline.rb', line 72

def decision_function(x)
  trans_x = apply_transforms(x)
  last_estimator.decision_function(trans_x)
end

#fit(x, y) ⇒ Pipeline

Fit the model with given training data.

Parameters:

  • x (Numo::DFloat) —

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

  • y (Numo::NArray) —

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

Returns:

  • (Pipeline) —

    The learned pipeline itself.



42
43
44
45
46
# File 'rumale-pipeline/lib/rumale/pipeline/pipeline.rb', line 42

def fit(x, y)
  trans_x = apply_transforms(x, y, fit: true)
  last_estimator&.fit(trans_x, y)
  self
end

#fit_predict(x, y = nil) ⇒ Numo::NArray

Call the fit_predict method of last estimator after applying all transforms.

Parameters:

  • x (Numo::DFloat) —

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

  • y (Numo::NArray) (defaults to: nil) —

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

Returns:

  • (Numo::NArray) —

    The predicted results by last estimator.



53
54
55
56
# File 'rumale-pipeline/lib/rumale/pipeline/pipeline.rb', line 53

def fit_predict(x, y = nil)
  trans_x = apply_transforms(x, y, fit: true)
  last_estimator.fit_predict(trans_x)
end

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

Call the fit_transform method of last estimator after applying all transforms.

Parameters:

  • x (Numo::DFloat) —

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

  • y (Numo::NArray) (defaults to: nil) —

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

Returns:

  • (Numo::NArray) —

    The predicted results by last estimator.



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

def fit_transform(x, y = nil)
  trans_x = apply_transforms(x, y, fit: true)
  last_estimator.fit_transform(trans_x, y)
end

#inverse_transform(z) ⇒ Numo::DFloat

Call the inverse_transform method in reverse order.

Parameters:

  • z (Numo::DFloat) —

    (shape: [n_samples, n_components]) The transformed samples to be restored into original space.

Returns:

  • (Numo::DFloat) —

    (shape: [n_samples, n_featuress]) The restored samples.



117
118
119
120
121
122
123
124
125
126
# File 'rumale-pipeline/lib/rumale/pipeline/pipeline.rb', line 117

def inverse_transform(z)
  itrans_z = z
  @steps.keys.reverse_each do |name|
    transformer = @steps[name]
    next if transformer.nil?

    itrans_z = transformer.inverse_transform(itrans_z)
  end
  itrans_z
end

#predict(x) ⇒ Numo::NArray

Call the predict method of last estimator after applying all transforms.

Parameters:

  • x (Numo::DFloat) —

    (shape: [n_samples, n_features]) The samples to obtain prediction result.

Returns:

  • (Numo::NArray) —

    The predicted results by last estimator.



81
82
83
84
# File 'rumale-pipeline/lib/rumale/pipeline/pipeline.rb', line 81

def predict(x)
  trans_x = apply_transforms(x)
  last_estimator.predict(trans_x)
end

#predict_log_proba(x) ⇒ Numo::DFloat

Call the predict_log_proba method of last estimator after applying all transforms.

Parameters:

  • x (Numo::DFloat) —

    (shape: [n_samples, n_features]) The samples to predict the log-probailities.

Returns:

  • (Numo::DFloat) —

    (shape: [n_samples, n_classes]) Predicted log-probability of each class per sample.



90
91
92
93
# File 'rumale-pipeline/lib/rumale/pipeline/pipeline.rb', line 90

def predict_log_proba(x)
  trans_x = apply_transforms(x)
  last_estimator.predict_log_proba(trans_x)
end

#predict_proba(x) ⇒ Numo::DFloat

Call the predict_proba method of last estimator after applying all transforms.

Parameters:

  • x (Numo::DFloat) —

    (shape: [n_samples, n_features]) The samples to predict the probailities.

Returns:

  • (Numo::DFloat) —

    (shape: [n_samples, n_classes]) Predicted probability of each class per sample.



99
100
101
102
# File 'rumale-pipeline/lib/rumale/pipeline/pipeline.rb', line 99

def predict_proba(x)
  trans_x = apply_transforms(x)
  last_estimator.predict_proba(trans_x)
end

#score(x, y) ⇒ Float

Call the score method of last estimator after applying all transforms.

Parameters:

  • x (Numo::DFloat) —

    (shape: [n_samples, n_features]) Testing data.

  • y (Numo::NArray) —

    (shape: [n_samples, n_outputs]) True target values or labels for testing data.

Returns:

  • (Float) —

    The score of last estimator



133
134
135
136
# File 'rumale-pipeline/lib/rumale/pipeline/pipeline.rb', line 133

def score(x, y)
  trans_x = apply_transforms(x)
  last_estimator.score(trans_x, y)
end

#transform(x) ⇒ Numo::DFloat

Call the transform method of last estimator after applying all transforms.

Parameters:

  • x (Numo::DFloat) —

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

Returns:

  • (Numo::DFloat) —

    (shape: [n_samples, n_components]) The transformed samples.



108
109
110
111
# File 'rumale-pipeline/lib/rumale/pipeline/pipeline.rb', line 108

def transform(x)
  trans_x = apply_transforms(x)
  last_estimator.nil? ? trans_x : last_estimator.transform(trans_x)
end