Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
10/29/08 11:21:04 (15 years ago)
Author:
gkronber
Message:

fixed #328 by restructuring evaluation operators to remove state in evaluation operators.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.GP.StructureIdentification.Classification/ConfusionMatrixEvaluator.cs

    r668 r702  
    2929
    3030namespace HeuristicLab.GP.StructureIdentification.Classification {
    31   public class ConfusionMatrixEvaluator : GPEvaluatorBase {
    32     private const double EPSILON = 1.0E-6;
    33     private double[] classesArr;
    34     private double[] thresholds;
    35     private IntMatrixData matrix;
     31  public class ConfusionMatrixEvaluator : GPClassificationEvaluatorBase {
    3632    public override string Description {
    3733      get {
     
    4339      : base() {
    4440      AddVariableInfo(new VariableInfo("ConfusionMatrix", "The confusion matrix of the model", typeof(IntMatrixData), 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));
    4641    }
    4742
    48     public override IOperation Apply(IScope scope) {
    49       ItemList<DoubleData> classes = GetVariableValue<ItemList<DoubleData>>("TargetClassValues", scope, true);
    50       classesArr = new double[classes.Count];
    51       for(int i = 0; i < classesArr.Length; i++) classesArr[i] = classes[i].Data;
    52       Array.Sort(classesArr);
    53       thresholds = new double[classes.Count - 1];
    54       for(int i = 0; i < classesArr.Length - 1; i++) {
    55         thresholds[i] = (classesArr[i] + classesArr[i + 1]) / 2.0;
     43    public override void Evaluate(IScope scope, BakedTreeEvaluator evaluator, HeuristicLab.DataAnalysis.Dataset dataset, int targetVariable, double[] classes, double[] thresholds, int start, int end) {
     44      IntMatrixData matrix = GetVariableValue<IntMatrixData>("ConfusionMatrix", scope, false, false);
     45      if(matrix == null) {
     46        matrix = new IntMatrixData(new int[classes.Length, classes.Length]);
     47        scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("ConfusionMatrix"), matrix));
    5648      }
    5749
    58       matrix = GetVariableValue<IntMatrixData>("ConfusionMatrix", scope, false, false);
    59       if(matrix == null) {
    60         matrix = new IntMatrixData(new int[classesArr.Length, classesArr.Length]);
    61         scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("ConfusionMatrix"), matrix));
    62       }
    63       return base.Apply(scope);
    64     }
    65 
    66     public override void Evaluate(int start, int end) {
    6750      int nSamples = end - start;
    6851      for(int sample = start; sample < end; sample++) {
    69         double est = GetEstimatedValue(sample);
    70         double origClass = GetOriginalValue(sample);
     52        double est = evaluator.Evaluate(sample);
     53        double origClass = dataset.GetValue(targetVariable,sample);
    7154        int estClassIndex = -1;
    7255        // if estimation is lower than the smallest threshold value -> estimated class is the lower class
    7356        if(est < thresholds[0]) estClassIndex = 0;
    7457        // if estimation is larger (or equal) than the largest threshold value -> estimated class is the upper class
    75         else if(est >= thresholds[thresholds.Length - 1]) estClassIndex = classesArr.Length - 1;
     58        else if(est >= thresholds[thresholds.Length - 1]) estClassIndex = classes.Length - 1;
    7659        else {
    7760          // otherwise the estimated class is the class which upper threshold is larger than the estimated value
     
    8366          }
    8467        }
    85         SetOriginalValue(sample, classesArr[estClassIndex]);
    8668
    87         int origClassIndex = -1;
    88         for(int i = 0; i < classesArr.Length; i++) {
    89           if(IsEqual(origClass, classesArr[i])) origClassIndex = i;
     69        // find the first threshold index that is larger to the original value
     70        int origClassIndex = classes.Length-1;
     71        for(int i = 0; i < thresholds.Length; i++) {
     72          if(origClass < thresholds[i]) {
     73            origClassIndex = i;
     74            break;
     75          }
    9076        }
    9177        matrix.Data[origClassIndex, estClassIndex]++;
    9278      }
    9379    }
    94 
    95     private bool IsEqual(double x, double y) {
    96       return Math.Abs(x - y) < EPSILON;
    97     }
    9880  }
    9981}
Note: See TracChangeset for help on using the changeset viewer.