- Timestamp:
- 09/14/09 17:30:02 (15 years ago)
- File:
-
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Modeling/3.2/SimpleAccuracyEvaluator.cs
r2348 r2351 24 24 using HeuristicLab.Data; 25 25 using HeuristicLab.DataAnalysis; 26 using System.Linq; 27 using HeuristicLab.Common; 26 28 27 namespace HeuristicLab.GP.StructureIdentification.Classification { 28 public class AccuracyEvaluator : GPClassificationEvaluatorBase { 29 private const double EPSILON = 1.0E-6; 29 namespace HeuristicLab.Modeling { 30 public class SimpleAccuracyEvaluator : SimpleEvaluatorBase { 31 public override string OutputVariableName { 32 get { 33 return "Accuracy"; 34 } 35 } 30 36 public override string Description { 31 37 get { … … 34 40 } 35 41 36 public AccuracyEvaluator() 37 : base() { 38 AddVariableInfo(new VariableInfo("Accuracy", "The total accuracy of the model (ratio of correctly classified instances to total number of instances)", typeof(DoubleData), VariableKind.New)); 42 public override double Evaluate(double[,] values) { 43 return Calculate(values); 39 44 } 40 45 41 public override void Evaluate(IScope scope, ITreeEvaluator evaluator, Dataset dataset, int targetVariable, double[] classes, double[] thresholds, int start, int end) { 42 DoubleData accuracy = GetVariableValue<DoubleData>("Accuracy", scope, false, false); 43 if (accuracy == null) { 44 accuracy = new DoubleData(); 45 scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("Accuracy"), accuracy)); 46 } 46 public static double Calculate(double[,] values) { 47 int nSamples = values.GetLength(0); 48 int nCorrect = 0; 49 double[] classes = CalculateTargetClasses(values); 50 double[] thresholds = CalculateThresholds(classes); 47 51 48 int nSamples = end - start; 49 int nCorrect = 0; 50 for (int sample = start; sample < end; sample++) { 51 double est = evaluator.Evaluate(sample); 52 double origClass = dataset.GetValue(sample, targetVariable); 52 for (int sample = 0; sample < nSamples; sample++) { 53 double est = values[sample, 0]; 54 double origClass = values[sample, 1]; 53 55 double estClass = double.NaN; 54 56 // if estimation is lower than the smallest threshold value -> estimated class is the lower class … … 65 67 } 66 68 } 67 if ( Math.Abs(estClass - origClass) < EPSILON) nCorrect++;69 if (estClass.IsAlmost(origClass)) nCorrect++; 68 70 } 69 accuracy.Data = nCorrect / (double)nSamples; 71 return nCorrect / (double)nSamples; 72 } 73 74 public static double[] CalculateTargetClasses(double[,] values) { 75 int n = values.GetLength(0); 76 double[] original = new double[n]; 77 for (int i = 0; i < n; i++) original[i] = values[i, 1]; 78 return original.OrderBy(x => x).Distinct().ToArray(); 79 } 80 81 public static double[] CalculateThresholds(double[] targetClasses) { 82 double[] thresholds = new double[targetClasses.Length - 1]; 83 for (int i = 1; i < targetClasses.Length; i++) { 84 thresholds[i - 1] = (targetClasses[i - 1] + targetClasses[i]) / 2.0; 85 } 86 return thresholds; 70 87 } 71 88 }
Note: See TracChangeset
for help on using the changeset viewer.