Class: Rumale::Preprocessing::OrdinalEncoder
- Inherits:
-
Object
- Object
- Rumale::Preprocessing::OrdinalEncoder
- Includes:
- Base::BaseEstimator, Base::Transformer
- Defined in:
- 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 included from Base::BaseEstimator
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.
-
#marshal_dump ⇒ Hash
Dump marshal data.
-
#marshal_load(obj) ⇒ nil
Load marshal data.
-
#transform(x) ⇒ Numo::DFloat
Encode categorical features.
Constructor Details
#initialize(categories: nil) ⇒ OrdinalEncoder
Create a new encoder that transform categorical features to integer values.
40 41 42 43 |
# File 'lib/rumale/preprocessing/ordinal_encoder.rb', line 40 def initialize(categories: nil) check_params_type_or_nil(Array, categories: categories) @categories = categories end |
Instance Attribute Details
#categories ⇒ Array (readonly)
Return the array consists of categorical value each feature.
34 35 36 |
# File 'lib/rumale/preprocessing/ordinal_encoder.rb', line 34 def categories @categories end |
Instance Method Details
#fit(x) ⇒ LabelEncoder
Fit encoder by extracting the category for each feature.
51 52 53 54 55 56 57 |
# File 'lib/rumale/preprocessing/ordinal_encoder.rb', line 51 def fit(x, _y = nil) raise TypeError, 'Expect class of sample matrix to be Numo::NArray' unless x.is_a?(Numo::NArray) 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.
65 66 67 68 69 |
# File 'lib/rumale/preprocessing/ordinal_encoder.rb', line 65 def fit_transform(x, _y = nil) raise TypeError, 'Expect class of sample matrix to be Numo::NArray' unless x.is_a?(Numo::NArray) 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.
93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/rumale/preprocessing/ordinal_encoder.rb', line 93 def inverse_transform(x) check_sample_array(x) n_features = x.shape[1] raise ArgumentError, 'Expect the number of features and the number of categories to be equal' if n_features != @categories.size 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 |
#marshal_dump ⇒ Hash
Dump marshal data.
108 109 110 |
# File 'lib/rumale/preprocessing/ordinal_encoder.rb', line 108 def marshal_dump { categories: @categories } end |
#marshal_load(obj) ⇒ nil
Load marshal data.
114 115 116 117 |
# File 'lib/rumale/preprocessing/ordinal_encoder.rb', line 114 def marshal_load(obj) @categories = obj[:categories] nil end |
#transform(x) ⇒ Numo::DFloat
Encode categorical features.
75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/rumale/preprocessing/ordinal_encoder.rb', line 75 def transform(x) raise TypeError, 'Expect class of sample matrix to be Numo::NArray' unless x.is_a?(Numo::NArray) raise ArgumentError, 'Expect sample matrix to be 2-D array' unless x.shape.size == 2 n_features = x.shape[1] raise ArgumentError, 'Expect the number of features and the number of categories to be equal' if n_features != @categories.size 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 |