Changeset 482
- Timestamp:
- 08/10/08 12:44:27 (16 years ago)
- Location:
- trunk/sources/HeuristicLab.StructureIdentification/Evaluation
- Files:
-
- 11 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.StructureIdentification/Evaluation/AccuracyEvaluator.cs
r480 r482 35 35 private double[] classesArr; 36 36 private double[] thresholds; 37 private DoubleData accuracy; 37 38 public override string Description { 38 39 get { 39 return @" TASK";40 return @"Calculates the total accuracy of the model (ratio of correctly classified instances to total number of instances) given a model and the list of possible target class values."; 40 41 } 41 42 } … … 43 44 public AccuracyEvaluator() 44 45 : base() { 46 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 47 AddVariableInfo(new VariableInfo("TargetClassValues", "The original class values of target variable (for instance negative=0 and positive=1).", typeof(ItemList<DoubleData>), VariableKind.In)); 46 48 } 47 49 48 50 public override IOperation Apply(IScope scope) { 51 accuracy = GetVariableValue<DoubleData>("Accuracy", scope, false, false); 52 if(accuracy == null) { 53 accuracy = new DoubleData(); 54 scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("Accuracy"), accuracy)); 55 } 56 49 57 ItemList<DoubleData> classes = GetVariableValue<ItemList<DoubleData>>("TargetClassValues", scope, true); 50 58 classesArr = new double[classes.Count]; … … 59 67 } 60 68 61 public override doubleEvaluate(int start, int end) {62 int nSamples = end -start;69 public override void Evaluate(int start, int end) { 70 int nSamples = end - start; 63 71 int nCorrect = 0; 64 72 for(int sample = start; sample < end; sample++) { … … 70 78 if(est < thresholds[0]) estClass = classesArr[0]; 71 79 // if estimation is larger (or equal) than the largest threshold value -> estimated class is the upper class 72 else if(est >= thresholds[thresholds.Length - 1]) estClass = classesArr[classesArr.Length - 1]; 80 else if(est >= thresholds[thresholds.Length - 1]) estClass = classesArr[classesArr.Length - 1]; 73 81 else { 74 82 // otherwise the estimated class is the class which upper threshold is larger than the estimated value … … 82 90 if(Math.Abs(estClass - origClass) < EPSILON) nCorrect++; 83 91 } 84 returnnCorrect / (double)nSamples;92 accuracy.Data = nCorrect / (double)nSamples; 85 93 } 86 94 } -
trunk/sources/HeuristicLab.StructureIdentification/Evaluation/ClassificationMeanSquaredErrorEvaluator.cs
r480 r482 31 31 32 32 namespace HeuristicLab.StructureIdentification { 33 public class ClassificationMeanSquaredErrorEvaluator : GPEvaluatorBase{33 public class ClassificationMeanSquaredErrorEvaluator : MeanSquaredErrorEvaluator { 34 34 private const double EPSILON = 1.0E-6; 35 35 private double[] classesArr; … … 54 54 } 55 55 56 public override double Evaluate(int start, int end) { 57 56 public override void Evaluate(int start, int end) { 58 57 double errorsSquaredSum = 0; 59 58 for(int sample = start; sample < end; sample++) { … … 79 78 errorsSquaredSum = double.MaxValue; 80 79 } 81 returnerrorsSquaredSum;80 mse.Data = errorsSquaredSum; 82 81 } 83 82 -
trunk/sources/HeuristicLab.StructureIdentification/Evaluation/CoefficientOfDeterminationEvaluator.cs
r480 r482 32 32 namespace HeuristicLab.StructureIdentification { 33 33 public class CoefficientOfDeterminationEvaluator : GPEvaluatorBase { 34 private DoubleData r2; 34 35 public override string Description { 35 36 get { … … 41 42 public CoefficientOfDeterminationEvaluator() 42 43 : base() { 44 AddVariableInfo(new VariableInfo("R2", "The coefficient of determination of the model", typeof(DoubleData), VariableKind.New)); 43 45 } 44 46 45 public override double Evaluate(int start, int end) { 47 public override IOperation Apply(IScope scope) { 48 r2 = GetVariableValue<DoubleData>("R2", scope, false, false); 49 if(r2 == null) { 50 r2 = new DoubleData(); 51 scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("R2"), r2)); 52 } 53 54 return base.Apply(scope); 55 } 56 57 public override void Evaluate(int start, int end) { 46 58 double errorsSquaredSum = 0.0; 47 59 double originalDeviationTotalSumOfSquares = 0.0; … … 60 72 61 73 double quality = 1 - errorsSquaredSum / originalDeviationTotalSumOfSquares; 62 if(quality > 1) 74 if(quality > 1) 63 75 throw new InvalidProgramException(); 64 if(double.IsNaN(quality) || double.IsInfinity(quality)) 76 if(double.IsNaN(quality) || double.IsInfinity(quality)) 65 77 quality = double.MaxValue; 66 r eturnquality;78 r2.Data = quality; 67 79 } 68 80 } -
trunk/sources/HeuristicLab.StructureIdentification/Evaluation/EarlyStoppingMeanSquaredErrorEvaluator.cs
r480 r482 52 52 53 53 // evaluates the function-tree for the given target-variable and the whole dataset and returns the MSE 54 public override doubleEvaluate(int start, int end) {54 public override void Evaluate(int start, int end) { 55 55 double errorsSquaredSum = 0; 56 56 int rows = end - start; … … 65 65 // check the limit and stop as soon as we hit the limit 66 66 if(errorsSquaredSum / rows >= qualityLimit) { 67 return errorsSquaredSum / (sample - start + 1); // return estimated MSE (when the remaining errors are on average the same) 67 mse.Data = errorsSquaredSum / (sample - start + 1); // return estimated MSE (when the remaining errors are on average the same) 68 return; 68 69 } 69 70 } … … 72 73 errorsSquaredSum = double.MaxValue; 73 74 } 74 returnerrorsSquaredSum;75 mse.Data = errorsSquaredSum; 75 76 } 76 77 } -
trunk/sources/HeuristicLab.StructureIdentification/Evaluation/GPEvaluatorBase.cs
r481 r482 34 34 private IEvaluator evaluator; 35 35 private int targetVariable; 36 private int trainingStart;37 private int trainingEnd;36 private int start; 37 private int end; 38 38 private bool useEstimatedValues; 39 39 private double[] backupValues; … … 55 55 AddVariableInfo(new VariableInfo("PunishmentFactor", "Punishment factor for invalid estimations", typeof(DoubleData), VariableKind.In)); 56 56 AddVariableInfo(new VariableInfo("TotalEvaluatedNodes", "Number of evaluated nodes", typeof(DoubleData), VariableKind.In | VariableKind.Out)); 57 AddVariableInfo(new VariableInfo(" TrainingSamplesStart", "Start index of training samples in dataset", typeof(IntData), VariableKind.In));58 AddVariableInfo(new VariableInfo(" TrainingSamplesEnd", "End index of training samples in dataset", typeof(IntData), VariableKind.In));57 AddVariableInfo(new VariableInfo("SamplesStart", "Start index of samples in dataset to evaluate", typeof(IntData), VariableKind.In)); 58 AddVariableInfo(new VariableInfo("SamplesEnd", "End index of samples in dataset to evaluate", typeof(IntData), VariableKind.In)); 59 59 AddVariableInfo(new VariableInfo("UseEstimatedTargetValue", "Wether to use the original (measured) or the estimated (calculated) value for the targat variable when doing autoregressive modelling", typeof(BoolData), VariableKind.In)); 60 AddVariableInfo(new VariableInfo("Quality", "The evaluated quality of the model", typeof(DoubleData), VariableKind.New));61 60 } 62 61 … … 69 68 treeSize = scope.GetVariableValue<IntData>("TreeSize", false).Data; 70 69 totalEvaluatedNodes = scope.GetVariableValue<DoubleData>("TotalEvaluatedNodes", true).Data; 71 int trainingStart = GetVariableValue<IntData>("TrainingSamplesStart", scope, true).Data;72 int trainingEnd = GetVariableValue<IntData>("TrainingSamplesEnd", scope, true).Data;70 int start = GetVariableValue<IntData>("SamplesStart", scope, true).Data; 71 int end = GetVariableValue<IntData>("SamplesEnd", scope, true).Data; 73 72 useEstimatedValues = GetVariableValue<BoolData>("UseEstimatedTargetValue", scope, true).Data; 74 73 // prepare for autoregressive modelling by saving the original values of the target-variable to a backup array 75 74 if(useEstimatedValues && 76 (backupValues == null || trainingStart!=this.trainingStart || trainingEnd!=this.trainingEnd)) {77 this. trainingStart = trainingStart;78 this. trainingEnd = trainingEnd;79 backupValues = new double[ trainingEnd - trainingStart];80 for(int i = trainingStart; i < trainingEnd; i++) {81 backupValues[i - trainingStart] = dataset.GetValue(i, targetVariable);75 (backupValues == null || start!=this.start || end!=this.end)) { 76 this.start = start; 77 this.end = end; 78 backupValues = new double[end - start]; 79 for(int i = start; i < end; i++) { 80 backupValues[i - start] = dataset.GetValue(i, targetVariable); 82 81 } 83 82 } 84 83 // get the mean of the values of the target variable to determin the max and min bounds of the estimated value 85 targetMean = dataset.GetMean(targetVariable, trainingStart, trainingEnd);84 targetMean = dataset.GetMean(targetVariable, start, end); 86 85 estimatedValueMin = targetMean - maximumPunishment; 87 86 estimatedValueMax = targetMean + maximumPunishment; … … 92 91 evaluatedSamples = 0; 93 92 94 // calculate the quality measure 95 double result = Evaluate(trainingStart, trainingEnd); 93 Evaluate(start, end); 96 94 97 95 // restore the values of the target variable from the backup array if necessary 98 if(useEstimatedValues) RestoreDataset(dataset, targetVariable, trainingStart, trainingEnd);96 if(useEstimatedValues) RestoreDataset(dataset, targetVariable, start, end); 99 97 // update the value of total evaluated nodes 100 98 scope.GetVariableValue<DoubleData>("TotalEvaluatedNodes", true).Data = totalEvaluatedNodes + treeSize * evaluatedSamples; 101 99 // write the calculate quality value 102 DoubleData quality = GetVariableValue<DoubleData>("Quality", scope, false, false);103 if(quality == null) {104 scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("Quality"), new DoubleData(result)));105 } else {106 quality.Data = result;107 }108 100 return null; 109 101 } … … 115 107 } 116 108 117 public abstract doubleEvaluate(int start, int end);109 public abstract void Evaluate(int start, int end); 118 110 119 111 public void SetOriginalValue(int sample, double value) { -
trunk/sources/HeuristicLab.StructureIdentification/Evaluation/MCCEvaluator.cs
r480 r482 35 35 private double[] original = new double[1]; 36 36 private double[] estimated = new double[1]; 37 private DoubleData mcc; 37 38 public override string Description { 38 39 get { 39 return @" TASK";40 return @"Calculates the matthews correlation coefficient for a given model and class separation threshold"; 40 41 } 41 42 } … … 43 44 : base() { 44 45 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)); 46 AddVariableInfo(new VariableInfo("MCC", "The matthews correlation coefficient of the model", typeof(DoubleData), VariableKind.New)); 45 47 } 46 48 47 49 public override IOperation Apply(IScope scope) { 50 mcc = GetVariableValue<DoubleData>("MCC", scope, false, false); 51 if(mcc == null) { 52 mcc = new DoubleData(); 53 scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("MCC"), mcc)); 54 } 48 55 limit = GetVariableValue<DoubleData>("ClassSeparation", scope, true).Data; 49 56 return base.Apply(scope); 50 57 } 51 58 52 public override doubleEvaluate(int start, int end) {59 public override void Evaluate(int start, int end) { 53 60 int nSamples = end - start; 54 61 if(estimated.Length != nSamples) { … … 85 92 } 86 93 } 87 returnbest_mcc;94 this.mcc.Data = best_mcc; 88 95 } 89 96 } -
trunk/sources/HeuristicLab.StructureIdentification/Evaluation/MeanAbsolutePercentageErrorEvaluator.cs
r480 r482 32 32 namespace HeuristicLab.StructureIdentification { 33 33 public class MeanAbsolutePercentageErrorEvaluator : GPEvaluatorBase { 34 private DoubleData mape; 34 35 public override string Description { 35 36 get { … … 41 42 public MeanAbsolutePercentageErrorEvaluator() 42 43 : base() { 44 AddVariableInfo(new VariableInfo("MAPE", "The mean absolute percentage error of the model", typeof(DoubleData), VariableKind.New)); 43 45 } 44 46 45 public override double Evaluate(int start, int end) { 47 public override IOperation Apply(IScope scope) { 48 mape = GetVariableValue<DoubleData>("MAPE", scope, false, false); 49 if(mape == null) { 50 mape = new DoubleData(); 51 scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("MAPE"), mape)); 52 } 53 54 return base.Apply(scope); 55 } 56 57 public override void Evaluate(int start, int end) { 46 58 double errorsSum = 0.0; 47 59 for(int sample = start; sample < end; sample++) { … … 57 69 if(double.IsNaN(quality) || double.IsInfinity(quality)) 58 70 quality = double.MaxValue; 59 returnquality;71 mape.Data = quality; 60 72 } 61 73 } -
trunk/sources/HeuristicLab.StructureIdentification/Evaluation/MeanSquaredErrorEvaluator.cs
r480 r482 32 32 namespace HeuristicLab.StructureIdentification { 33 33 public class MeanSquaredErrorEvaluator : GPEvaluatorBase { 34 protected DoubleData mse; 34 35 public override string Description { 35 36 get { … … 41 42 public MeanSquaredErrorEvaluator() 42 43 : base() { 44 AddVariableInfo(new VariableInfo("MSE", "The mean squared error of the model", typeof(DoubleData), VariableKind.New)); 43 45 } 44 46 45 public override double Evaluate(int start, int end) { 47 public override IOperation Apply(IScope scope) { 48 mse = GetVariableValue<DoubleData>("MSE", scope, false, false); 49 if(mse == null) { 50 mse = new DoubleData(); 51 scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("MSE"), mse)); 52 } 53 54 return base.Apply(scope); 55 } 56 57 public override void Evaluate(int start, int end) { 46 58 double errorsSquaredSum = 0; 47 59 for(int sample = start; sample < end; sample++) { … … 59 71 errorsSquaredSum = double.MaxValue; 60 72 } 61 returnerrorsSquaredSum;73 mse.Data = errorsSquaredSum; 62 74 } 63 75 } -
trunk/sources/HeuristicLab.StructureIdentification/Evaluation/SimpleEvaluator.cs
r480 r482 52 52 } 53 53 54 public override doubleEvaluate(int start, int end) {54 public override void Evaluate(int start, int end) { 55 55 for(int sample = start; sample < end; sample++) { 56 56 ItemList row = new ItemList(); … … 62 62 values.Add(row); 63 63 } 64 return double.NaN;65 64 } 66 65 } -
trunk/sources/HeuristicLab.StructureIdentification/Evaluation/TheilInequalityCoefficientEvaluator.cs
r480 r482 33 33 public class TheilInequalityCoefficientEvaluator : GPEvaluatorBase { 34 34 private bool differential; 35 private DoubleData theilInequaliy; 35 36 public override string Description { 36 37 get { … … 43 44 : base() { 44 45 AddVariableInfo(new VariableInfo("Differential", "Wether to calculate the coefficient for the predicted change vs. original change or for the absolute prediction vs. original value", typeof(BoolData), VariableKind.In)); 46 AddVariableInfo(new VariableInfo("TheilInequalityCoefficient", "Theil's inequality coefficient of the model", typeof(DoubleData), VariableKind.New)); 47 45 48 } 46 49 47 50 public override IOperation Apply(IScope scope) { 48 51 differential = GetVariableValue<BoolData>("Differential", scope, true).Data; 52 theilInequaliy = GetVariableValue<DoubleData>("TheilInequalityCoefficient", scope, false, false); 53 if(theilInequaliy == null) { 54 theilInequaliy = new DoubleData(); 55 scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("TheilInequalityCoefficient"), theilInequaliy)); 56 } 57 49 58 return base.Apply(scope); 50 59 } 51 60 52 public override doubleEvaluate(int start, int end) {61 public override void Evaluate(int start, int end) { 53 62 double errorsSquaredSum = 0.0; 54 63 double estimatedSquaredSum = 0.0; … … 59 68 double estimatedChange = GetEstimatedValue(sample) - prevValue; 60 69 double originalChange = GetOriginalValue(sample) - prevValue; 61 SetOriginalValue(sample, estimatedChange +prevValue);70 SetOriginalValue(sample, estimatedChange + prevValue); 62 71 if(!double.IsNaN(originalChange) && !double.IsInfinity(originalChange)) { 63 72 double error = estimatedChange - originalChange; … … 71 80 if(double.IsNaN(quality) || double.IsInfinity(quality)) 72 81 quality = double.MaxValue; 73 returnquality;82 theilInequaliy.Data = quality; 74 83 } 75 84 } -
trunk/sources/HeuristicLab.StructureIdentification/Evaluation/VarianceAccountedForEvaluator.cs
r480 r482 32 32 namespace HeuristicLab.StructureIdentification { 33 33 public class VarianceAccountedForEvaluator : GPEvaluatorBase { 34 private DoubleData vaf; 34 35 public override string Description { 35 36 get { … … 50 51 public VarianceAccountedForEvaluator() 51 52 : base() { 53 AddVariableInfo(new VariableInfo("VAF", "The variance-accounted-for quality of the model", typeof(DoubleData), VariableKind.New)); 54 52 55 } 53 56 57 public override IOperation Apply(IScope scope) { 58 vaf = GetVariableValue<DoubleData>("VAF", scope, false, false); 59 if(vaf == null) { 60 vaf = new DoubleData(); 61 scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("VAF"), vaf)); 62 } 54 63 55 public override double Evaluate(int start, int end) { 64 return base.Apply(scope); 65 } 66 67 public override void Evaluate(int start, int end) { 56 68 int nSamples = end - start; 57 69 double[] errors = new double[nSamples]; … … 73 85 quality = double.MaxValue; 74 86 } 75 returnquality;87 vaf.Data = quality; 76 88 } 77 89 }
Note: See TracChangeset
for help on using the changeset viewer.