- Timestamp:
- 10/29/08 11:21:04 (16 years ago)
- Location:
- trunk/sources/HeuristicLab.GP.StructureIdentification.Classification
- Files:
-
- 1 added
- 1 deleted
- 5 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 } -
trunk/sources/HeuristicLab.GP.StructureIdentification.Classification/ClassificationMeanSquaredErrorEvaluator.cs
r668 r702 29 29 30 30 namespace HeuristicLab.GP.StructureIdentification.Classification { 31 public class ClassificationMeanSquaredErrorEvaluator : MeanSquaredErrorEvaluator { 32 private const double EPSILON = 1.0E-6; 33 private double[] classesArr; 31 public class ClassificationMeanSquaredErrorEvaluator : GPClassificationEvaluatorBase { 32 private const double EPSILON = 1.0E-7; 34 33 public override string Description { 35 34 get { … … 41 40 public ClassificationMeanSquaredErrorEvaluator() 42 41 : base() { 43 AddVariableInfo(new VariableInfo(" TargetClassValues", "The original class values of target variable (for instance negative=0 and positive=1).", typeof(ItemList<DoubleData>), VariableKind.In));42 AddVariableInfo(new VariableInfo("MSE", "The mean squared error of the model", typeof(DoubleData), VariableKind.New)); 44 43 } 45 44 46 public override IOperation Apply(IScope scope) { 47 ItemList<DoubleData> classes = GetVariableValue<ItemList<DoubleData>>("TargetClassValues", scope, true); 48 classesArr = new double[classes.Count]; 49 for(int i = 0; i < classesArr.Length; i++) classesArr[i] = classes[i].Data; 50 Array.Sort(classesArr); 51 return base.Apply(scope); 52 } 53 54 public override void Evaluate(int start, int end) { 45 public override void Evaluate(IScope scope, BakedTreeEvaluator evaluator, HeuristicLab.DataAnalysis.Dataset dataset, int targetVariable, double[] classes, double[] thresholds, int start, int end) 46 { 55 47 double errorsSquaredSum = 0; 56 48 for(int sample = start; sample < end; sample++) { 57 double estimated = GetEstimatedValue(sample); 58 double original = GetOriginalValue(sample); 59 SetOriginalValue(sample, estimated); 49 double estimated = evaluator.Evaluate(sample); 50 double original = dataset.GetValue(targetVariable, sample); 60 51 if(!double.IsNaN(original) && !double.IsInfinity(original)) { 61 52 double error = estimated - original; … … 63 54 // on the lower end and upper end only add linear error if the absolute error is larger than 1 64 55 // the error>1.0 constraint is needed for balance because in the interval ]-1, 1[ the squared error is smaller than the absolute error 65 if((IsEqual(original, classes Arr[0]) && error < -1.0) ||66 (IsEqual(original, classes Arr[classesArr.Length - 1]) && error > 1.0)) {56 if((IsEqual(original, classes[0]) && error < -1.0) || 57 (IsEqual(original, classes[classes.Length - 1]) && error > 1.0)) { 67 58 errorsSquaredSum += Math.Abs(error); // only add linear error below the smallest class or above the largest class 68 59 } else { … … 76 67 errorsSquaredSum = double.MaxValue; 77 68 } 69 70 DoubleData mse = GetVariableValue<DoubleData>("MSE", scope, false, false); 71 if(mse == null) { 72 mse = new DoubleData(); 73 scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("MSE"), mse)); 74 } 75 78 76 mse.Data = errorsSquaredSum; 79 77 } -
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 } -
trunk/sources/HeuristicLab.GP.StructureIdentification.Classification/HeuristicLab.GP.StructureIdentification.Classification.csproj
r669 r702 67 67 <Compile Include="ClassificationMeanSquaredErrorEvaluator.cs" /> 68 68 <Compile Include="ConfusionMatrixEvaluator.cs" /> 69 <Compile Include="GPClassificationEvaluatorBase.cs" /> 69 70 <Compile Include="CrossValidation.cs" /> 70 71 <Compile Include="FunctionLibraryInjector.cs"> … … 72 73 </Compile> 73 74 <Compile Include="HeuristicLabGPClassificationPlugin.cs" /> 74 <Compile Include="MCCEvaluator.cs" />75 75 <Compile Include="MulticlassModeller.cs" /> 76 76 <Compile Include="MulticlassOneVsOneAnalyzer.cs" /> -
trunk/sources/HeuristicLab.GP.StructureIdentification.Classification/MulticlassOneVsOneAnalyzer.cs
r668 r702 79 79 80 80 BakedTreeEvaluator evaluator = new BakedTreeEvaluator(); 81 evaluator.ResetEvaluator(functionTree, dataset );81 evaluator.ResetEvaluator(functionTree, dataset, targetVariable, samplesStart, samplesEnd, 1.0); 82 82 83 83 for(int i = 0; i < (samplesEnd - samplesStart); i++) {
Note: See TracChangeset
for help on using the changeset viewer.