Class: Rumale::Preprocessing::LabelEncoder
- Inherits:
-
Object
- Object
- Rumale::Preprocessing::LabelEncoder
- Includes:
- Base::BaseEstimator, Base::Transformer
- Defined in:
- 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 included from Base::BaseEstimator
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.
-
#marshal_dump ⇒ Hash
Dump marshal data.
-
#marshal_load(obj) ⇒ nil
Load marshal data.
-
#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.
29 30 31 32 |
# File 'lib/rumale/preprocessing/label_encoder.rb', line 29 def initialize @params = {} @classes = nil end |
Instance Attribute Details
#classes ⇒ Array (readonly)
Return the class labels.
26 27 28 |
# File 'lib/rumale/preprocessing/label_encoder.rb', line 26 def classes @classes end |
Instance Method Details
#fit(x) ⇒ LabelEncoder
Fit label-encoder to labels.
40 41 42 43 44 45 |
# File 'lib/rumale/preprocessing/label_encoder.rb', line 40 def fit(x, _y = nil) x = x.to_a if x.is_a?(Numo::NArray) check_params_type(Array, x: x) @classes = x.sort.uniq self end |
#fit_transform(x) ⇒ Numo::Int32
Fit label-encoder to labels, then return encoded labels.
53 54 55 56 57 |
# File 'lib/rumale/preprocessing/label_encoder.rb', line 53 def fit_transform(x, _y = nil) x = x.to_a if x.is_a?(Numo::NArray) check_params_type(Array, x: x) fit(x).transform(x) end |
#inverse_transform(x) ⇒ Array
Decode encoded labels.
73 74 75 76 |
# File 'lib/rumale/preprocessing/label_encoder.rb', line 73 def inverse_transform(x) check_label_array(x) x.to_a.map { |n| @classes[n] } end |
#marshal_dump ⇒ Hash
Dump marshal data.
80 81 82 83 |
# File 'lib/rumale/preprocessing/label_encoder.rb', line 80 def marshal_dump { params: @params, classes: @classes } end |
#marshal_load(obj) ⇒ nil
Load marshal data.
87 88 89 90 91 |
# File 'lib/rumale/preprocessing/label_encoder.rb', line 87 def marshal_load(obj) @params = obj[:params] @classes = obj[:classes] nil end |
#transform(x) ⇒ Numo::Int32
Encode labels.
63 64 65 66 67 |
# File 'lib/rumale/preprocessing/label_encoder.rb', line 63 def transform(x) x = x.to_a if x.is_a?(Numo::NArray) check_params_type(Array, x: x) Numo::Int32[*(x.map { |v| @classes.index(v) })] end |