1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 | *
|
---|
5 | * This file is part of HeuristicLab.
|
---|
6 | *
|
---|
7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 | #endregion
|
---|
21 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using System.Text;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Data;
|
---|
28 | using HeuristicLab.DataAnalysis;
|
---|
29 |
|
---|
30 |
|
---|
31 | namespace HeuristicLab.GP.StructureIdentification.Classification {
|
---|
32 | public class ROCAnalyser : OperatorBase {
|
---|
33 |
|
---|
34 | public override string Description {
|
---|
35 | get { return @"Calculate TPR & FPR for various treshholds on dataset"; }
|
---|
36 | }
|
---|
37 |
|
---|
38 | public ROCAnalyser()
|
---|
39 | : base() {
|
---|
40 | AddVariableInfo(new VariableInfo("Values", "Item list holding the estimated and orignial values for the ROCAnalyser", typeof(ItemList), VariableKind.In));
|
---|
41 | AddVariableInfo(new VariableInfo("ROCValues", "The values of the ROCAnalyzer, namely TPR & FPR", typeof(ItemList), VariableKind.New | VariableKind.Out));
|
---|
42 | }
|
---|
43 |
|
---|
44 | public override IOperation Apply(IScope scope) {
|
---|
45 | ItemList values = GetVariableValue<ItemList>("Values", scope, true);
|
---|
46 | ItemList rocValues = GetVariableValue<ItemList>("ROCValues", scope, false, false);
|
---|
47 | if (rocValues == null) {
|
---|
48 | rocValues = new ItemList();
|
---|
49 | IVariableInfo info = GetVariableInfo("ROCValues");
|
---|
50 | if (info.Local)
|
---|
51 | AddVariable(new HeuristicLab.Core.Variable(info.ActualName, rocValues));
|
---|
52 | else
|
---|
53 | scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName(info.FormalName), rocValues));
|
---|
54 | } else
|
---|
55 | rocValues.Clear();
|
---|
56 |
|
---|
57 | //ROC Curve starts at 0,0
|
---|
58 | ItemList row = new ItemList();
|
---|
59 | row.Add(new DoubleData(0));
|
---|
60 | row.Add(new DoubleData(0));
|
---|
61 | rocValues.Add(row);
|
---|
62 |
|
---|
63 | //calculate new ROC Values
|
---|
64 | double estimated;
|
---|
65 | double original;
|
---|
66 | double positiveClassKey;
|
---|
67 | double negativeClassKey;
|
---|
68 | double truePositiveRate;
|
---|
69 | double falsePositiveRate;
|
---|
70 |
|
---|
71 | //initialize classes dictionary
|
---|
72 | Dictionary<double, List<double>> classes = new Dictionary<double, List<double>>();
|
---|
73 | foreach (ItemList value in values) {
|
---|
74 | estimated = ((DoubleData)value[0]).Data;
|
---|
75 | original = ((DoubleData)value[1]).Data;
|
---|
76 | if (!classes.ContainsKey(original))
|
---|
77 | classes[original] = new List<double>();
|
---|
78 | classes[original].Add(estimated);
|
---|
79 | }
|
---|
80 |
|
---|
81 | //check for 2 classes classification problem
|
---|
82 | if (classes.Keys.Count != 2)
|
---|
83 | throw new Exception("ROCAnalyser only handles 2 class classification problems");
|
---|
84 |
|
---|
85 | //sort estimated values in classes dictionary
|
---|
86 | foreach (List<double> estimatedValues in classes.Values)
|
---|
87 | estimatedValues.Sort();
|
---|
88 |
|
---|
89 | //calculate truePosivite- & falsePositiveRate
|
---|
90 | positiveClassKey = classes.Keys.Min<double>();
|
---|
91 | negativeClassKey = classes.Keys.Max<double>();
|
---|
92 | for (int i = 0; i < classes[negativeClassKey].Count; i++) {
|
---|
93 | truePositiveRate = classes[positiveClassKey].Count<double>(value => value < classes[negativeClassKey][i]) / classes[positiveClassKey].Count;
|
---|
94 | //stop calculation if truePositiveRate = 1; save runtime
|
---|
95 | if (truePositiveRate == 1)
|
---|
96 | break;
|
---|
97 | falsePositiveRate = (i) / classes[negativeClassKey].Count;
|
---|
98 | row = new ItemList();
|
---|
99 | row.Add(new DoubleData(falsePositiveRate));
|
---|
100 | row.Add(new DoubleData(truePositiveRate));
|
---|
101 | rocValues.Add(row);
|
---|
102 | }
|
---|
103 |
|
---|
104 | //ROC ends at 1,1
|
---|
105 | row = new ItemList();
|
---|
106 | row.Add(new DoubleData(1));
|
---|
107 | row.Add(new DoubleData(1));
|
---|
108 | rocValues.Add(row);
|
---|
109 |
|
---|
110 | return null;
|
---|
111 | }
|
---|
112 | }
|
---|
113 | }
|
---|