source: branches/ClassificationEnsembleVoting/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Classification/ClassificationEnsembleSolution.cs @ 7562

Last change on this file since 7562 was 7562, checked in by sforsten, 8 years ago

#1776:

  • bug fix in NeighbourhoodWeightCalculator
  • added GetConfidence method to IClassificationEnsembleSolutionWeightCalculator
  • adjusted the confidence column in ClassificationEnsembleSolutionEstimatedClassValuesView
File size: 11.3 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.Collections;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30using HeuristicLab.Problems.DataAnalysis.Interfaces.Classification;
31
32namespace HeuristicLab.Problems.DataAnalysis {
33  /// <summary>
34  /// Represents classification solutions that contain an ensemble of multiple classification models
35  /// </summary>
36  [StorableClass]
37  [Item("Classification Ensemble Solution", "A classification solution that contains an ensemble of multiple classification models")]
38  [Creatable("Data Analysis - Ensembles")]
39  public sealed class ClassificationEnsembleSolution : ClassificationSolution, IClassificationEnsembleSolution {
40    public new IClassificationEnsembleModel Model {
41      get { return (IClassificationEnsembleModel)base.Model; }
42    }
43    public new ClassificationEnsembleProblemData ProblemData {
44      get { return (ClassificationEnsembleProblemData)base.ProblemData; }
45      set { base.ProblemData = value; }
46    }
47
48    private readonly CheckedItemCollection<IClassificationSolution> classificationSolutions;
49    public ICheckedItemCollection<IClassificationSolution> ClassificationSolutions {
50      get { return classificationSolutions; }
51    }
52
53    private IClassificationEnsembleSolutionWeightCalculator weightCalculator;
54
55    public IClassificationEnsembleSolutionWeightCalculator WeightCalculator {
56      set {
57        if (value != null) {
58          weightCalculator = value;
59          if (!ProblemData.IsEmpty)
60            RecalculateResults();
61        }
62      }
63      get { return weightCalculator; }
64    }
65
66    [StorableConstructor]
67    private ClassificationEnsembleSolution(bool deserializing)
68      : base(deserializing) {
69      classificationSolutions = new CheckedItemCollection<IClassificationSolution>();
70    }
71    [StorableHook(HookType.AfterDeserialization)]
72    private void AfterDeserialization() {
73      foreach (var model in Model.Models) {
74        IClassificationProblemData problemData = (IClassificationProblemData)ProblemData.Clone();
75        classificationSolutions.Add(model.CreateClassificationSolution(problemData));
76      }
77      RegisterClassificationSolutionsEventHandler();
78    }
79
80    private ClassificationEnsembleSolution(ClassificationEnsembleSolution original, Cloner cloner)
81      : base(original, cloner) {
82      classificationSolutions = cloner.Clone(original.classificationSolutions);
83      RegisterClassificationSolutionsEventHandler();
84    }
85
86    public ClassificationEnsembleSolution()
87      : base(new ClassificationEnsembleModel(), ClassificationEnsembleProblemData.EmptyProblemData) {
88      classificationSolutions = new CheckedItemCollection<IClassificationSolution>();
89
90      weightCalculator = new MajorityVoteWeightCalculator();
91
92      RegisterClassificationSolutionsEventHandler();
93    }
94
95    public ClassificationEnsembleSolution(IEnumerable<IClassificationModel> models, IClassificationProblemData problemData)
96      : this(models, problemData,
97             models.Select(m => (IntRange)problemData.TrainingPartition.Clone()),
98             models.Select(m => (IntRange)problemData.TestPartition.Clone())
99      ) { }
100
101    public ClassificationEnsembleSolution(IEnumerable<IClassificationModel> models, IClassificationProblemData problemData, IEnumerable<IntRange> trainingPartitions, IEnumerable<IntRange> testPartitions)
102      : base(new ClassificationEnsembleModel(Enumerable.Empty<IClassificationModel>()), new ClassificationEnsembleProblemData(problemData)) {
103      this.classificationSolutions = new CheckedItemCollection<IClassificationSolution>();
104
105      List<IClassificationSolution> solutions = new List<IClassificationSolution>();
106      var modelEnumerator = models.GetEnumerator();
107      var trainingPartitionEnumerator = trainingPartitions.GetEnumerator();
108      var testPartitionEnumerator = testPartitions.GetEnumerator();
109
110      while (modelEnumerator.MoveNext() & trainingPartitionEnumerator.MoveNext() & testPartitionEnumerator.MoveNext()) {
111        var p = (IClassificationProblemData)problemData.Clone();
112        p.TrainingPartition.Start = trainingPartitionEnumerator.Current.Start;
113        p.TrainingPartition.End = trainingPartitionEnumerator.Current.End;
114        p.TestPartition.Start = testPartitionEnumerator.Current.Start;
115        p.TestPartition.End = testPartitionEnumerator.Current.End;
116
117        solutions.Add(modelEnumerator.Current.CreateClassificationSolution(p));
118      }
119      if (modelEnumerator.MoveNext() | trainingPartitionEnumerator.MoveNext() | testPartitionEnumerator.MoveNext()) {
120        throw new ArgumentException();
121      }
122
123      RegisterClassificationSolutionsEventHandler();
124      classificationSolutions.AddRange(solutions);
125    }
126
127    public override IDeepCloneable Clone(Cloner cloner) {
128      return new ClassificationEnsembleSolution(this, cloner);
129    }
130    private void RegisterClassificationSolutionsEventHandler() {
131      classificationSolutions.ItemsAdded += new CollectionItemsChangedEventHandler<IClassificationSolution>(classificationSolutions_ItemsAdded);
132      classificationSolutions.ItemsRemoved += new CollectionItemsChangedEventHandler<IClassificationSolution>(classificationSolutions_ItemsRemoved);
133      classificationSolutions.CollectionReset += new CollectionItemsChangedEventHandler<IClassificationSolution>(classificationSolutions_CollectionReset);
134      classificationSolutions.CheckedItemsChanged += new CollectionItemsChangedEventHandler<IClassificationSolution>(classificationSolutions_CheckedItemsChanged);
135    }
136
137    protected override void RecalculateResults() {
138      weightCalculator.CalculateNormalizedWeights(classificationSolutions.CheckedItems);
139      CalculateResults();
140    }
141
142    #region Evaluation
143    public override IEnumerable<double> EstimatedTrainingClassValues {
144      get {
145        return weightCalculator.AggregateEstimatedClassValues(classificationSolutions.CheckedItems,
146                                                              ProblemData.Dataset,
147                                                              ProblemData.TrainingIndizes,
148                                                              weightCalculator.GetTrainingClassDelegate());
149      }
150    }
151
152    public override IEnumerable<double> EstimatedTestClassValues {
153      get {
154        return weightCalculator.AggregateEstimatedClassValues(classificationSolutions.CheckedItems,
155                                                              ProblemData.Dataset,
156                                                              ProblemData.TestIndizes,
157                                                              weightCalculator.GetTestClassDelegate());
158      }
159    }
160
161    public override IEnumerable<double> GetEstimatedClassValues(IEnumerable<int> rows) {
162      return weightCalculator.AggregateEstimatedClassValues(classificationSolutions.CheckedItems,
163                                                            ProblemData.Dataset,
164                                                            rows,
165                                                            weightCalculator.GetAllClassDelegate());
166    }
167
168    public IEnumerable<IEnumerable<double>> GetEstimatedClassValueVectors(Dataset dataset, IEnumerable<int> rows) {
169      IEnumerable<IClassificationModel> models = classificationSolutions.CheckedItems.Select(sol => sol.Model);
170      if (!models.Any()) yield break;
171      var estimatedValuesEnumerators = (from model in models
172                                        select model.GetEstimatedClassValues(dataset, rows).GetEnumerator())
173                                       .ToList();
174
175      while (estimatedValuesEnumerators.All(en => en.MoveNext())) {
176        yield return from enumerator in estimatedValuesEnumerators
177                     select enumerator.Current;
178      }
179    }
180    #endregion
181
182    protected override void OnProblemDataChanged() {
183      IClassificationProblemData problemData = new ClassificationProblemData(ProblemData.Dataset,
184                                                                     ProblemData.AllowedInputVariables,
185                                                                     ProblemData.TargetVariable);
186      problemData.TrainingPartition.Start = ProblemData.TrainingPartition.Start;
187      problemData.TrainingPartition.End = ProblemData.TrainingPartition.End;
188      problemData.TestPartition.Start = ProblemData.TestPartition.Start;
189      problemData.TestPartition.End = ProblemData.TestPartition.End;
190
191      foreach (var solution in ClassificationSolutions) {
192        if (solution is ClassificationEnsembleSolution)
193          solution.ProblemData = ProblemData;
194        else
195          solution.ProblemData = problemData;
196      }
197      base.OnProblemDataChanged();
198    }
199
200    public void AddClassificationSolutions(IEnumerable<IClassificationSolution> solutions) {
201      classificationSolutions.AddRange(solutions);
202    }
203    public void RemoveClassificationSolutions(IEnumerable<IClassificationSolution> solutions) {
204      classificationSolutions.RemoveRange(solutions);
205    }
206
207    private void classificationSolutions_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IClassificationSolution> e) {
208      foreach (var solution in e.Items) AddClassificationSolution(solution);
209      RecalculateResults();
210    }
211    private void classificationSolutions_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IClassificationSolution> e) {
212      foreach (var solution in e.Items) RemoveClassificationSolution(solution);
213      RecalculateResults();
214    }
215    private void classificationSolutions_CollectionReset(object sender, CollectionItemsChangedEventArgs<IClassificationSolution> e) {
216      foreach (var solution in e.OldItems) RemoveClassificationSolution(solution);
217      foreach (var solution in e.Items) AddClassificationSolution(solution);
218      RecalculateResults();
219    }
220    private void classificationSolutions_CheckedItemsChanged(object sender, CollectionItemsChangedEventArgs<IClassificationSolution> e) {
221      RecalculateResults();
222    }
223
224    private void AddClassificationSolution(IClassificationSolution solution) {
225      if (Model.Models.Contains(solution.Model)) throw new ArgumentException();
226      Model.Add(solution.Model);
227    }
228
229    private void RemoveClassificationSolution(IClassificationSolution solution) {
230      if (!Model.Models.Contains(solution.Model)) throw new ArgumentException();
231      Model.Remove(solution.Model);
232    }
233  }
234}
Note: See TracBrowser for help on using the repository browser.