stanfordnlp/CoreNLP · error · RuntimeException

LogisticClassifier is only for binary classification!

Error message

LogisticClassifier is only for binary classification!

What it means

LogisticClassifier is a binary-only classifier: trainWeightedData requires the dataset's label index to contain exactly 2 labels. If the GeneralDataset has more (or fewer) labels, a RuntimeException is thrown before training starts. Use other classifiers for multiclass problems.

Solutions

  1. Reduce/verify the dataset has exactly two labels before training
  2. For multiclass use LogisticClassifierFactory's one-vs-all wrapping or a LinearClassifier/OGBoost instead
  3. Check data.labelIndex.size() as a pre-flight validation and handle it in your pipeline

Example fix

// before
classifier.trainWeightedData(multiclassData, weights);
// after
LogisticClassifier<L,F> c = new LogisticClassifierFactory<L,F>().trainClassifier(oneVsRestData, 0.0, 1e-4, true);
Defensive patterns

Strategy: validation

Validate before calling

if (data.labelIndex.size() != 2)
  throw new IllegalArgumentException("LogisticClassifier needs exactly 2 labels, found " + data.labelIndex.size());

Type guard

boolean isBinary(GeneralDataset<?,?> d) {
  return d != null && d.labelIndex != null && d.labelIndex.size() == 2;
}

Try / catch

try {
  classifier.trainWeightedData(data, weights);
} catch (RuntimeException e) {
  if (e.getMessage().contains("binary")) {
    LinearClassifier<L,F> lc = new LinearClassifierFactory<L,F>().trainClassifier(data);
  } else throw e;
}

Prevention

When it happens

Trigger: Calling the deprecated trainWeightedData(GeneralDataset, float[]) with a dataset whose labelIndex.size() != 2 — e.g., a 3-class Dataset or an empty-label dataset.

Common situations: Feeding a multiclass dataset (e.g., 3+ classes) into logistic training; datasets built with label indices that accidentally contain extra labels; migrating from LinearClassifier which supports multiclass.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10). Data as JSON: /api/errors/5c6e1a70a1263a98. Report an issue: GitHub.

Appendix: source

Thrown at src/edu/stanford/nlp/classify/LogisticClassifier.java:293

  }

  private double probabilityOfRVFDatum(RVFDatum<L, F> example) {
    return probabilityOf(example.asFeaturesCounter(), example.label());
  }

  public double probabilityOf(Counter<F> features, L label) {
    short sign = (short)(label.equals(classes[0]) ? 1 : -1);
    return 1.0 / (1.0 + Math.exp(sign * scoreOf(features)));
  }

  /**
   * Trains on weighted dataset.
   * @param dataWeights weights of the data.
   */
  @Deprecated //Use LogisticClassifierFactory to train instead.
  public void trainWeightedData(GeneralDataset<L,F> data, float[] dataWeights){
    if (data.labelIndex.size() != 2) {
      throw new RuntimeException("LogisticClassifier is only for binary classification!");
    }

    Minimizer<DiffFunction> minim;
      LogisticObjectiveFunction lof = null;
      if(data instanceof Dataset<?,?>)
        lof = new LogisticObjectiveFunction(data.numFeatureTypes(), data.getDataArray(), data.getLabelsArray(), prior,dataWeights);
      else if(data instanceof RVFDataset<?,?>)
        lof = new LogisticObjectiveFunction(data.numFeatureTypes(), data.getDataArray(), data.getValuesArray(), data.getLabelsArray(), prior,dataWeights);
      minim = new QNMinimizer(lof);
      weights = minim.minimize(lof, 1e-4, new double[data.numFeatureTypes()]);

    featureIndex = data.featureIndex;
    classes[0] = data.labelIndex.get(0);
    classes[1] = data.labelIndex.get(1);
  }

  @Deprecated //Use LogisticClassifierFactory to train instead.
  public void train(GeneralDataset<L, F> data) {

View on GitHub (pinned to 1b7edd19c4)