Class: Rumale::Preprocessing::PolynomialFeatures
- Inherits:
-
Base::Estimator
- Object
- Base::Estimator
- Rumale::Preprocessing::PolynomialFeatures
- Includes:
- Base::Transformer
- Defined in:
- rumale-preprocessing/lib/rumale/preprocessing/polynomial_features.rb
Overview
Generating polynomial features from the given samples.
Instance Attribute Summary collapse
-
#n_output_features ⇒ Integer
readonly
Return the number of polynomial features.
Attributes inherited from Base::Estimator
Instance Method Summary collapse
-
#fit(x) ⇒ PolynomialFeatures
Calculate the number of output polynomial fetures.
-
#fit_transform(x) ⇒ Numo::DFloat
Calculate the number of polynomial features, and then transform samples to polynomial features.
-
#initialize(degree: 2) ⇒ PolynomialFeatures
constructor
Create a transformer for generating polynomial features.
-
#transform(x) ⇒ Numo::DFloat
Transform the given samples to polynomial features.
Constructor Details
#initialize(degree: 2) ⇒ PolynomialFeatures
Create a transformer for generating polynomial features.
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_features ⇒ Integer (readonly)
Return the number of polynomial features.
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.
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.
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.
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 |