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;
|
---|
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 | /// <summary>
|
---|
31 | ///
|
---|
32 | /// </summary>
|
---|
33 | [StorableClass]
|
---|
34 | [Item("NeighbourhoodWeightCalculator", "")]
|
---|
35 | public class NeighbourhoodWeightCalculator : DiscriminantClassificationWeightCalculator {
|
---|
36 |
|
---|
37 | public NeighbourhoodWeightCalculator()
|
---|
38 | : base() {
|
---|
39 | }
|
---|
40 |
|
---|
41 | [StorableConstructor]
|
---|
42 | protected NeighbourhoodWeightCalculator(bool deserializing) : base(deserializing) { }
|
---|
43 | protected NeighbourhoodWeightCalculator(NeighbourhoodWeightCalculator original, Cloner cloner)
|
---|
44 | : base(original, cloner) {
|
---|
45 | }
|
---|
46 |
|
---|
47 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
48 | return new NeighbourhoodWeightCalculator(this, cloner);
|
---|
49 | }
|
---|
50 |
|
---|
51 | protected override IEnumerable<double> DiscriminantCalculateWeights(ItemCollection<IDiscriminantFunctionClassificationSolution> discriminantSolutions) {
|
---|
52 | List<List<double>> estimatedTrainingValEnumerators = new List<List<double>>();
|
---|
53 | List<List<double>> estimatedTrainingClassValEnumerators = new List<List<double>>();
|
---|
54 | foreach (var solution in discriminantSolutions) {
|
---|
55 | estimatedTrainingValEnumerators.Add(solution.EstimatedTrainingValues.ToList());
|
---|
56 | estimatedTrainingClassValEnumerators.Add(solution.EstimatedTrainingClassValues.ToList());
|
---|
57 | }
|
---|
58 |
|
---|
59 | List<double> weights = Enumerable.Repeat<double>(0, discriminantSolutions.Count()).ToList<double>();
|
---|
60 |
|
---|
61 | IClassificationProblemData problemData = discriminantSolutions.ElementAt(0).ProblemData;
|
---|
62 | List<double> targetValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable).ToList();
|
---|
63 | List<double> trainingVal = GetValues(targetValues, problemData.TrainingIndizes).ToList();
|
---|
64 |
|
---|
65 | double pointAvg, help;
|
---|
66 | int count;
|
---|
67 | for (int point = 0; point < estimatedTrainingClassValEnumerators.First().Count; point++) {
|
---|
68 | pointAvg = 0.0;
|
---|
69 | count = 0;
|
---|
70 | for (int solution = 0; solution < estimatedTrainingClassValEnumerators.Count; solution++) {
|
---|
71 | if (estimatedTrainingClassValEnumerators[solution][point].Equals(targetValues[point])) {
|
---|
72 | pointAvg += estimatedTrainingValEnumerators[solution][point];
|
---|
73 | count++;
|
---|
74 | }
|
---|
75 | }
|
---|
76 | pointAvg /= (double)count;
|
---|
77 | for (int solution = 0; solution < estimatedTrainingClassValEnumerators.Count; solution++) {
|
---|
78 | if (estimatedTrainingClassValEnumerators[solution][point].Equals(targetValues[point])) {
|
---|
79 | weights[solution] += 0.5;
|
---|
80 | help = Math.Abs(estimatedTrainingValEnumerators[solution][point] - 0.5);
|
---|
81 | weights[solution] += help < 0.5 ? 0.5 - help : 0.0;
|
---|
82 | }
|
---|
83 | }
|
---|
84 | }
|
---|
85 | return weights;
|
---|
86 | }
|
---|
87 | }
|
---|
88 | }
|
---|