Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
08/09/08 00:31:27 (16 years ago)
Author:
gkronber
Message:

improved AccuracyEvaluator (#241)
calculates the ratio of correctly classified instances to total number of instances with static class threshold (exactly in the middle between two classes)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.StructureIdentification/Evaluation/AccuracyEvaluator.cs

    r453 r474  
    3232namespace HeuristicLab.StructureIdentification {
    3333  public class AccuracyEvaluator : GPEvaluatorBase {
     34    private const double EPSILON = 1.0E-6;
    3435    public override string Description {
    3536      get {
     
    4041    public AccuracyEvaluator()
    4142      : base() {
    42       AddVariableInfo(new VariableInfo("ClassSeparation", "The value of separation between negative and positive target classification values (for instance 0.5 if negative=0 and positive=1).", typeof(DoubleData), VariableKind.In));
     43      AddVariableInfo(new VariableInfo("TargetClassValues", "The original class values of target variable (for instance negative=0 and positive=1).", typeof(ItemList<DoubleData>), VariableKind.In));
    4344    }
    4445
     
    4950      int trainingEnd = GetVariableValue<IntData>("TrainingSamplesEnd", scope, true).Data;
    5051      int nSamples = trainingEnd-trainingStart;
    51       double limit = GetVariableValue<DoubleData>("ClassSeparation", scope, true).Data;
    52       double TP = 0;
    53       double TN = 0;
    54       double targetMean = dataset.GetMean(targetVariable, trainingStart, trainingEnd);
     52      ItemList<DoubleData> classes = GetVariableValue<ItemList<DoubleData>>("TargetClassValues", scope, true);
     53      double[] classesArr = new double[classes.Count];
     54      for(int i=0;i<classesArr.Length;i++) classesArr[i] = classes[i].Data;
     55      Array.Sort(classesArr);
     56      double[] thresholds = new double[classes.Count - 1];
     57      for(int i=0;i<classesArr.Length-1;i++) {
     58        thresholds[i] = (classesArr[i]+classesArr[i+1]) / 2.0;
     59      }
     60
     61      int nCorrect = 0;
    5562      for(int sample = trainingStart; sample < trainingEnd; sample++) {
    5663        double est = evaluator.Evaluate(sample);
    57         double orig = dataset.GetValue(sample, targetVariable);
    58         if(double.IsNaN(est) || double.IsInfinity(est)) {
    59           est = targetMean + maximumPunishment;
    60         } else if(est > targetMean + maximumPunishment) {
    61           est = targetMean + maximumPunishment;
    62         } else if(est < targetMean - maximumPunishment) {
    63           est = targetMean - maximumPunishment;
     64        double origClass = dataset.GetValue(sample, targetVariable);
     65        double estClass = double.NaN;
     66        if(est < classesArr[0]) estClass = classesArr[0];
     67        else if(est > classesArr[classesArr.Length - 1]) estClass = classesArr[classesArr.Length - 1];
     68        else {
     69          for(int k = 0; k < thresholds.Length; k++) {
     70            if(thresholds[k] > est) {
     71              estClass = classesArr[k + 1];
     72              break;
     73            }
     74          }
    6475        }
    65         if(orig >= limit && est>=limit) TP++;
    66         if(orig < limit && est < limit) TN++;
     76        if(Math.Abs(estClass - origClass) < EPSILON) nCorrect++;
    6777      }
    6878      scope.GetVariableValue<DoubleData>("TotalEvaluatedNodes", true).Data = totalEvaluatedNodes + treeSize * nSamples;
    69       return (TP+TN) / nSamples;
     79      return  nCorrect / (double)nSamples;
    7080    }
    7181  }
Note: See TracChangeset for help on using the changeset viewer.