Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 7549 was 7549, checked in by sforsten, 12 years ago

#1776:

  • models can be selected with a check box
  • all strategies are now finished
  • major changes have been made to provide the same behaviour when getting the estimated training or test values of an ensemble
File size: 4.2 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(IEnumerable<IDiscriminantFunctionClassificationSolution> discriminantSolutions) {
52      List<double> weights = new List<double>();
53      IClassificationProblemData problemData = discriminantSolutions.ElementAt(0).ProblemData;
54      IEnumerable<double> targetValues;
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        problemData = solution.ProblemData;
67        targetValues = GetValues(problemData.Dataset.GetDoubleValues(problemData.TargetVariable).ToList(), problemData.TrainingIndizes);
68        trainingValues = targetValues.GetEnumerator();
69
70        IEnumerator<double> estimatedTrainingVal = solution.EstimatedTrainingValues.GetEnumerator();
71        IEnumerator<double> estimatedTrainingClassVal = solution.EstimatedTrainingClassValues.GetEnumerator();
72
73        double curWeight = 0.0;
74        while (estimatedTrainingVal.MoveNext() && estimatedTrainingClassVal.MoveNext() && trainingValues.MoveNext()) {
75          if (trainingValues.Current.Equals(maxClass)) {
76            if (estimatedTrainingVal.Current >= maxClass)
77              curWeight += 1.0;
78            else {
79              double distanceToPoint = Math.Abs(estimatedTrainingVal.Current - maxClass);
80              curWeight += Math.Max(1.0 - (1.0 / halfDistanceBetweenClasses) * distanceToPoint, -1.0);
81            }
82          } else if (trainingValues.Current.Equals(minClass)) {
83            if (estimatedTrainingVal.Current <= minClass)
84              curWeight += 1.0;
85            else {
86              double distanceToPoint = Math.Abs(estimatedTrainingVal.Current - minClass);
87              curWeight += Math.Max(1.0 - (1.0 / halfDistanceBetweenClasses) * distanceToPoint, -1.0);
88            }
89          }
90        }
91        // normalize the weight (otherwise a model with a bigger training partition would probably be better)
92        weights.Add(curWeight / targetValues.Count());
93      }
94      return weights;
95    }
96  }
97}
Note: See TracBrowser for help on using the repository browser.