Class OneVersusRest<T>

java.lang.Object
smile.classification.AbstractClassifier<T>
smile.classification.OneVersusRest<T>
All Implemented Interfaces:
Serializable, ToDoubleFunction<T>, ToIntFunction<T>, Classifier<T>

public class OneVersusRest<T> extends AbstractClassifier<T>
One-vs-rest (or one-vs-all) strategy for reducing the problem of multiclass classification to multiple binary classification problems. It involves training a single classifier per class, with the samples of that class as positive samples and all other samples as negatives. This strategy requires the base classifiers to produce a real-valued confidence score for its decision, rather than just a class label; discrete class labels alone can lead to ambiguities, where multiple classes are predicted for a single sample.

Making decisions means applying all classifiers to an unseen sample x and predicting the label k for which the corresponding classifier reports the highest confidence score.

Although this strategy is popular, it is a heuristic that suffers from several problems. Firstly, the scale of the confidence values may differ between the binary classifiers. Second, even if the class distribution is balanced in the training set, the binary classification learners see unbalanced distributions because typically the set of negatives they see is much larger than the set of positives.

See Also:
  • Constructor Details

    • OneVersusRest

      public OneVersusRest(Classifier<T>[] classifiers, PlattScaling[] platt)
      Constructor.
      Parameters:
      classifiers - the binary classifier for each one-vs-rest case.
      platt - Platt scaling models.
    • OneVersusRest

      public OneVersusRest(Classifier<T>[] classifiers, PlattScaling[] platt, IntSet labels)
      Constructor.
      Parameters:
      classifiers - the binary classifier for each one-vs-rest case.
      platt - Platt scaling models.
      labels - the class label encoder.
  • Method Details

    • fit

      public static <T> OneVersusRest<T> fit(T[] x, int[] y, BiFunction<T[],int[],Classifier<T>> trainer)
      Fits a multi-class model with binary classifiers. Use +1 and -1 as positive and negative class labels.
      Type Parameters:
      T - the data type.
      Parameters:
      x - the training samples.
      y - the training labels.
      trainer - the lambda to train binary classifiers.
      Returns:
      the model.
    • fit

      public static <T> OneVersusRest<T> fit(T[] x, int[] y, int pos, int neg, BiFunction<T[],int[],Classifier<T>> trainer)
      Fits a multi-class model with binary classifiers.
      Type Parameters:
      T - the data type.
      Parameters:
      x - the training samples.
      y - the training labels.
      pos - the class label for one case.
      neg - the class label for rest cases.
      trainer - the lambda to train binary classifiers.
      Returns:
      the model.
    • fit

      public static DataFrameClassifier fit(Formula formula, DataFrame data, BiFunction<Formula,DataFrame,DataFrameClassifier> trainer)
      Fits a multi-class model with binary data frame classifiers.
      Parameters:
      formula - a symbolic description of the model to be fitted.
      data - the data frame of the explanatory and response variables.
      trainer - the lambda to train binary classifiers.
      Returns:
      the model.
    • predict

      public int predict(T x)
      Description copied from interface: Classifier
      Predicts the class label of an instance.
      Parameters:
      x - the instance to be classified.
      Returns:
      the predicted class label.
    • soft

      public boolean soft()
      Description copied from interface: Classifier
      Returns true if this is a soft classifier that can estimate the posteriori probabilities of classification.
      Returns:
      true if soft classifier.
    • predict

      public int predict(T x, double[] posteriori)
      Description copied from interface: Classifier
      Predicts the class label of an instance and also calculate a posteriori probabilities. Classifiers may NOT support this method since not all classification algorithms are able to calculate such a posteriori probabilities.
      Parameters:
      x - an instance to be classified.
      posteriori - a posteriori probabilities on output.
      Returns:
      the predicted class label