Class OneVersusOne<T>

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

public class OneVersusOne<T> extends AbstractClassifier<T>
One-vs-one strategy for reducing the problem of multiclass classification to multiple binary classification problems. This approach trains K (K − 1) / 2 binary classifiers for a K-way multiclass problem; each receives the samples of a pair of classes from the original training set, and must learn to distinguish these two classes. At prediction time, a voting scheme is applied: all K (K − 1) / 2 classifiers are applied to an unseen sample and the class that got the highest number of positive predictions gets predicted by the combined classifier. Like One-vs-rest, one-vs-one suffers from ambiguities in that some regions of its input space may receive the same number of votes.
See Also:
  • Constructor Details

    • OneVersusOne

      public OneVersusOne(Classifier<T>[][] classifiers, PlattScaling[][] platt)
      Constructor.
      Parameters:
      classifiers - the binary classifier for each one-vs-one case. Only the lower half is needed.
      platt - Platt scaling models.
    • OneVersusOne

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

    • fit

      public static <T> OneVersusOne<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> OneVersusOne<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)
      Prediction is based on voting.
      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)
      Prediction is based posteriori probability estimation. The result may be different from predict(T x).
      Parameters:
      x - an instance to be classified.
      posteriori - a posteriori probabilities on output.
      Returns:
      the predicted class label