Class: Rumale::Preprocessing::OneHotEncoder
- Inherits:
-
Base::Estimator
- Object
- Base::Estimator
- Rumale::Preprocessing::OneHotEncoder
- Includes:
- Base::Transformer
- Defined in:
- rumale-preprocessing/lib/rumale/preprocessing/one_hot_encoder.rb
Overview
Encode categorical integer features to one-hot-vectors.
Instance Attribute Summary collapse
-
#active_features ⇒ Nimo::Int32
readonly
Return the indices for feature values that actually occur in the training set.
-
#feature_indices ⇒ Numo::Int32
readonly
Return the indices to feature ranges.
-
#n_values ⇒ Numo::Int32
readonly
Return the maximum values for each feature.
Attributes inherited from Base::Estimator
Instance Method Summary collapse
-
#fit(x) ⇒ OneHotEncoder
Fit one-hot-encoder to samples.
-
#fit_transform(x) ⇒ Numo::DFloat
Fit one-hot-encoder to samples, then encode samples into one-hot-vectors.
-
#initialize ⇒ OneHotEncoder
constructor
Create a new encoder for encoding categorical integer features to one-hot-vectors.
-
#transform(x) ⇒ Numo::DFloat
Encode samples into one-hot-vectors.
Constructor Details
#initialize ⇒ OneHotEncoder
Create a new encoder for encoding categorical integer features to one-hot-vectors
40 41 42 |
# File 'rumale-preprocessing/lib/rumale/preprocessing/one_hot_encoder.rb', line 40 def initialize # rubocop:disable Lint/UselessMethodDefinition super() end |
Instance Attribute Details
#active_features ⇒ Nimo::Int32 (readonly)
Return the indices for feature values that actually occur in the training set.
33 34 35 |
# File 'rumale-preprocessing/lib/rumale/preprocessing/one_hot_encoder.rb', line 33 def active_features @active_features end |
#feature_indices ⇒ Numo::Int32 (readonly)
Return the indices to feature ranges.
37 38 39 |
# File 'rumale-preprocessing/lib/rumale/preprocessing/one_hot_encoder.rb', line 37 def feature_indices @feature_indices end |
#n_values ⇒ Numo::Int32 (readonly)
Return the maximum values for each feature.
29 30 31 |
# File 'rumale-preprocessing/lib/rumale/preprocessing/one_hot_encoder.rb', line 29 def n_values @n_values end |
Instance Method Details
#fit(x) ⇒ OneHotEncoder
Fit one-hot-encoder to samples.
49 50 51 52 53 54 55 56 |
# File 'rumale-preprocessing/lib/rumale/preprocessing/one_hot_encoder.rb', line 49 def fit(x, _y = nil) raise ArgumentError, 'Expected the input samples only consists of non-negative integer values.' if x.lt(0).any? @n_values = x.max(0) + 1 @feature_indices = Numo::Int32.hstack([[0], @n_values]).cumsum @active_features = encode(x, @feature_indices).sum(axis: 0).ne(0).where self end |
#fit_transform(x) ⇒ Numo::DFloat
Fit one-hot-encoder to samples, then encode samples into one-hot-vectors
64 65 66 67 68 |
# File 'rumale-preprocessing/lib/rumale/preprocessing/one_hot_encoder.rb', line 64 def fit_transform(x, _y = nil) raise ArgumentError, 'Expected the input samples only consists of non-negative integer values.' if x.lt(0).any? fit(x).transform(x) end |
#transform(x) ⇒ Numo::DFloat
Encode samples into one-hot-vectors.
74 75 76 77 78 79 |
# File 'rumale-preprocessing/lib/rumale/preprocessing/one_hot_encoder.rb', line 74 def transform(x) raise ArgumentError, 'Expected the input samples only consists of non-negative integer values.' if x.lt(0).any? codes = encode(x, @feature_indices) codes[true, @active_features].dup end |