{"record":{"id":"e2ad97f84749a685","repo":"stanfordnlp/CoreNLP","slug":"logisticclassifier-is-only-for-binary-classificati-e2ad97","errorCode":null,"errorMessage":"LogisticClassifier is only for binary classification!","messagePattern":"LogisticClassifier is only for binary classification!","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/classify/LogisticClassifierFactory.java","lineNumber":33,"sourceCode":" * This uses the standard statistics textbook formulation of binary\n * logistic regression, which is more efficient than using the\n * LinearClassifier class.\n * \n * @author Ramesh Nallapati nmramesh@cs.stanford.edu\n * \n */\npublic class LogisticClassifierFactory<L,F> implements ClassifierFactory<L, F, LogisticClassifier<L,F>> {\n  private static final long serialVersionUID = 1L;\n  private double[] weights;\n  private Index<F> featureIndex;\n  private L[] classes = ErasureUtils.<L>mkTArray(Object.class,2);\n\n\n  public LogisticClassifier<L,F> trainWeightedData(GeneralDataset<L,F> data, float[] dataWeights){\n    if(data instanceof RVFDataset)\n      ((RVFDataset<L,F>)data).ensureRealValues();\n    if (data.labelIndex.size() != 2) {\n      throw new RuntimeException(\"LogisticClassifier is only for binary classification!\");\n    }\n\n    Minimizer<DiffFunction> minim;\n    LogisticObjectiveFunction lof = null;\n    if(data instanceof Dataset<?,?>)\n      lof = new LogisticObjectiveFunction(data.numFeatureTypes(), data.getDataArray(), data.getLabelsArray(), new LogPrior(LogPrior.LogPriorType.QUADRATIC),dataWeights);\n    else if(data instanceof RVFDataset<?,?>)\n      lof = new LogisticObjectiveFunction(data.numFeatureTypes(), data.getDataArray(), data.getValuesArray(), data.getLabelsArray(), new LogPrior(LogPrior.LogPriorType.QUADRATIC),dataWeights);\n    minim = new QNMinimizer(lof);\n    weights = minim.minimize(lof, 1e-4, new double[data.numFeatureTypes()]);\n\n    featureIndex = data.featureIndex;\n    classes[0] = data.labelIndex.get(0);\n    classes[1] = data.labelIndex.get(1);\n    return new LogisticClassifier<>(weights, featureIndex, classes);\n  }\n\n  public LogisticClassifier<L,F> trainClassifier(GeneralDataset<L, F> data) {","sourceCodeStart":15,"sourceCodeEnd":51,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/classify/LogisticClassifierFactory.java#L15-L51","documentation":"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.","triggerScenarios":"Calling trainWeightedData(GeneralDataset, float[]) with a dataset having more than two (or zero) labels — commonly an RVFDataset or Dataset with 3+ classes.","commonSituations":"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.","solutions":["Filter or binarize the dataset to exactly two labels before training","For multiclass, wrap in a one-vs-rest loop over LogisticClassifierFactory or use a multiclass-capable classifier","Validate data.labelIndex.size() == 2 as an early pipeline check"],"exampleFix":"// before\nfactory.trainWeightedData(multiclassData, weights);\n// after\nfor (L posLabel : labels) {\n  GeneralDataset<L,F> bin = Dataset.binaryOneVsRest(multiclassData, posLabel);\n  factory.trainWeightedData(bin, weights);\n}","handlingStrategy":"validation","validationCode":"if (data.labelIndex.size() != 2)\n  throw new IllegalArgumentException(\"LogisticClassifierFactory needs binary data, got \" + data.labelIndex.size());","typeGuard":"boolean isBinary(GeneralDataset<?,?> d) {\n  return d.labelIndex.size() == 2;\n}","tryCatchPattern":"try {\n  LogisticClassifier<L,F> c = factory.trainWeightedData(data, weights);\n} catch (RuntimeException e) {\n  if (e.getMessage().contains(\"binary\")) throw new IllegalArgumentException(\"Use a multiclass classifier for this dataset\", e);\n  throw e;\n}","preventionTips":["Validate label cardinality in your data-loading pipeline","Document that 'logistic' in this library is strictly binary","Set up a one-vs-rest wrapper utility once and reuse it"],"tags":["java","runtimeexception","binary-classification","factory"],"backgroundTag":"invalid-argument-value","analyzedSha":"1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a","analyzedAt":"2026-09-10T02:24:07.274Z","contentChangedAt":"2026-09-10T02:24:07.274Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}