{"record":{"id":"5c6e1a70a1263a98","repo":"stanfordnlp/CoreNLP","slug":"logisticclassifier-is-only-for-binary-classificati","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/LogisticClassifier.java","lineNumber":293,"sourceCode":"  }\n\n  private double probabilityOfRVFDatum(RVFDatum<L, F> example) {\n    return probabilityOf(example.asFeaturesCounter(), example.label());\n  }\n\n  public double probabilityOf(Counter<F> features, L label) {\n    short sign = (short)(label.equals(classes[0]) ? 1 : -1);\n    return 1.0 / (1.0 + Math.exp(sign * scoreOf(features)));\n  }\n\n  /**\n   * Trains on weighted dataset.\n   * @param dataWeights weights of the data.\n   */\n  @Deprecated //Use LogisticClassifierFactory to train instead.\n  public void trainWeightedData(GeneralDataset<L,F> data, float[] dataWeights){\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(), prior,dataWeights);\n      else if(data instanceof RVFDataset<?,?>)\n        lof = new LogisticObjectiveFunction(data.numFeatureTypes(), data.getDataArray(), data.getValuesArray(), data.getLabelsArray(), prior,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  }\n\n  @Deprecated //Use LogisticClassifierFactory to train instead.\n  public void train(GeneralDataset<L, F> data) {","sourceCodeStart":275,"sourceCodeEnd":311,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/classify/LogisticClassifier.java#L275-L311","documentation":"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.","triggerScenarios":"Calling the deprecated trainWeightedData(GeneralDataset, float[]) with a dataset whose labelIndex.size() != 2 — e.g., a 3-class Dataset or an empty-label dataset.","commonSituations":"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.","solutions":["Reduce/verify the dataset has exactly two labels before training","For multiclass use LogisticClassifierFactory's one-vs-all wrapping or a LinearClassifier/OGBoost instead","Check data.labelIndex.size() as a pre-flight validation and handle it in your pipeline"],"exampleFix":"// before\nclassifier.trainWeightedData(multiclassData, weights);\n// after\nLogisticClassifier<L,F> c = new LogisticClassifierFactory<L,F>().trainClassifier(oneVsRestData, 0.0, 1e-4, true);","handlingStrategy":"validation","validationCode":"if (data.labelIndex.size() != 2)\n  throw new IllegalArgumentException(\"LogisticClassifier needs exactly 2 labels, found \" + data.labelIndex.size());","typeGuard":"boolean isBinary(GeneralDataset<?,?> d) {\n  return d != null && d.labelIndex != null && d.labelIndex.size() == 2;\n}","tryCatchPattern":"try {\n  classifier.trainWeightedData(data, weights);\n} catch (RuntimeException e) {\n  if (e.getMessage().contains(\"binary\")) {\n    LinearClassifier<L,F> lc = new LinearClassifierFactory<L,F>().trainClassifier(data);\n  } else throw e;\n}","preventionTips":["Count labels with data.labelIndex.size() before choosing a classifier","Use LinearClassifier for multiclass problems by default","Prefer the non-deprecated LogisticClassifierFactory entry points"],"tags":["java","runtimeexception","binary-classification","dataset"],"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"}