{"record":{"id":"0c9ca167b08b1bdd","repo":"stanfordnlp/CoreNLP","slug":"can-t-standardize-array-whose-mean-is-nan","errorCode":null,"errorMessage":"Can't standardize array whose mean is NaN","messagePattern":"Can't standardize array whose mean is NaN","errorType":"exception","errorClass":"ArithmeticException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/math/ArrayMath.java","lineNumber":1386,"sourceCode":"    if (total == 0.0 || Float.isNaN(total)) {\n      if (a.length < 100) {\n        throw new ArithmeticException(\"Can't normalize an array with sum 0.0 or NaN: \" + Arrays.toString(a));\n      } else {\n        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;","sourceCodeStart":1368,"sourceCodeEnd":1404,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/math/ArrayMath.java#L1368-L1404","documentation":"ArrayMath.standardize(double[]) subtracts the mean and divides by the standard deviation. If the array's mean is NaN, standardization is meaningless, so it throws an ArithmeticException. A NaN mean almost always means the array contains at least one NaN (or +Inf/-Inf) element.","triggerScenarios":"Calling ArrayMath.standardize(a) where mean(a) returns NaN — i.e. the array contains NaN, or Inf and -Inf together, or is empty (mean of empty array yields NaN).","commonSituations":"Feature normalization pipelines where one feature value was parsed from bad data ('NaN' string, missing value), or empty arrays passed accidentally from empty collections.","solutions":["Scan the array before calling standardize and remove or impute NaN/Infinite elements.","Check ArrayMath.isEmpty / a.length > 0 before standardizing; reject empty arrays at the caller.","Fix the upstream data parsing/generation step that introduced NaN into the array.","Catch ArithmeticException if NaN inputs are expected and use a fallback (e.g. return the array unchanged or use precomputed statistics)."],"exampleFix":"// before\nArrayMath.standardize(values);\n// after\nboolean hasBad = false;\nfor (double v : values) { if (Double.isNaN(v) || Double.isInfinite(v)) { hasBad = true; break; } }\nif (values.length > 0 && !hasBad) {\n  ArrayMath.standardize(values);\n}","handlingStrategy":"validation","validationCode":"if (a.length == 0) throw new IllegalArgumentException(\"empty array\");\nfor (double v : a) {\n  if (Double.isNaN(v) || Double.isInfinite(v)) throw new IllegalArgumentException(\"bad value: \" + v);\n}","typeGuard":null,"tryCatchPattern":"try {\n  ArrayMath.standardize(a);\n} catch (ArithmeticException e) {\n  // fall back: leave data unscaled or impute\n}","preventionTips":["Validate arrays for NaN/Inf/emptiness before standardization","Fix parsing code that lets 'NaN' or missing values into numeric arrays","Impute or drop bad elements during preprocessing","Compute mean/stdev yourself first to log which condition fails"],"tags":["math","nan","standardization","data-quality"],"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"}