Changeset 702 for trunk/sources/HeuristicLab.GP.StructureIdentification.Classification/ConfusionMatrixEvaluator.cs
- Timestamp:
- 10/29/08 11:21:04 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.GP.StructureIdentification.Classification/ConfusionMatrixEvaluator.cs
r668 r702 29 29 30 30 namespace 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 { 36 32 public override string Description { 37 33 get { … … 43 39 : base() { 44 40 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));46 41 } 47 42 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)); 56 48 } 57 49 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) {67 50 int nSamples = end - start; 68 51 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); 71 54 int estClassIndex = -1; 72 55 // if estimation is lower than the smallest threshold value -> estimated class is the lower class 73 56 if(est < thresholds[0]) estClassIndex = 0; 74 57 // 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 = classes Arr.Length - 1;58 else if(est >= thresholds[thresholds.Length - 1]) estClassIndex = classes.Length - 1; 76 59 else { 77 60 // otherwise the estimated class is the class which upper threshold is larger than the estimated value … … 83 66 } 84 67 } 85 SetOriginalValue(sample, classesArr[estClassIndex]);86 68 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 } 90 76 } 91 77 matrix.Data[origClassIndex, estClassIndex]++; 92 78 } 93 79 } 94 95 private bool IsEqual(double x, double y) {96 return Math.Abs(x - y) < EPSILON;97 }98 80 } 99 81 }
Note: See TracChangeset
for help on using the changeset viewer.