Class: Rumale::Preprocessing::LabelBinarizer
- Inherits:
-
Base::Estimator
- Object
- Base::Estimator
- Rumale::Preprocessing::LabelBinarizer
- Includes:
- Base::Transformer
- Defined in:
- rumale-preprocessing/lib/rumale/preprocessing/label_binarizer.rb
Overview
Encode labels to binary labels with one-vs-all scheme.
Instance Attribute Summary collapse
-
#classes ⇒ Array
readonly
Return the class labels.
Attributes inherited from Base::Estimator
Instance Method Summary collapse
-
#fit(y) ⇒ LabelBinarizer
Fit encoder to labels.
-
#fit_transform(y) ⇒ Numo::Int32
Fit encoder to labels, then return binarized labels.
-
#initialize(neg_label: 0, pos_label: 1) ⇒ LabelBinarizer
constructor
Create a new encoder for binarizing labels with one-vs-all scheme.
-
#inverse_transform(x) ⇒ Array
Decode binarized labels.
-
#transform(y) ⇒ Numo::Int32
Encode labels.
Constructor Details
#initialize(neg_label: 0, pos_label: 1) ⇒ LabelBinarizer
Create a new encoder for binarizing labels with one-vs-all scheme.
34 35 36 37 38 39 40 |
# File 'rumale-preprocessing/lib/rumale/preprocessing/label_binarizer.rb', line 34 def initialize(neg_label: 0, pos_label: 1) super() @params = { neg_label: neg_label, pos_label: pos_label } end |
Instance Attribute Details
#classes ⇒ Array (readonly)
Return the class labels.
28 29 30 |
# File 'rumale-preprocessing/lib/rumale/preprocessing/label_binarizer.rb', line 28 def classes @classes end |
Instance Method Details
#fit(y) ⇒ LabelBinarizer
Fit encoder to labels.
47 48 49 50 51 |
# File 'rumale-preprocessing/lib/rumale/preprocessing/label_binarizer.rb', line 47 def fit(y, _not_used = nil) y = y.to_a if y.is_a?(Numo::NArray) @classes = y.uniq.sort self end |
#fit_transform(y) ⇒ Numo::Int32
Fit encoder to labels, then return binarized labels.
58 59 60 61 |
# File 'rumale-preprocessing/lib/rumale/preprocessing/label_binarizer.rb', line 58 def fit_transform(y, _not_used = nil) y = y.to_a if y.is_a?(Numo::NArray) fit(y).transform(y) end |
#inverse_transform(x) ⇒ Array
Decode binarized labels.
80 81 82 83 |
# File 'rumale-preprocessing/lib/rumale/preprocessing/label_binarizer.rb', line 80 def inverse_transform(x) n_samples = x.shape[0] Array.new(n_samples) { |n| @classes[x[n, true].ne(@params[:neg_label]).where[0]] } end |
#transform(y) ⇒ Numo::Int32
Encode labels.
67 68 69 70 71 72 73 74 |
# File 'rumale-preprocessing/lib/rumale/preprocessing/label_binarizer.rb', line 67 def transform(y) y = y.to_a if y.is_a?(Numo::NArray) n_classes = @classes.size n_samples = y.size codes = Numo::Int32.zeros(n_samples, n_classes) + @params[:neg_label] n_samples.times { |n| codes[n, @classes.index(y[n])] = @params[:pos_label] } codes end |