Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Classification/ClassificationEnsembleModel.cs @ 13921

Last change on this file since 13921 was 13921, checked in by bburlacu, 8 years ago

#2604: Revert changes to DataAnalysisSolution and IDataAnalysisSolution and implement the desired properties in model classes that implement IDataAnalysisModel, IRegressionModel and IClassificationModel.

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