Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ClassificationEnsembleVoting/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Classification/WeightCalculators/ContinuousPointCertaintyWeightCalculator.cs @ 7531

Last change on this file since 7531 was 7531, checked in by sforsten, 13 years ago

#1776:

  • 2 more strategies have been implemented
  • major changes in the inheritance have been made to make it possible to add strategies which don't use a voting strategy with weights
  • ClassificationEnsembleSolutionEstimatedClassValuesView doesn't currently show the confidence (has been removed for test purpose)
File size: 4.1 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Problems.DataAnalysis {
30  /// <summary>
31  ///
32  /// </summary>
33  [StorableClass]
34  [Item("ContinuousPointCertaintyWeightCalculator", "")]
35  public class ContinuousPointCertaintyWeightCalculator : DiscriminantClassificationWeightCalculator {
36
37    public ContinuousPointCertaintyWeightCalculator()
38      : base() {
39    }
40
41    [StorableConstructor]
42    protected ContinuousPointCertaintyWeightCalculator(bool deserializing) : base(deserializing) { }
43    protected ContinuousPointCertaintyWeightCalculator(ContinuousPointCertaintyWeightCalculator original, Cloner cloner)
44      : base(original, cloner) {
45    }
46
47    public override IDeepCloneable Clone(Cloner cloner) {
48      return new ContinuousPointCertaintyWeightCalculator(this, cloner);
49    }
50
51    protected override IEnumerable<double> DiscriminantCalculateWeights(ItemCollection<IDiscriminantFunctionClassificationSolution> discriminantSolutions) {
52      List<double> weights = new List<double>();
53      IClassificationProblemData problemData = discriminantSolutions.ElementAt(0).ProblemData;
54      IEnumerable<double> targetValues = GetValues(problemData.Dataset.GetDoubleValues(problemData.TargetVariable).ToList(), problemData.TrainingIndizes);
55      IEnumerator<double> trainingValues;
56
57      //only works for binary classification
58      if (!problemData.ClassValues.Count().Equals(2))
59        return Enumerable.Repeat<double>(1, discriminantSolutions.Count);
60
61      double maxClass = problemData.ClassValues.Max();
62      double minClass = problemData.ClassValues.Min();
63      double halfDistanceBetweenClasses = (maxClass - minClass) / 2;
64
65      foreach (var solution in discriminantSolutions) {
66        IEnumerator<double> estimatedTrainingVal = solution.EstimatedTrainingValues.GetEnumerator();
67        IEnumerator<double> estimatedTrainingClassVal = solution.EstimatedTrainingClassValues.GetEnumerator();
68
69        trainingValues = targetValues.GetEnumerator();
70        double curWeight = 0.0;
71        while (estimatedTrainingVal.MoveNext() && estimatedTrainingClassVal.MoveNext() && trainingValues.MoveNext()) {
72          //if (estimatedTrainingClassVal.Current.Equals(trainingValues.Current)) {
73          if (trainingValues.Current.Equals(maxClass)) {
74            if (estimatedTrainingVal.Current >= maxClass)
75              curWeight += 1.0;
76            else {
77              double distanceToPoint = Math.Abs(estimatedTrainingVal.Current - maxClass);
78              curWeight += Math.Max(1.0 - (1.0 / halfDistanceBetweenClasses) * distanceToPoint, -1.0);
79            }
80          } else if (trainingValues.Current.Equals(minClass)) {
81            if (estimatedTrainingVal.Current <= minClass)
82              curWeight += 1.0;
83            else {
84              double distanceToPoint = Math.Abs(estimatedTrainingVal.Current - minClass);
85              curWeight += Math.Max(1.0 - (1.0 / halfDistanceBetweenClasses) * distanceToPoint, -1.0);
86            }
87          }
88          //}
89        }
90        weights.Add(curWeight);
91      }
92      return weights;
93    }
94  }
95}
Note: See TracBrowser for help on using the repository browser.