Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.SupportVectorMachines/3.2/SimpleR2Evaluator.cs @ 1840

Last change on this file since 1840 was 1819, checked in by mkommend, 15 years ago

created new project for LibSVM source files (ticket #619)

File size: 1.9 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using HeuristicLab.Core;
6using HeuristicLab.Data;
7using HeuristicLab.DataAnalysis;
8
9namespace HeuristicLab.SupportVectorMachines {
10  public class SimpleR2Evaluator : OperatorBase{
11
12    public SimpleR2Evaluator()
13      : base() {
14      AddVariableInfo(new VariableInfo("Values", "Target vs predicted values", typeof(ItemList), VariableKind.In));
15      AddVariableInfo(new VariableInfo("R2", "Coefficient of determination", typeof(DoubleData), VariableKind.New | VariableKind.Out));
16    }
17
18    public override IOperation Apply(IScope scope) {
19      ItemList values = GetVariableValue<ItemList>("Values", scope, true);
20
21      double targetMean = 0;
22      double sse = 0;
23      double cnt = 0;
24      foreach (ItemList row in values) {                               
25        double estimated = ((DoubleData)row[0]).Data;
26        double target = ((DoubleData)row[1]).Data;
27        if (!double.IsNaN(estimated) && !double.IsInfinity(estimated) &&
28            !double.IsNaN(target) && !double.IsInfinity(target)) {
29          targetMean += target;
30          double error = estimated - target;
31          sse += error * error;
32          cnt++;
33        }
34      }
35      targetMean /= cnt;
36
37      double targetDeviationTotalSumOfSquares = 0;
38      foreach (ItemList row in values) {
39        double target = ((DoubleData)row[1]).Data;
40        if (!double.IsNaN(target) && !double.IsInfinity(target)) {
41          target = target - targetMean;
42          target = target * target;
43          targetDeviationTotalSumOfSquares += target;
44        }
45      }
46      double quality = 1 - sse / targetDeviationTotalSumOfSquares;
47      if (quality > 1)
48        throw new InvalidProgramException();
49
50      scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("R2"), new DoubleData(quality)));
51      return null;
52    }
53  }
54}
Note: See TracBrowser for help on using the repository browser.