Changeset 1894 for trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/Evaluators/CoefficientOfDeterminationEvaluator.cs
- Timestamp:
- 05/25/09 18:43:26 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/Evaluators/CoefficientOfDeterminationEvaluator.cs
r1891 r1894 27 27 using HeuristicLab.Data; 28 28 using HeuristicLab.Operators; 29 using HeuristicLab.Modeling; 29 30 30 31 namespace HeuristicLab.GP.StructureIdentification { 31 public class CoefficientOfDeterminationEvaluator : GPEvaluatorBase { 32 public class CoefficientOfDeterminationEvaluator : SimpleGPEvaluatorBase { 33 34 public override string OutputVariableName { 35 get { 36 return "R2"; 37 } 38 } 39 32 40 public override string Description { 33 41 get { … … 37 45 } 38 46 39 public CoefficientOfDeterminationEvaluator() 40 : base() { 41 AddVariableInfo(new VariableInfo("R2", "The coefficient of determination of the model", typeof(DoubleData), VariableKind.New)); 42 } 47 public override double Evaluate(double[,] values) { 43 48 44 public override void Evaluate(IScope scope, ITreeEvaluator evaluator, HeuristicLab.DataAnalysis.Dataset dataset, int targetVariable, int start, int end, bool updateTargetValues) { 45 double errorsSquaredSum = 0.0; 46 double originalDeviationTotalSumOfSquares = 0.0; 47 double targetMean = dataset.GetMean(targetVariable, start, end); 48 49 double originalSum = 0.0; 50 int n = 0; 51 for (int sample = start; sample < end; sample++) { 52 double estimated = evaluator.Evaluate(sample); 53 double original = dataset.GetValue(sample, targetVariable); 54 if (updateTargetValues) { 55 dataset.SetValue(sample, targetVariable, estimated); 56 } 57 if (!double.IsNaN(original) && !double.IsInfinity(original)) { 58 double error = estimated - original; 59 errorsSquaredSum += error * error; 60 61 originalSum += original; 62 n++; 63 } 64 } 65 66 double originalMean = originalSum / n; 67 for(int sample = start; sample < end; sample++){ 68 double original = dataset.GetValue(sample, targetVariable); 69 if (!double.IsNaN(original) && !double.IsInfinity(original)) { 70 original = original - originalMean; 71 original = original * original; 72 originalDeviationTotalSumOfSquares += original; 73 } 74 } 75 76 double quality = 1 - errorsSquaredSum / originalDeviationTotalSumOfSquares; 77 if (quality > 1) 78 throw new InvalidProgramException(); 49 double quality = SimpleR2Evaluator.Calculate(values); 79 50 if (double.IsNaN(quality) || double.IsInfinity(quality)) 80 51 quality = double.MaxValue; 81 52 82 DoubleData r2 = GetVariableValue<DoubleData>("R2", scope, false, false); 83 if (r2 == null) { 84 r2 = new DoubleData(); 85 scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("R2"), r2)); 86 } 87 88 r2.Data = quality; 53 return quality; 89 54 } 90 55 }
Note: See TracChangeset
for help on using the changeset viewer.