Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
07/05/10 17:14:22 (14 years ago)
Author:
gkronber
Message:

Improved efficiency of analyzers and evaluators for regression problems. #1074

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Regression/3.3/Analyzers/RegressionSolutionAnalyzer.cs

    r3923 r3996  
    119119      var bestSolution = UpdateBestSolution();
    120120      if (prevBestSolutionQuality == null || prevBestSolutionQuality.Value > BestSolutionQualityParameter.ActualValue.Value) {
    121         UpdateBestSolutionResults(bestSolution);
     121        RegressionSolutionAnalyzer.UpdateBestSolutionResults(bestSolution, ProblemData, Results, GenerationsParameter.ActualValue);
    122122      }
    123123
    124124      return base.Apply();
    125125    }
    126     private void UpdateBestSolutionResults(DataAnalysisSolution bestSolution) {
     126
     127    public static void UpdateBestSolutionResults(DataAnalysisSolution bestSolution, DataAnalysisProblemData problemData, ResultCollection results, IntValue CurrentGeneration) {
    127128      var solution = bestSolution;
    128129      #region update R2,MSE, Rel Error
    129       double[] trainingValues = ProblemData.Dataset.GetVariableValues(
    130         ProblemData.TargetVariable.Value,
    131         ProblemData.TrainingSamplesStart.Value,
    132         ProblemData.TrainingSamplesEnd.Value);
    133       double[] testValues = ProblemData.Dataset.GetVariableValues(
    134         ProblemData.TargetVariable.Value,
    135         ProblemData.TestSamplesStart.Value,
    136         ProblemData.TestSamplesEnd.Value);
    137       double trainingR2 = SimpleRSquaredEvaluator.Calculate(trainingValues, solution.EstimatedTrainingValues);
    138       double testR2 = SimpleRSquaredEvaluator.Calculate(testValues, solution.EstimatedTestValues);
    139       double trainingMse = SimpleMSEEvaluator.Calculate(trainingValues, solution.EstimatedTrainingValues);
    140       double testMse = SimpleMSEEvaluator.Calculate(testValues, solution.EstimatedTestValues);
    141       double trainingRelError = SimpleMeanAbsolutePercentageErrorEvaluator.Calculate(trainingValues, solution.EstimatedTrainingValues);
    142       double testRelError = SimpleMeanAbsolutePercentageErrorEvaluator.Calculate(testValues, solution.EstimatedTestValues);
    143       if (Results.ContainsKey(BestSolutionResultName)) {
    144         Results[BestSolutionResultName].Value = solution;
    145         Results[BestSolutionTrainingRSquared].Value = new DoubleValue(trainingR2);
    146         Results[BestSolutionTestRSquared].Value = new DoubleValue(testR2);
    147         Results[BestSolutionTrainingMse].Value = new DoubleValue(trainingMse);
    148         Results[BestSolutionTestMse].Value = new DoubleValue(testMse);
    149         Results[BestSolutionTrainingRelativeError].Value = new DoubleValue(trainingRelError);
    150         Results[BestSolutionTestRelativeError].Value = new DoubleValue(testRelError);
    151         if (GenerationsParameter.ActualValue != null) // this check is needed because linear regression solutions do not have a generations parameter
    152           Results[BestSolutionGeneration].Value = new IntValue(GenerationsParameter.ActualValue.Value);
     130      IEnumerable<double> trainingValues = problemData.Dataset.GetEnumeratedVariableValues(
     131        problemData.TargetVariable.Value,
     132        problemData.TrainingSamplesStart.Value,
     133        problemData.TrainingSamplesEnd.Value);
     134      IEnumerable<double> testValues = problemData.Dataset.GetEnumeratedVariableValues(
     135        problemData.TargetVariable.Value,
     136        problemData.TestSamplesStart.Value,
     137        problemData.TestSamplesEnd.Value);
     138      OnlineMeanSquaredErrorEvaluator mseEvaluator = new OnlineMeanSquaredErrorEvaluator();
     139      OnlineMeanAbsolutePercentageErrorEvaluator relErrorEvaluator = new OnlineMeanAbsolutePercentageErrorEvaluator();
     140      OnlinePearsonsRSquaredEvaluator r2Evaluator = new OnlinePearsonsRSquaredEvaluator();
     141      #region training
     142      var originalEnumerator = trainingValues.GetEnumerator();
     143      var estimatedEnumerator = solution.EstimatedTrainingValues.GetEnumerator();
     144      while (originalEnumerator.MoveNext() & estimatedEnumerator.MoveNext()) {
     145        mseEvaluator.Add(originalEnumerator.Current, estimatedEnumerator.Current);
     146        r2Evaluator.Add(originalEnumerator.Current, estimatedEnumerator.Current);
     147        relErrorEvaluator.Add(originalEnumerator.Current, estimatedEnumerator.Current);
     148      }
     149      double trainingR2 = r2Evaluator.RSquared;
     150      double trainingMse = mseEvaluator.MeanSquaredError;
     151      double trainingRelError = relErrorEvaluator.MeanAbsolutePercentageError;
     152      #endregion
     153      mseEvaluator.Reset();
     154      relErrorEvaluator.Reset();
     155      r2Evaluator.Reset();
     156      #region test
     157      originalEnumerator = testValues.GetEnumerator();
     158      estimatedEnumerator = solution.EstimatedTestValues.GetEnumerator();
     159      while (originalEnumerator.MoveNext() & estimatedEnumerator.MoveNext()) {
     160        mseEvaluator.Add(originalEnumerator.Current, estimatedEnumerator.Current);
     161        r2Evaluator.Add(originalEnumerator.Current, estimatedEnumerator.Current);
     162        relErrorEvaluator.Add(originalEnumerator.Current, estimatedEnumerator.Current);
     163      }
     164      double testR2 = r2Evaluator.RSquared;
     165      double testMse = mseEvaluator.MeanSquaredError;
     166      double testRelError = relErrorEvaluator.MeanAbsolutePercentageError;
     167      #endregion
     168      if (results.ContainsKey(BestSolutionResultName)) {
     169        results[BestSolutionResultName].Value = solution;
     170        results[BestSolutionTrainingRSquared].Value = new DoubleValue(trainingR2);
     171        results[BestSolutionTestRSquared].Value = new DoubleValue(testR2);
     172        results[BestSolutionTrainingMse].Value = new DoubleValue(trainingMse);
     173        results[BestSolutionTestMse].Value = new DoubleValue(testMse);
     174        results[BestSolutionTrainingRelativeError].Value = new DoubleValue(trainingRelError);
     175        results[BestSolutionTestRelativeError].Value = new DoubleValue(testRelError);
     176        if (CurrentGeneration != null) // this check is needed because linear regression solutions do not have a generations parameter
     177          results[BestSolutionGeneration].Value = new IntValue(CurrentGeneration.Value);
    153178      } else {
    154         Results.Add(new Result(BestSolutionResultName, solution));
    155         Results.Add(new Result(BestSolutionTrainingRSquared, new DoubleValue(trainingR2)));
    156         Results.Add(new Result(BestSolutionTestRSquared, new DoubleValue(testR2)));
    157         Results.Add(new Result(BestSolutionTrainingMse, new DoubleValue(trainingMse)));
    158         Results.Add(new Result(BestSolutionTestMse, new DoubleValue(testMse)));
    159         Results.Add(new Result(BestSolutionTrainingRelativeError, new DoubleValue(trainingRelError)));
    160         Results.Add(new Result(BestSolutionTestRelativeError, new DoubleValue(testRelError)));
    161         if (GenerationsParameter.ActualValue != null)
    162           Results.Add(new Result(BestSolutionGeneration, new IntValue(GenerationsParameter.ActualValue.Value)));
     179        results.Add(new Result(BestSolutionResultName, solution));
     180        results.Add(new Result(BestSolutionTrainingRSquared, new DoubleValue(trainingR2)));
     181        results.Add(new Result(BestSolutionTestRSquared, new DoubleValue(testR2)));
     182        results.Add(new Result(BestSolutionTrainingMse, new DoubleValue(trainingMse)));
     183        results.Add(new Result(BestSolutionTestMse, new DoubleValue(testMse)));
     184        results.Add(new Result(BestSolutionTrainingRelativeError, new DoubleValue(trainingRelError)));
     185        results.Add(new Result(BestSolutionTestRelativeError, new DoubleValue(testRelError)));
     186        if (CurrentGeneration != null)
     187          results.Add(new Result(BestSolutionGeneration, new IntValue(CurrentGeneration.Value)));
    163188      }
    164189      #endregion
Note: See TracChangeset for help on using the changeset viewer.