Class: Rumale::Multiclass::OneVsRestClassifier

Inherits:
Object
  • Object
show all
Includes:
Base::BaseEstimator, Base::Classifier
Defined in:
lib/rumale/multiclass/one_vs_rest_classifier.rb

Overview

Note:

All classifier in Rumale support multi-class classifiction since version 0.2.7. There is no need to explicitly use this class for multiclass classifiction.

OneVsRestClassifier is a class that implements One-vs-Rest (OvR) strategy for multi-class classification.

Examples:

base_estimator = Rumale::LinearModel::LogisticRegression.new
estimator = Rumale::Multiclass::OneVsRestClassifier.new(estimator: base_estimator)
estimator.fit(training_samples, training_labels)
results = estimator.predict(testing_samples)

Instance Attribute Summary collapse

Attributes included from Base::BaseEstimator

#params

Instance Method Summary collapse

Methods included from Base::Classifier

#score

Constructor Details

#initialize(estimator: nil) ⇒ OneVsRestClassifier

Create a new multi-class classifier with the one-vs-rest startegy.

Parameters:

  • estimator (Classifier) (defaults to: nil)

    The (binary) classifier for construction a multi-class classifier.



35
36
37
38
39
40
41
# File 'lib/rumale/multiclass/one_vs_rest_classifier.rb', line 35

def initialize(estimator: nil)
  check_params_type(Rumale::Base::BaseEstimator, estimator: estimator)
  @params = {}
  @params[:estimator] = estimator
  @estimators = nil
  @classes = nil
end

Instance Attribute Details

#classesNumo::Int32 (readonly)

Return the class labels.

Returns:

  • (Numo::Int32)

    (shape: [n_classes])



30
31
32
# File 'lib/rumale/multiclass/one_vs_rest_classifier.rb', line 30

def classes
  @classes
end

#estimatorsArray<Classifier> (readonly)

Return the set of estimators.

Returns:

  • (Array<Classifier>)


26
27
28
# File 'lib/rumale/multiclass/one_vs_rest_classifier.rb', line 26

def estimators
  @estimators
end

Instance Method Details

#decision_function(x) ⇒ Numo::DFloat

Calculate confidence scores for samples.

Parameters:

  • x (Numo::DFloat)

    (shape: [n_samples, n_features]) The samples to compute the scores.

Returns:

  • (Numo::DFloat)

    (shape: [n_samples, n_classes]) Confidence scores per sample for each class.



65
66
67
68
69
# File 'lib/rumale/multiclass/one_vs_rest_classifier.rb', line 65

def decision_function(x)
  check_sample_array(x)
  n_classes = @classes.size
  Numo::DFloat.asarray(Array.new(n_classes) { |m| @estimators[m].decision_function(x).to_a }).transpose
end

#fit(x, y) ⇒ OneVsRestClassifier

Fit the model with given training data.

Parameters:

  • x (Numo::DFloat)

    (shape: [n_samples, n_features]) The training data to be used for fitting the model.

  • y (Numo::Int32)

    (shape: [n_samples]) The labels to be used for fitting the model.

Returns:



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/rumale/multiclass/one_vs_rest_classifier.rb', line 48

def fit(x, y)
  check_sample_array(x)
  check_label_array(y)
  check_sample_label_size(x, y)
  y_arr = y.to_a
  @classes = Numo::Int32.asarray(y_arr.uniq.sort)
  @estimators = @classes.to_a.map do |label|
    bin_y = Numo::Int32.asarray(y_arr.map { |l| l == label ? 1 : -1 })
    @params[:estimator].dup.fit(x, bin_y)
  end
  self
end

#marshal_dumpHash

Dump marshal data.

Returns:

  • (Hash)

    The marshal data about OneVsRestClassifier.



84
85
86
87
88
# File 'lib/rumale/multiclass/one_vs_rest_classifier.rb', line 84

def marshal_dump
  { params: @params,
    classes: @classes,
    estimators: @estimators.map { |e| Marshal.dump(e) } }
end

#marshal_load(obj) ⇒ nil

Load marshal data.

Returns:

  • (nil)


92
93
94
95
96
97
# File 'lib/rumale/multiclass/one_vs_rest_classifier.rb', line 92

def marshal_load(obj)
  @params = obj[:params]
  @classes = obj[:classes]
  @estimators = obj[:estimators].map { |e| Marshal.load(e) }
  nil
end

#predict(x) ⇒ Numo::Int32

Predict class labels for samples.

Parameters:

  • x (Numo::DFloat)

    (shape: [n_samples, n_features]) The samples to predict the labels.

Returns:

  • (Numo::Int32)

    (shape: [n_samples]) Predicted class label per sample.



75
76
77
78
79
80
# File 'lib/rumale/multiclass/one_vs_rest_classifier.rb', line 75

def predict(x)
  check_sample_array(x)
  n_samples, = x.shape
  decision_values = decision_function(x)
  Numo::Int32.asarray(Array.new(n_samples) { |n| @classes[decision_values[n, true].max_index] })
end