Class: Rumale::Preprocessing::OrdinalEncoder
- Inherits:
-
Base::Estimator
- Object
- Base::Estimator
- Rumale::Preprocessing::OrdinalEncoder
- Includes:
- Base::Transformer
- Defined in:
- rumale-preprocessing/lib/rumale/preprocessing/ordinal_encoder.rb
Overview
Transfrom categorical features to integer values.
Instance Attribute Summary collapse
-
#categories ⇒ Array
readonly
Return the array consists of categorical value each feature.
Attributes inherited from Base::Estimator
Instance Method Summary collapse
-
#fit(x) ⇒ LabelEncoder
Fit encoder by extracting the category for each feature.
-
#fit_transform(x) ⇒ Numo::DFloat
Fit encoder, then return encoded categorical features to integer values.
-
#initialize(categories: nil) ⇒ OrdinalEncoder
constructor
Create a new encoder that transform categorical features to integer values.
-
#inverse_transform(x) ⇒ Numo::NArray
Decode values to categorical features.
-
#transform(x) ⇒ Numo::DFloat
Encode categorical features.
Constructor Details
#initialize(categories: nil) ⇒ OrdinalEncoder
Create a new encoder that transform categorical features to integer values.
41 42 43 44 |
# File 'rumale-preprocessing/lib/rumale/preprocessing/ordinal_encoder.rb', line 41 def initialize(categories: nil) super() @categories = categories end |
Instance Attribute Details
#categories ⇒ Array (readonly)
Return the array consists of categorical value each feature.
35 36 37 |
# File 'rumale-preprocessing/lib/rumale/preprocessing/ordinal_encoder.rb', line 35 def categories @categories end |
Instance Method Details
#fit(x) ⇒ LabelEncoder
Fit encoder by extracting the category for each feature.
52 53 54 55 56 57 58 |
# File 'rumale-preprocessing/lib/rumale/preprocessing/ordinal_encoder.rb', line 52 def fit(x, _y = nil) raise ArgumentError, 'Expect sample matrix to be 2-D array' unless x.shape.size == 2 n_features = x.shape[1] @categories = Array.new(n_features) { |n| x[true, n].to_a.uniq.sort } self end |
#fit_transform(x) ⇒ Numo::DFloat
Fit encoder, then return encoded categorical features to integer values.
66 67 68 69 70 |
# File 'rumale-preprocessing/lib/rumale/preprocessing/ordinal_encoder.rb', line 66 def fit_transform(x, _y = nil) raise ArgumentError, 'Expect sample matrix to be 2-D array' unless x.shape.size == 2 fit(x).transform(x) end |
#inverse_transform(x) ⇒ Numo::NArray
Decode values to categorical features.
96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'rumale-preprocessing/lib/rumale/preprocessing/ordinal_encoder.rb', line 96 def inverse_transform(x) n_features = x.shape[1] if n_features != @categories.size raise ArgumentError, 'Expect the number of features and the number of categories to be equal' end inv_transformed = Array.new(n_features) do |n| x[true, n].to_a.map { |i| @categories[n][i.to_i] } end Numo::NArray.asarray(inv_transformed.transpose) end |
#transform(x) ⇒ Numo::DFloat
Encode categorical features.
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'rumale-preprocessing/lib/rumale/preprocessing/ordinal_encoder.rb', line 76 def transform(x) raise ArgumentError, 'Expect sample matrix to be 2-D array' unless x.shape.size == 2 n_features = x.shape[1] if n_features != @categories.size raise ArgumentError, 'Expect the number of features and the number of categories to be equal' end transformed = Array.new(n_features) do |n| x[true, n].to_a.map { |v| @categories[n].index(v) } end Numo::DFloat.asarray(transformed.transpose) end |