Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ClassificationEnsembleVoting/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Classification/WeightCalculators/PointCertaintyWeightCalculator.cs @ 7491

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

#1776:

  • two new weight calculators have been implemented
  • one has been deleted, because it didn't work as expected
File size: 4.0 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("PointCertaintyWeightCalculator", "")]
35  public class PointCertaintyWeightCalculator : WeightCalculator {
36
37    public PointCertaintyWeightCalculator()
38      : base() {
39    }
40
41    [StorableConstructor]
42    protected PointCertaintyWeightCalculator(bool deserializing) : base(deserializing) { }
43    protected PointCertaintyWeightCalculator(PointCertaintyWeightCalculator original, Cloner cloner)
44      : base(original, cloner) {
45    }
46
47    public override IDeepCloneable Clone(Cloner cloner) {
48      return new PointCertaintyWeightCalculator(this, cloner);
49    }
50
51    public override IEnumerable<double> CalculateWeights(ItemCollection<IClassificationSolution> classificationSolutions) {
52      if (classificationSolutions.Count <= 0)
53        return new List<double>();
54
55      if (!classificationSolutions.All(x => x is IDiscriminantFunctionClassificationSolution))
56        return Enumerable.Repeat<double>(1, classificationSolutions.Count);
57
58      ItemCollection<IDiscriminantFunctionClassificationSolution> discriminantSolutions = new ItemCollection<IDiscriminantFunctionClassificationSolution>();
59      foreach (var solution in classificationSolutions) {
60        discriminantSolutions.Add((IDiscriminantFunctionClassificationSolution)solution);
61      }
62
63      List<double> weights = new List<double>();
64
65      IClassificationProblemData problemData = classificationSolutions.ElementAt(0).ProblemData;
66      IList<double> targetValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable).ToList();
67      IEnumerator<double> testVal;
68
69      double avg = problemData.ClassValues.Average();
70
71      foreach (var solution in discriminantSolutions) {
72        IEnumerator<double> estimatedTest = solution.EstimatedTestValues.GetEnumerator();
73        IEnumerator<double> estimatedTestClass = solution.EstimatedTestClassValues.GetEnumerator();
74
75        testVal = GetValues(targetValues, problemData.TestIndizes).GetEnumerator();
76        double curWeight = 0.0;
77        while (estimatedTest.MoveNext() && estimatedTestClass.MoveNext() && testVal.MoveNext()) {
78          if (estimatedTestClass.Current.Equals(testVal.Current)) {
79            curWeight += 0.5;
80            double distanceToPoint = Math.Abs(estimatedTest.Current - avg);
81            double distanceToClass = Math.Abs(testVal.Current - avg);
82            if (testVal.Current > avg && estimatedTest.Current > avg
83             || testVal.Current < avg && estimatedTest.Current < avg)
84              curWeight += distanceToPoint < distanceToClass ? (0.5 / distanceToClass) * distanceToPoint : 0.5;
85          }
86        }
87        weights.Add(curWeight);
88      }
89      return weights.Select(x => x / weights.Sum());
90    }
91
92    private IEnumerable<double> GetValues(IList<double> targetValues, IEnumerable<int> indizes) {
93      return from i in indizes
94             select targetValues[i];
95    }
96  }
97}
Note: See TracBrowser for help on using the repository browser.