Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
09/14/09 17:30:02 (15 years ago)
Author:
gkronber
Message:

Worked on SVM classification engine. #625

File:
1 moved

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Modeling/3.2/SimpleAccuracyEvaluator.cs

    r2348 r2351  
    2424using HeuristicLab.Data;
    2525using HeuristicLab.DataAnalysis;
     26using System.Linq;
     27using HeuristicLab.Common;
    2628
    27 namespace HeuristicLab.GP.StructureIdentification.Classification {
    28   public class AccuracyEvaluator : GPClassificationEvaluatorBase {
    29     private const double EPSILON = 1.0E-6;
     29namespace HeuristicLab.Modeling {
     30  public class SimpleAccuracyEvaluator : SimpleEvaluatorBase {
     31    public override string OutputVariableName {
     32      get {
     33        return "Accuracy";
     34      }
     35    }
    3036    public override string Description {
    3137      get {
     
    3440    }
    3541
    36     public AccuracyEvaluator()
    37       : base() {
    38       AddVariableInfo(new VariableInfo("Accuracy", "The total accuracy of the model (ratio of correctly classified instances to total number of instances)", typeof(DoubleData), VariableKind.New));
     42    public override double Evaluate(double[,] values) {
     43      return Calculate(values);
    3944    }
    4045
    41     public override void Evaluate(IScope scope, ITreeEvaluator evaluator, Dataset dataset, int targetVariable, double[] classes, double[] thresholds, int start, int end) {
    42       DoubleData accuracy = GetVariableValue<DoubleData>("Accuracy", scope, false, false);
    43       if (accuracy == null) {
    44         accuracy = new DoubleData();
    45         scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("Accuracy"), accuracy));
    46       }
     46    public static double Calculate(double[,] values) {
     47      int nSamples = values.GetLength(0);
     48      int nCorrect = 0;
     49      double[] classes = CalculateTargetClasses(values);
     50      double[] thresholds = CalculateThresholds(classes);
    4751
    48       int nSamples = end - start;
    49       int nCorrect = 0;
    50       for (int sample = start; sample < end; sample++) {
    51         double est = evaluator.Evaluate(sample);
    52         double origClass = dataset.GetValue(sample, targetVariable);
     52      for (int sample = 0; sample < nSamples; sample++) {
     53        double est = values[sample, 0];
     54        double origClass = values[sample, 1];
    5355        double estClass = double.NaN;
    5456        // if estimation is lower than the smallest threshold value -> estimated class is the lower class
     
    6567          }
    6668        }
    67         if (Math.Abs(estClass - origClass) < EPSILON) nCorrect++;
     69        if (estClass.IsAlmost(origClass)) nCorrect++;
    6870      }
    69       accuracy.Data = nCorrect / (double)nSamples;
     71      return nCorrect / (double)nSamples;
     72    }
     73
     74    public static double[] CalculateTargetClasses(double[,] values) {
     75      int n = values.GetLength(0);
     76      double[] original = new double[n];
     77      for (int i = 0; i < n; i++) original[i] = values[i, 1];
     78      return original.OrderBy(x => x).Distinct().ToArray();
     79    }
     80
     81    public static double[] CalculateThresholds(double[] targetClasses) {
     82      double[] thresholds = new double[targetClasses.Length - 1];
     83      for (int i = 1; i < targetClasses.Length; i++) {
     84        thresholds[i - 1] = (targetClasses[i - 1] + targetClasses[i]) / 2.0;
     85      }
     86      return thresholds;
    7087    }
    7188  }
Note: See TracChangeset for help on using the changeset viewer.