Class: Rumale::Preprocessing::LabelEncoder
- Inherits:
-
Base::Estimator
- Object
- Base::Estimator
- Rumale::Preprocessing::LabelEncoder
- Includes:
- Base::Transformer
- Defined in:
- rumale-preprocessing/lib/rumale/preprocessing/label_encoder.rb
Overview
Encode labels to values between 0 and n_classes - 1.
Instance Attribute Summary collapse
-
#classes ⇒ Array
readonly
Return the class labels.
Attributes inherited from Base::Estimator
Instance Method Summary collapse
-
#fit(x) ⇒ LabelEncoder
Fit label-encoder to labels.
-
#fit_transform(x) ⇒ Numo::Int32
Fit label-encoder to labels, then return encoded labels.
-
#initialize ⇒ LabelEncoder
constructor
Create a new encoder for encoding labels to values between 0 and n_classes - 1.
-
#inverse_transform(x) ⇒ Array
Decode encoded labels.
-
#transform(x) ⇒ Numo::Int32
Encode labels.
Constructor Details
#initialize ⇒ LabelEncoder
Create a new encoder for encoding labels to values between 0 and n_classes - 1.
30 31 32 |
# File 'rumale-preprocessing/lib/rumale/preprocessing/label_encoder.rb', line 30 def initialize # rubocop:disable Lint/UselessMethodDefinition super() end |
Instance Attribute Details
#classes ⇒ Array (readonly)
Return the class labels.
27 28 29 |
# File 'rumale-preprocessing/lib/rumale/preprocessing/label_encoder.rb', line 27 def classes @classes end |
Instance Method Details
#fit(x) ⇒ LabelEncoder
Fit label-encoder to labels.
40 41 42 43 44 |
# File 'rumale-preprocessing/lib/rumale/preprocessing/label_encoder.rb', line 40 def fit(x, _y = nil) x = x.to_a if x.is_a?(Numo::NArray) @classes = x.sort.uniq self end |
#fit_transform(x) ⇒ Numo::Int32
Fit label-encoder to labels, then return encoded labels.
52 53 54 55 |
# File 'rumale-preprocessing/lib/rumale/preprocessing/label_encoder.rb', line 52 def fit_transform(x, _y = nil) x = x.to_a if x.is_a?(Numo::NArray) fit(x).transform(x) end |
#inverse_transform(x) ⇒ Array
Decode encoded labels.
70 71 72 |
# File 'rumale-preprocessing/lib/rumale/preprocessing/label_encoder.rb', line 70 def inverse_transform(x) x.to_a.map { |n| @classes[n] } end |
#transform(x) ⇒ Numo::Int32
Encode labels.
61 62 63 64 |
# File 'rumale-preprocessing/lib/rumale/preprocessing/label_encoder.rb', line 61 def transform(x) x = x.to_a if x.is_a?(Numo::NArray) Numo::Int32[*(x.map { |v| @classes.index(v) })] end |