{"record":{"id":"ee26e5d0eb44cabe","repo":"stanfordnlp/CoreNLP","slug":"can-t-standardize-array-whose-standard-deviation-i","errorCode":null,"errorMessage":"Can't standardize array whose standard deviation is 0.0 or NaN","messagePattern":"Can't standardize array whose standard deviation is 0\\.0 or NaN","errorType":"exception","errorClass":"ArithmeticException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/math/ArrayMath.java","lineNumber":1390,"sourceCode":"        throw new ArithmeticException(\"Can't normalize an array with sum 0.0 or NaN: \" + Arrays.toString(Arrays.copyOf(a, 100)) + \" ... \");\n      }\n\n    }\n    multiplyInPlace(a, 1.0/total); // divide each value by total\n  }\n\n  /**\n   * Standardize values in this array, i.e., subtract the mean and divide by the standard deviation.\n   * If standard deviation is 0.0, throws a RuntimeException.\n   */\n  public static void standardize(double[] a) {\n    double m = mean(a);\n    if (Double.isNaN(m)) {\n      throw new ArithmeticException(\"Can't standardize array whose mean is NaN\");\n    }\n    double s = stdev(a);\n    if (s == 0.0 || Double.isNaN(s)) {\n      throw new ArithmeticException(\"Can't standardize array whose standard deviation is 0.0 or NaN\");\n    }\n    addInPlace(a, -m); // subtract mean\n    multiplyInPlace(a, 1.0/s); // divide by standard deviation\n  }\n\n  public static double L2Norm(double[] a) {\n    double result = 0.0;\n    for(double d: a) {\n      result += d * d;\n    }\n    return Math.sqrt(result);\n  }\n  public static float L2Norm(float[] a) {\n    double result = 0;\n    for(float d: a) {\n      result += d * d;\n    }\n    return (float) Math.sqrt(result);","sourceCodeStart":1372,"sourceCodeEnd":1408,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/math/ArrayMath.java#L1372-L1408","documentation":"ArrayMath.standardize(double[]) also throws when the array's standard deviation is 0.0 (all elements identical) or NaN, because dividing by such a value is undefined. This check runs after the NaN-mean check, so the array has a valid mean but no spread.","triggerScenarios":"Calling ArrayMath.standardize(a) where every element of a is the same constant (stdev == 0.0), or stdev computes NaN (e.g. variance overflow or NaN variance from Inf values).","commonSituations":"Normalizing a feature column that is constant across the dataset (a degenerate feature), or features containing only one repeated default value after sparse data preprocessing.","solutions":["Compute ArrayMath.stdev(a) first and skip standardization when it is 0.0 or NaN, leaving the (mean-centered or original) values as-is.","Add an epsilon to the standard deviation before dividing if a standardized scale is required regardless.","Drop constant/degenerate feature columns in preprocessing before the normalization stage.","Catch ArithmeticException and fall back to mean-centering only (addInPlace(a, -ArrayMath.mean(a)))."],"exampleFix":"// before\nArrayMath.standardize(feature);\n// after\ndouble s = ArrayMath.stdev(feature);\nif (s != 0.0 && !Double.isNaN(s)) {\n  ArrayMath.standardize(feature);\n} else {\n  ArrayMath.addInPlace(feature, -ArrayMath.mean(feature)); // center only\n}","handlingStrategy":"validation","validationCode":"double s = ArrayMath.stdev(a);\nif (Double.isNaN(s) || s == 0.0) {\n  // skip scaling or center-only\n} else {\n  ArrayMath.standardize(a);\n}","typeGuard":null,"tryCatchPattern":"try {\n  ArrayMath.standardize(a);\n} catch (ArithmeticException e) {\n  ArrayMath.addInPlace(a, -ArrayMath.mean(a)); // center-only fallback\n}","preventionTips":["Detect constant feature columns during preprocessing and drop them","Check stdev before standardizing","Use epsilon in denominators when scaling must always happen","Remember: zero variance means the feature carries no information"],"tags":["math","zero-variance","standardization","degenerate-data"],"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-17T15:17:12.973Z"}