1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using HeuristicLab.Core;
|
---|
6 | using HeuristicLab.Data;
|
---|
7 | using HeuristicLab.DataAnalysis;
|
---|
8 |
|
---|
9 | namespace 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 | }
|
---|