Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceReintegration/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Classification/ClassificationEnsembleModel.cs @ 15018

Last change on this file since 15018 was 15018, checked in by gkronber, 7 years ago

#2520 introduced StorableConstructorFlag type for StorableConstructors

File size: 4.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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.Collections.Generic;
23using System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Persistence;
27
28namespace HeuristicLab.Problems.DataAnalysis {
29  /// <summary>
30  /// Represents classification solutions that contain an ensemble of multiple classification models
31  /// </summary>
32  [StorableType("6761ed28-bdc7-4294-beb7-077133ad5cbc")]
33  [Item("ClassificationEnsembleModel", "A classification model that contains an ensemble of multiple classification models")]
34  public class ClassificationEnsembleModel : ClassificationModel, IClassificationEnsembleModel {
35    public override IEnumerable<string> VariablesUsedForPrediction {
36      get { return models.SelectMany(x => x.VariablesUsedForPrediction).Distinct().OrderBy(x => x); }
37    }
38
39    [Storable]
40    private List<IClassificationModel> models;
41    public IEnumerable<IClassificationModel> Models {
42      get { return new List<IClassificationModel>(models); }
43    }
44
45    [StorableConstructor]
46    protected ClassificationEnsembleModel(StorableConstructorFlag deserializing) : base(deserializing) { }
47    protected ClassificationEnsembleModel(ClassificationEnsembleModel original, Cloner cloner)
48      : base(original, cloner) {
49      this.models = original.Models.Select(m => cloner.Clone(m)).ToList();
50    }
51
52    public ClassificationEnsembleModel() : this(Enumerable.Empty<IClassificationModel>()) { }
53    public ClassificationEnsembleModel(IEnumerable<IClassificationModel> models)
54      : base(string.Empty) {
55      this.name = ItemName;
56      this.description = ItemDescription;
57      this.models = new List<IClassificationModel>(models);
58
59      if (this.models.Any()) this.TargetVariable = this.models.First().TargetVariable;
60    }
61
62    public override IDeepCloneable Clone(Cloner cloner) {
63      return new ClassificationEnsembleModel(this, cloner);
64    }
65
66    public void Add(IClassificationModel model) {
67      if (string.IsNullOrEmpty(TargetVariable)) TargetVariable = model.TargetVariable;
68      models.Add(model);
69    }
70    public void Remove(IClassificationModel model) {
71      models.Remove(model);
72      if (!models.Any()) TargetVariable = string.Empty;
73    }
74
75    public IEnumerable<IEnumerable<double>> GetEstimatedClassValueVectors(IDataset dataset, IEnumerable<int> rows) {
76      var estimatedValuesEnumerators = (from model in models
77                                        select model.GetEstimatedClassValues(dataset, rows).GetEnumerator())
78                                       .ToList();
79
80      while (estimatedValuesEnumerators.All(en => en.MoveNext())) {
81        yield return from enumerator in estimatedValuesEnumerators
82                     select enumerator.Current;
83      }
84    }
85
86
87    public override IEnumerable<double> GetEstimatedClassValues(IDataset dataset, IEnumerable<int> rows) {
88      foreach (var estimatedValuesVector in GetEstimatedClassValueVectors(dataset, rows)) {
89        // return the class which is most often occuring
90        yield return
91          estimatedValuesVector
92          .GroupBy(x => x)
93          .OrderBy(g => -g.Count())
94          .Select(g => g.Key)
95          .First();
96      }
97    }
98
99    public override IClassificationSolution CreateClassificationSolution(IClassificationProblemData problemData) {
100      return new ClassificationEnsembleSolution(models, new ClassificationEnsembleProblemData(problemData));
101    }
102
103
104  }
105}
Note: See TracBrowser for help on using the repository browser.