stanfordnlp/CoreNLP · error · RuntimeException

LogisticClassifier is only for binary classification!

Error message

LogisticClassifier is only for binary classification!

What it means

LogisticClassifierFactory.trainWeightedData checks that the dataset is binary (labelIndex.size() == 2) before building the LogisticObjectiveFunction; otherwise it throws this RuntimeException. The factory adds ensureRealValues() for RVFDatasets first, but the binary invariant still applies.

Solutions

  1. Filter or binarize the dataset to exactly two labels before training
  2. For multiclass, wrap in a one-vs-rest loop over LogisticClassifierFactory or use a multiclass-capable classifier
  3. Validate data.labelIndex.size() == 2 as an early pipeline check

Example fix

// before
factory.trainWeightedData(multiclassData, weights);
// after
for (L posLabel : labels) {
  GeneralDataset<L,F> bin = Dataset.binaryOneVsRest(multiclassData, posLabel);
  factory.trainWeightedData(bin, weights);
}
Defensive patterns

Strategy: validation

Validate before calling

if (data.labelIndex.size() != 2)
  throw new IllegalArgumentException("LogisticClassifierFactory needs binary data, got " + data.labelIndex.size());

Type guard

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

Try / catch

try {
  LogisticClassifier<L,F> c = factory.trainWeightedData(data, weights);
} catch (RuntimeException e) {
  if (e.getMessage().contains("binary")) throw new IllegalArgumentException("Use a multiclass classifier for this dataset", e);
  throw e;
}

Prevention

When it happens

Trigger: Calling trainWeightedData(GeneralDataset, float[]) with a dataset having more than two (or zero) labels — commonly an RVFDataset or Dataset with 3+ classes.

Common situations: Multiclass sentiment/topic data passed to logistic factory training; label index polluted by extra label strings from a merged dataset; expecting automatic one-vs-all behavior which the factory does not perform.

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/e2ad97f84749a685. Report an issue: GitHub.

Appendix: source

Thrown at src/edu/stanford/nlp/classify/LogisticClassifierFactory.java:33

 * This uses the standard statistics textbook formulation of binary
 * logistic regression, which is more efficient than using the
 * LinearClassifier class.
 * 
 * @author Ramesh Nallapati nmramesh@cs.stanford.edu
 * 
 */
public class LogisticClassifierFactory<L,F> implements ClassifierFactory<L, F, LogisticClassifier<L,F>> {
  private static final long serialVersionUID = 1L;
  private double[] weights;
  private Index<F> featureIndex;
  private L[] classes = ErasureUtils.<L>mkTArray(Object.class,2);


  public LogisticClassifier<L,F> trainWeightedData(GeneralDataset<L,F> data, float[] dataWeights){
    if(data instanceof RVFDataset)
      ((RVFDataset<L,F>)data).ensureRealValues();
    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(), new LogPrior(LogPrior.LogPriorType.QUADRATIC),dataWeights);
    else if(data instanceof RVFDataset<?,?>)
      lof = new LogisticObjectiveFunction(data.numFeatureTypes(), data.getDataArray(), data.getValuesArray(), data.getLabelsArray(), new LogPrior(LogPrior.LogPriorType.QUADRATIC),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);
    return new LogisticClassifier<>(weights, featureIndex, classes);
  }

  public LogisticClassifier<L,F> trainClassifier(GeneralDataset<L, F> data) {

View on GitHub (pinned to 1b7edd19c4)