Class: Rumale::Preprocessing::PolynomialFeatures

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

Overview

Generating polynomial features from the given samples.

Examples:

require 'rumale/preprocessing/polynomial_features'

transformer = Rumale::Preprocessing::PolynomialFeatures.new(degree: 2)
x = Numo::DFloat[[0, 1], [2, 3], [4, 5]]
z = transformer.fit_transform(x)
p z

# Numo::DFloat#shape=[3,6]
# [[1, 0, 1, 0, 0, 1],
#  [1, 2, 3, 4, 6, 9],
#  [1, 4, 5, 16, 20, 25]]

# If you want to perform polynomial regression, combine it with LinearRegression as follows:
require 'rumale/preprocessing/polynomial_features'
require 'rumale/linear_model/linear_regression'
require 'rumale/pipeline/pipeline'

ply = Rumale::Preprocessing::PolynomialFeatures.new(degree: 2)
reg = Rumale::LinearModel::LinearRegression.new(fit_bias: false, random_seed: 1)
pipeline = Rumale::Pipeline::Pipeline.new(steps: { trs: ply, est: reg })
pipeline.fit(training_samples, training_values)
results = pipeline.predict(testing_samples)

Instance Attribute Summary collapse

Attributes inherited from Base::Estimator

#params

Instance Method Summary collapse

Constructor Details

#initialize(degree: 2) ⇒ PolynomialFeatures

Create a transformer for generating polynomial features.

Parameters:

  • degree (Integer) (defaults to: 2)

    The degree of polynomial features.

Raises:

  • (ArgumentError)


45
46
47
48
49
50
# File 'rumale-preprocessing/lib/rumale/preprocessing/polynomial_features.rb', line 45

def initialize(degree: 2)
  raise ArgumentError, 'Expect the value of degree parameter greater than or eqaul to 1.' if degree < 1

  super()
  @params = { degree: degree }
end

Instance Attribute Details

#n_output_featuresInteger (readonly)

Return the number of polynomial features.

Returns:

  • (Integer)


40
41
42
# File 'rumale-preprocessing/lib/rumale/preprocessing/polynomial_features.rb', line 40

def n_output_features
  @n_output_features
end

Instance Method Details

#fit(x) ⇒ PolynomialFeatures

Calculate the number of output polynomial fetures.

Parameters:

  • x (Numo::DFloat)

    (shape: [n_samples, n_features]) The samples to calculate the number of output polynomial fetures.

Returns:



57
58
59
60
61
62
63
64
65
66
# File 'rumale-preprocessing/lib/rumale/preprocessing/polynomial_features.rb', line 57

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

  n_features = x.shape[1]
  @n_output_features = 1
  @params[:degree].times do |t|
    @n_output_features += Array.new(n_features) { |n| n }.repeated_combination(t + 1).size
  end
  self
end

#fit_transform(x) ⇒ Numo::DFloat

Calculate the number of polynomial features, and then transform samples to polynomial features.

Parameters:

  • x (Numo::DFloat)

    (shape: [n_samples, n_features]) The samples to calculate the number of polynomial features and be transformed.

Returns:

  • (Numo::DFloat)

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



74
75
76
77
78
# File 'rumale-preprocessing/lib/rumale/preprocessing/polynomial_features.rb', line 74

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

  fit(x).transform(x)
end

#transform(x) ⇒ Numo::DFloat

Transform the given samples to polynomial features.

Parameters:

  • x (Numo::DFloat)

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

Returns:

  • (Numo::DFloat)

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



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'rumale-preprocessing/lib/rumale/preprocessing/polynomial_features.rb', line 84

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

  # initialize transformed features
  n_samples, n_features = x.shape
  z = Numo::DFloat.zeros(n_samples, n_output_features)
  # bias
  z[true, 0] = 1
  curr_col = 1
  # itself
  z[true, 1..n_features] = x
  curr_col += n_features
  # high degree features
  curr_feat_ids = Array.new(n_features + 1) { |n| n + 1 }
  (1...@params[:degree]).each do
    next_feat_ids = []
    n_features.times do |d|
      f_range = curr_feat_ids[d]...curr_feat_ids.last
      next_col = curr_col + f_range.size
      z[true, curr_col...next_col] = z[true, f_range] * x[true, d..d]
      next_feat_ids.push(curr_col)
      curr_col = next_col
    end
    next_feat_ids.push(curr_col)
    curr_feat_ids = next_feat_ids
  end
  z
end