1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2012 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.Collections;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
28 |
|
---|
29 | namespace HeuristicLab.Problems.DataAnalysis {
|
---|
30 | [StorableClass]
|
---|
31 | [Item("MedianPointThresholdCalculator", "")]
|
---|
32 | public class MedianPointThresholdCalculator : DiscriminantClassificationWeightCalculator {
|
---|
33 |
|
---|
34 | public MedianPointThresholdCalculator()
|
---|
35 | : base() {
|
---|
36 | }
|
---|
37 | [StorableConstructor]
|
---|
38 | protected MedianPointThresholdCalculator(bool deserializing) : base(deserializing) { }
|
---|
39 | protected MedianPointThresholdCalculator(MedianPointThresholdCalculator original, Cloner cloner)
|
---|
40 | : base(original, cloner) {
|
---|
41 | }
|
---|
42 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
43 | return new MedianPointThresholdCalculator(this, cloner);
|
---|
44 | }
|
---|
45 |
|
---|
46 | protected double[] threshold;
|
---|
47 | protected double[] classValues;
|
---|
48 |
|
---|
49 | /// <summary>
|
---|
50 | ///
|
---|
51 | /// </summary>
|
---|
52 | /// <param name="discriminantSolutions"></param>
|
---|
53 | /// <returns>median instead of weights, because it doesn't use any weights</returns>
|
---|
54 | protected override IEnumerable<double> DiscriminantCalculateWeights(IEnumerable<IDiscriminantFunctionClassificationSolution> discriminantSolutions) {
|
---|
55 | List<List<double>> estimatedValues = new List<List<double>>();
|
---|
56 | List<List<double>> estimatedClassValues = new List<List<double>>();
|
---|
57 |
|
---|
58 | List<IClassificationProblemData> solutionProblemData = discriminantSolutions.Select(sol => sol.ProblemData).ToList();
|
---|
59 | Dataset dataSet = solutionProblemData[0].Dataset;
|
---|
60 | IEnumerable<int> rows = Enumerable.Range(0, dataSet.Rows);
|
---|
61 | foreach (var solution in discriminantSolutions) {
|
---|
62 | estimatedValues.Add(solution.Model.GetEstimatedValues(dataSet, rows).ToList());
|
---|
63 | estimatedClassValues.Add(solution.Model.GetEstimatedValues(dataSet, rows).ToList());
|
---|
64 | }
|
---|
65 |
|
---|
66 | List<double> median = new List<double>();
|
---|
67 | List<double> targetValues = dataSet.GetDoubleValues(solutionProblemData[0].TargetVariable).ToList();
|
---|
68 | IList<double> curTrainingpoints = new List<double>();
|
---|
69 | int removed = 0;
|
---|
70 | int count = targetValues.Count;
|
---|
71 | for (int point = 0; point < count; point++) {
|
---|
72 | curTrainingpoints.Clear();
|
---|
73 | for (int solutionPos = 0; solutionPos < solutionProblemData.Count; solutionPos++) {
|
---|
74 | if (PointInTraining(solutionProblemData[solutionPos], point)) {
|
---|
75 | curTrainingpoints.Add(estimatedValues[solutionPos][point]);
|
---|
76 | }
|
---|
77 | }
|
---|
78 | if (curTrainingpoints.Count > 0)
|
---|
79 | median.Add(GetMedian(curTrainingpoints.OrderBy(p => p).ToList()));
|
---|
80 | else {
|
---|
81 | //remove not used points
|
---|
82 | targetValues.RemoveAt(point - removed);
|
---|
83 | removed++;
|
---|
84 | }
|
---|
85 | }
|
---|
86 | AccuracyMaximizationThresholdCalculator.CalculateThresholds(solutionProblemData[0], median, targetValues, out classValues, out threshold);
|
---|
87 | return Enumerable.Repeat<double>(1, discriminantSolutions.Count());
|
---|
88 | }
|
---|
89 |
|
---|
90 | protected override double DiscriminantAggregateEstimatedClassValues(IDictionary<IClassificationSolution, double> estimatedClassValues, IDictionary<IDiscriminantFunctionClassificationSolution, double> estimatedValues) {
|
---|
91 | IList<double> values = estimatedValues.Select(x => x.Value).ToList();
|
---|
92 | if (values.Count <= 0)
|
---|
93 | return double.NaN;
|
---|
94 | double median = GetMedian(values);
|
---|
95 | return GetClassValueToMedian(median);
|
---|
96 | }
|
---|
97 | private double GetClassValueToMedian(double median) {
|
---|
98 | double classValue = classValues.First();
|
---|
99 | for (int i = 0; i < classValues.Count(); i++) {
|
---|
100 | if (median > threshold[i])
|
---|
101 | classValue = classValues[i];
|
---|
102 | else
|
---|
103 | break;
|
---|
104 | }
|
---|
105 | return classValue;
|
---|
106 | }
|
---|
107 |
|
---|
108 | protected override double GetDiscriminantConfidence(IEnumerable<IDiscriminantFunctionClassificationSolution> solutions, int index, double estimatedClassValue) {
|
---|
109 | // only works with binary classification
|
---|
110 | if (!classValues.Count().Equals(2))
|
---|
111 | return double.NaN;
|
---|
112 | Dataset dataset = solutions.First().ProblemData.Dataset;
|
---|
113 | IList<double> values = solutions.Select(s => s.Model.GetEstimatedValues(dataset, Enumerable.Repeat(index, 1)).First()).ToList();
|
---|
114 | if (values.Count <= 0)
|
---|
115 | return double.NaN;
|
---|
116 | double median = GetMedian(values);
|
---|
117 | if (estimatedClassValue.Equals(classValues[0])) {
|
---|
118 | if (median < estimatedClassValue)
|
---|
119 | return 1;
|
---|
120 | else if (median >= threshold[1])
|
---|
121 | return 0;
|
---|
122 | else {
|
---|
123 | double distance = threshold[1] - classValues[0];
|
---|
124 | return (1 / distance) * (median - classValues[0]);
|
---|
125 | }
|
---|
126 | } else if (estimatedClassValue.Equals(classValues[1])) {
|
---|
127 | if (median > estimatedClassValue)
|
---|
128 | return 1;
|
---|
129 | else if (median <= threshold[1])
|
---|
130 | return 0;
|
---|
131 | else {
|
---|
132 | double distance = classValues[1] - threshold[1];
|
---|
133 | return (1 / distance) * (classValues[1] - median);
|
---|
134 | }
|
---|
135 | } else
|
---|
136 | return double.NaN;
|
---|
137 | }
|
---|
138 |
|
---|
139 | private double GetMedian(IList<double> estimatedValues) {
|
---|
140 | int count = estimatedValues.Count;
|
---|
141 | if (count % 2 == 0)
|
---|
142 | return 0.5 * (estimatedValues[count / 2 - 1] + estimatedValues[count / 2]);
|
---|
143 | else
|
---|
144 | return estimatedValues[count / 2];
|
---|
145 | }
|
---|
146 | }
|
---|
147 | }
|
---|