Changeset 702 for trunk/sources/HeuristicLab.GP.StructureIdentification.Classification/AccuracyEvaluator.cs
- Timestamp:
- 10/29/08 11:21:04 (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.GP.StructureIdentification.Classification/AccuracyEvaluator.cs
r668 r702 27 27 using HeuristicLab.Data; 28 28 using HeuristicLab.GP.StructureIdentification; 29 using HeuristicLab.DataAnalysis; 29 30 30 31 namespace HeuristicLab.GP.StructureIdentification.Classification { 31 public class AccuracyEvaluator : GP EvaluatorBase {32 public class AccuracyEvaluator : GPClassificationEvaluatorBase { 32 33 private const double EPSILON = 1.0E-6; 33 private double[] classesArr;34 private double[] thresholds;35 private DoubleData accuracy;36 34 public override string Description { 37 35 get { … … 43 41 : base() { 44 42 AddVariableInfo(new VariableInfo("Accuracy", "The total accuracy of the model (ratio of correctly classified instances to total number of instances)", typeof(DoubleData), VariableKind.New)); 45 AddVariableInfo(new VariableInfo("TargetClassValues", "The original class values of target variable (for instance negative=0 and positive=1).", typeof(ItemList<DoubleData>), VariableKind.In));46 43 } 47 44 48 public override IOperation Apply(IScope scope) {49 accuracy = GetVariableValue<DoubleData>("Accuracy", scope, false, false);45 public override void Evaluate(IScope scope, BakedTreeEvaluator evaluator, Dataset dataset, int targetVariable, double[] classes, double[] thresholds, int start, int end) { 46 DoubleData accuracy = GetVariableValue<DoubleData>("Accuracy", scope, false, false); 50 47 if(accuracy == null) { 51 48 accuracy = new DoubleData(); … … 53 50 } 54 51 55 ItemList<DoubleData> classes = GetVariableValue<ItemList<DoubleData>>("TargetClassValues", scope, true);56 classesArr = new double[classes.Count];57 for(int i = 0; i < classesArr.Length; i++) classesArr[i] = classes[i].Data;58 Array.Sort(classesArr);59 thresholds = new double[classes.Count - 1];60 for(int i = 0; i < classesArr.Length - 1; i++) {61 thresholds[i] = (classesArr[i] + classesArr[i + 1]) / 2.0;62 }63 64 return base.Apply(scope);65 }66 67 public override void Evaluate(int start, int end) {68 52 int nSamples = end - start; 69 53 int nCorrect = 0; 70 54 for(int sample = start; sample < end; sample++) { 71 double est = GetEstimatedValue(sample);72 double origClass = GetOriginalValue(sample);55 double est = evaluator.Evaluate(sample); 56 double origClass = dataset.GetValue(targetVariable, sample); 73 57 double estClass = double.NaN; 74 58 // if estimation is lower than the smallest threshold value -> estimated class is the lower class 75 if(est < thresholds[0]) estClass = classes Arr[0];59 if(est < thresholds[0]) estClass = classes[0]; 76 60 // if estimation is larger (or equal) than the largest threshold value -> estimated class is the upper class 77 else if(est >= thresholds[thresholds.Length - 1]) estClass = classes Arr[classesArr.Length - 1];61 else if(est >= thresholds[thresholds.Length - 1]) estClass = classes[classes.Length - 1]; 78 62 else { 79 63 // otherwise the estimated class is the class which upper threshold is larger than the estimated value 80 64 for(int k = 0; k < thresholds.Length; k++) { 81 65 if(thresholds[k] > est) { 82 estClass = classes Arr[k];66 estClass = classes[k]; 83 67 break; 84 68 } 85 69 } 86 70 } 87 SetOriginalValue(sample, estClass);88 71 if(Math.Abs(estClass - origClass) < EPSILON) nCorrect++; 89 72 }
Note: See TracChangeset
for help on using the changeset viewer.