public static double calculateRegressionError(MLRegression method, MLDataSet data) {

    final ErrorCalculation errorCalculation = new ErrorCalculation();
    if (method instanceof MLContext) ((MLContext) method).clearContext();

    for (final MLDataPair pair : data) {
      final MLData actual = method.compute(pair.getInput());
      errorCalculation.updateError(
          actual.getData(), pair.getIdeal().getData(), pair.getSignificance());
    }
    return errorCalculation.calculate();
  }
  /**
   * Calculate the error for this neural network. The error is calculated using
   * root-mean-square(RMS).
   *
   * @param data The training set.
   * @return The error percentage.
   */
  public double calculateError(final MLDataSet data) {
    final ErrorCalculation errorCalculation = new ErrorCalculation();

    final double[] actual = new double[this.outputCount];
    final MLDataPair pair = BasicMLDataPair.createPair(data.getInputSize(), data.getIdealSize());

    for (int i = 0; i < data.getRecordCount(); i++) {
      data.getRecord(i, pair);
      compute(pair.getInputArray(), actual);
      errorCalculation.updateError(actual, pair.getIdealArray(), pair.getSignificance());
    }
    return errorCalculation.calculate();
  }