[7491] | 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", "")]
|
---|
[7531] | 35 | public class NeighbourhoodWeightCalculator : DiscriminantClassificationWeightCalculator {
|
---|
[7491] | 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 |
|
---|
[7549] | 51 | protected override IEnumerable<double> DiscriminantCalculateWeights(IEnumerable<IDiscriminantFunctionClassificationSolution> discriminantSolutions) {
|
---|
| 52 | List<List<double>> estimatedValues = new List<List<double>>();
|
---|
| 53 | List<List<double>> estimatedClassValues = new List<List<double>>();
|
---|
| 54 |
|
---|
| 55 | List<IClassificationProblemData> solutionProblemData = discriminantSolutions.Select(sol => sol.ProblemData).ToList();
|
---|
| 56 | Dataset dataSet = solutionProblemData[0].Dataset;
|
---|
| 57 | IEnumerable<int> rows = Enumerable.Range(0, dataSet.Rows);
|
---|
[7531] | 58 | foreach (var solution in discriminantSolutions) {
|
---|
[7549] | 59 | estimatedValues.Add(solution.Model.GetEstimatedValues(dataSet, rows).ToList());
|
---|
[7562] | 60 | estimatedClassValues.Add(solution.Model.GetEstimatedClassValues(dataSet, rows).ToList());
|
---|
[7491] | 61 | }
|
---|
| 62 |
|
---|
[7549] | 63 | List<double> weights = Enumerable.Repeat<double>(0, solutionProblemData.Count).ToList<double>();
|
---|
| 64 | List<double> targetValues = dataSet.GetDoubleValues(solutionProblemData[0].TargetVariable).ToList();
|
---|
[7491] | 65 |
|
---|
| 66 | double pointAvg, help;
|
---|
| 67 | int count;
|
---|
[7549] | 68 | for (int point = 0; point < targetValues.Count; point++) {
|
---|
[7491] | 69 | pointAvg = 0.0;
|
---|
| 70 | count = 0;
|
---|
[7549] | 71 | for (int solutionPos = 0; solutionPos < estimatedClassValues.Count; solutionPos++) {
|
---|
| 72 | if (PointInTraining(solutionProblemData[solutionPos], point)
|
---|
| 73 | && estimatedClassValues[solutionPos][point].Equals(targetValues[point])) {
|
---|
| 74 | pointAvg += estimatedValues[solutionPos][point];
|
---|
[7491] | 75 | count++;
|
---|
| 76 | }
|
---|
| 77 | }
|
---|
| 78 | pointAvg /= (double)count;
|
---|
[7549] | 79 | for (int solutionPos = 0; solutionPos < estimatedClassValues.Count; solutionPos++) {
|
---|
| 80 | if (PointInTraining(solutionProblemData[solutionPos], point)
|
---|
| 81 | && estimatedClassValues[solutionPos][point].Equals(targetValues[point])) {
|
---|
| 82 | weights[solutionPos] += 0.5;
|
---|
| 83 | help = Math.Abs(estimatedValues[solutionPos][point] - 0.5);
|
---|
| 84 | weights[solutionPos] += help < 0.5 ? 0.5 - help : 0.0;
|
---|
[7491] | 85 | }
|
---|
| 86 | }
|
---|
| 87 | }
|
---|
[7549] | 88 | // normalize the weight (otherwise a model with a bigger training partition would probably be better)
|
---|
| 89 | for (int i = 0; i < weights.Count; i++) {
|
---|
| 90 | weights[i] = weights[i] / solutionProblemData[i].TrainingIndizes.Count();
|
---|
| 91 | }
|
---|
[7504] | 92 | return weights;
|
---|
[7491] | 93 | }
|
---|
| 94 | }
|
---|
| 95 | }
|
---|