Class: Rumale::Pipeline::Pipeline

Inherits:
Object
  • Object
show all
Includes:
Base::BaseEstimator
Defined in:
lib/rumale/pipeline/pipeline.rb

Overview

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

Examples:

rbf = Rumale::KernelApproximation::RBF.new(gamma: 1.0, n_coponents: 128, random_seed: 1)
svc = Rumale::LinearModel::SVC.new(reg_param: 1.0, fit_bias: true, max_iter: 5000, random_seed: 1)
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 included from Base::BaseEstimator

#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 'lib/rumale/pipeline/pipeline.rb', line 30

def initialize(steps:)
  check_params_type(Hash, steps: steps)
  validate_steps(steps)
  @params = {}
  @steps = steps
end

Instance Attribute Details

#stepsHash (readonly)

Return the steps.

Returns:

  • (Hash)


24
25
26
# File '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.



75
76
77
78
79
# File 'lib/rumale/pipeline/pipeline.rb', line 75

def decision_function(x)
  check_sample_array(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
47
# File 'lib/rumale/pipeline/pipeline.rb', line 42

def fit(x, y)
  check_sample_array(x)
  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.



54
55
56
57
58
# File 'lib/rumale/pipeline/pipeline.rb', line 54

def fit_predict(x, y = nil)
  check_sample_array(x)
  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.



65
66
67
68
69
# File 'lib/rumale/pipeline/pipeline.rb', line 65

def fit_transform(x, y = nil)
  check_sample_array(x)
  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.



125
126
127
128
129
130
131
132
133
134
# File 'lib/rumale/pipeline/pipeline.rb', line 125

def inverse_transform(z)
  check_sample_array(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

#marshal_dumpHash

Dump marshal data.

Returns:

  • (Hash)

    The marshal data about Pipeline.



149
150
151
152
# File 'lib/rumale/pipeline/pipeline.rb', line 149

def marshal_dump
  { params: @params,
    steps: @steps }
end

#marshal_load(obj) ⇒ nil

Load marshal data.

Returns:

  • (nil)


156
157
158
159
160
# File 'lib/rumale/pipeline/pipeline.rb', line 156

def marshal_load(obj)
  @params = obj[:params]
  @steps = obj[:steps]
  nil
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.



85
86
87
88
89
# File 'lib/rumale/pipeline/pipeline.rb', line 85

def predict(x)
  check_sample_array(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.



95
96
97
98
99
# File 'lib/rumale/pipeline/pipeline.rb', line 95

def predict_log_proba(x)
  check_sample_array(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.



105
106
107
108
109
# File 'lib/rumale/pipeline/pipeline.rb', line 105

def predict_proba(x)
  check_sample_array(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



141
142
143
144
145
# File 'lib/rumale/pipeline/pipeline.rb', line 141

def score(x, y)
  check_sample_array(x)
  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.



115
116
117
118
119
# File 'lib/rumale/pipeline/pipeline.rb', line 115

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