Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1776:

  • improvements in the usage of the WeightCalculators
  • small changes in all class which inherit from the WeightCalculator class
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("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    protected 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      IClassificationProblemData problemData = classificationSolutions.ElementAt(0).ProblemData;
65      IEnumerable<double> targetValues = GetValues(problemData.Dataset.GetDoubleValues(problemData.TargetVariable).ToList(), problemData.TrainingIndizes);
66      IEnumerator<double> trainingValues;
67      double avg = problemData.ClassValues.Average();
68
69      foreach (var solution in discriminantSolutions) {
70        IEnumerator<double> estimatedTrainingVal = solution.EstimatedTrainingValues.GetEnumerator();
71        IEnumerator<double> estimatedTrainingClassVal = solution.EstimatedTrainingClassValues.GetEnumerator();
72
73        trainingValues = targetValues.GetEnumerator();
74        double curWeight = 0.0;
75        while (estimatedTrainingVal.MoveNext() && estimatedTrainingClassVal.MoveNext() && trainingValues.MoveNext()) {
76          if (estimatedTrainingClassVal.Current.Equals(trainingValues.Current)) {
77            curWeight += 0.5;
78            double distanceToPoint = Math.Abs(estimatedTrainingVal.Current - avg);
79            double distanceToClass = Math.Abs(trainingValues.Current - avg);
80            if (trainingValues.Current > avg && estimatedTrainingVal.Current > avg
81             || trainingValues.Current < avg && estimatedTrainingVal.Current < avg)
82              curWeight += distanceToPoint < distanceToClass ? (0.5 / distanceToClass) * distanceToPoint : 0.5;
83          }
84        }
85        weights.Add(curWeight);
86      }
87      return weights;
88    }
89
90    private IEnumerable<double> GetValues(IList<double> targetValues, IEnumerable<int> indizes) {
91      return from i in indizes
92             select targetValues[i];
93    }
94  }
95}
Note: See TracBrowser for help on using the repository browser.