1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2011 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.Collections.Generic;
|
---|
23 | using System.Linq;
|
---|
24 | using HeuristicLab.Common;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
27 | using HeuristicLab.Data;
|
---|
28 | using System;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.Problems.DataAnalysis {
|
---|
31 | /// <summary>
|
---|
32 | /// Represents classification solutions that contain an ensemble of multiple classification models
|
---|
33 | /// </summary>
|
---|
34 | [StorableClass]
|
---|
35 | [Item("Classification Ensemble Solution", "A classification solution that contains an ensemble of multiple classification models")]
|
---|
36 | // [Creatable("Data Analysis")]
|
---|
37 | public class ClassificationEnsembleSolution : ClassificationSolution, IClassificationEnsembleSolution {
|
---|
38 |
|
---|
39 | public new IClassificationEnsembleModel Model {
|
---|
40 | set { base.Model = value; }
|
---|
41 | get { return (IClassificationEnsembleModel)base.Model; }
|
---|
42 | }
|
---|
43 |
|
---|
44 | [Storable]
|
---|
45 | private Dictionary<IClassificationModel, IntRange> trainingPartitions;
|
---|
46 | [Storable]
|
---|
47 | private Dictionary<IClassificationModel, IntRange> testPartitions;
|
---|
48 |
|
---|
49 |
|
---|
50 | [StorableConstructor]
|
---|
51 | protected ClassificationEnsembleSolution(bool deserializing) : base(deserializing) { }
|
---|
52 | protected ClassificationEnsembleSolution(ClassificationEnsembleSolution original, Cloner cloner)
|
---|
53 | : base(original, cloner) {
|
---|
54 | trainingPartitions = new Dictionary<IClassificationModel, IntRange>();
|
---|
55 | testPartitions = new Dictionary<IClassificationModel, IntRange>();
|
---|
56 | foreach (var pair in original.trainingPartitions) {
|
---|
57 | trainingPartitions[cloner.Clone(pair.Key)] = cloner.Clone(pair.Value);
|
---|
58 | }
|
---|
59 | foreach (var pair in original.testPartitions) {
|
---|
60 | testPartitions[cloner.Clone(pair.Key)] = cloner.Clone(pair.Value);
|
---|
61 | }
|
---|
62 | RecalculateResults();
|
---|
63 | }
|
---|
64 | public ClassificationEnsembleSolution(IEnumerable<IClassificationModel> models, IClassificationProblemData problemData)
|
---|
65 | : base(new ClassificationEnsembleModel(models), new ClassificationEnsembleProblemData(problemData)) {
|
---|
66 | this.name = ItemName;
|
---|
67 | this.description = ItemDescription;
|
---|
68 | trainingPartitions = new Dictionary<IClassificationModel, IntRange>();
|
---|
69 | testPartitions = new Dictionary<IClassificationModel, IntRange>();
|
---|
70 | foreach (var model in models) {
|
---|
71 | trainingPartitions[model] = (IntRange)problemData.TrainingPartition.Clone();
|
---|
72 | testPartitions[model] = (IntRange)problemData.TestPartition.Clone();
|
---|
73 | }
|
---|
74 | RecalculateResults();
|
---|
75 | }
|
---|
76 |
|
---|
77 | public ClassificationEnsembleSolution(IEnumerable<IClassificationModel> models, IClassificationProblemData problemData, IEnumerable<IntRange> trainingPartitions, IEnumerable<IntRange> testPartitions)
|
---|
78 | : base(new ClassificationEnsembleModel(models), new ClassificationEnsembleProblemData(problemData)) {
|
---|
79 | this.trainingPartitions = new Dictionary<IClassificationModel, IntRange>();
|
---|
80 | this.testPartitions = new Dictionary<IClassificationModel, IntRange>();
|
---|
81 | AddModelsAndParitions(models,
|
---|
82 | trainingPartitions,
|
---|
83 | testPartitions);
|
---|
84 | RecalculateResults();
|
---|
85 | }
|
---|
86 |
|
---|
87 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
88 | return new ClassificationEnsembleSolution(this, cloner);
|
---|
89 | }
|
---|
90 |
|
---|
91 | public override IEnumerable<double> EstimatedTrainingClassValues {
|
---|
92 | get {
|
---|
93 | var rows = ProblemData.TrainingIndizes;
|
---|
94 | var estimatedValuesEnumerators = (from model in Model.Models
|
---|
95 | select new { Model = model, EstimatedValuesEnumerator = model.GetEstimatedClassValues(ProblemData.Dataset, rows).GetEnumerator() })
|
---|
96 | .ToList();
|
---|
97 | var rowsEnumerator = rows.GetEnumerator();
|
---|
98 | // aggregate to make sure that MoveNext is called for all enumerators
|
---|
99 | while (rowsEnumerator.MoveNext() & estimatedValuesEnumerators.Select(en => en.EstimatedValuesEnumerator.MoveNext()).Aggregate(true, (acc, b) => acc & b)) {
|
---|
100 | int currentRow = rowsEnumerator.Current;
|
---|
101 |
|
---|
102 | var selectedEnumerators = from pair in estimatedValuesEnumerators
|
---|
103 | where RowIsTrainingForModel(currentRow, pair.Model) && !RowIsTestForModel(currentRow, pair.Model)
|
---|
104 | select pair.EstimatedValuesEnumerator;
|
---|
105 | yield return AggregateEstimatedClassValues(selectedEnumerators.Select(x => x.Current));
|
---|
106 | }
|
---|
107 | }
|
---|
108 | }
|
---|
109 |
|
---|
110 | public override IEnumerable<double> EstimatedTestClassValues {
|
---|
111 | get {
|
---|
112 | var rows = ProblemData.TestIndizes;
|
---|
113 | var estimatedValuesEnumerators = (from model in Model.Models
|
---|
114 | select new { Model = model, EstimatedValuesEnumerator = model.GetEstimatedClassValues(ProblemData.Dataset, rows).GetEnumerator() })
|
---|
115 | .ToList();
|
---|
116 | var rowsEnumerator = ProblemData.TestIndizes.GetEnumerator();
|
---|
117 | // aggregate to make sure that MoveNext is called for all enumerators
|
---|
118 | while (rowsEnumerator.MoveNext() & estimatedValuesEnumerators.Select(en => en.EstimatedValuesEnumerator.MoveNext()).Aggregate(true, (acc, b) => acc & b)) {
|
---|
119 | int currentRow = rowsEnumerator.Current;
|
---|
120 |
|
---|
121 | var selectedEnumerators = from pair in estimatedValuesEnumerators
|
---|
122 | where RowIsTestForModel(currentRow, pair.Model)
|
---|
123 | select pair.EstimatedValuesEnumerator;
|
---|
124 |
|
---|
125 | yield return AggregateEstimatedClassValues(selectedEnumerators.Select(x => x.Current));
|
---|
126 | }
|
---|
127 | }
|
---|
128 | }
|
---|
129 |
|
---|
130 | private bool RowIsTrainingForModel(int currentRow, IClassificationModel model) {
|
---|
131 | return trainingPartitions == null || !trainingPartitions.ContainsKey(model) ||
|
---|
132 | (trainingPartitions[model].Start <= currentRow && currentRow < trainingPartitions[model].End);
|
---|
133 | }
|
---|
134 |
|
---|
135 | private bool RowIsTestForModel(int currentRow, IClassificationModel model) {
|
---|
136 | return testPartitions == null || !testPartitions.ContainsKey(model) ||
|
---|
137 | (testPartitions[model].Start <= currentRow && currentRow < testPartitions[model].End);
|
---|
138 | }
|
---|
139 |
|
---|
140 | public override IEnumerable<double> GetEstimatedClassValues(IEnumerable<int> rows) {
|
---|
141 | return from xs in GetEstimatedClassValueVectors(ProblemData.Dataset, rows)
|
---|
142 | select AggregateEstimatedClassValues(xs);
|
---|
143 | }
|
---|
144 |
|
---|
145 | public IEnumerable<IEnumerable<double>> GetEstimatedClassValueVectors(Dataset dataset, IEnumerable<int> rows) {
|
---|
146 | var estimatedValuesEnumerators = (from model in Model.Models
|
---|
147 | select model.GetEstimatedClassValues(dataset, rows).GetEnumerator())
|
---|
148 | .ToList();
|
---|
149 |
|
---|
150 | while (estimatedValuesEnumerators.All(en => en.MoveNext())) {
|
---|
151 | yield return from enumerator in estimatedValuesEnumerators
|
---|
152 | select enumerator.Current;
|
---|
153 | }
|
---|
154 | }
|
---|
155 |
|
---|
156 | private double AggregateEstimatedClassValues(IEnumerable<double> estimatedClassValues) {
|
---|
157 | return estimatedClassValues
|
---|
158 | .GroupBy(x => x)
|
---|
159 | .OrderBy(g => -g.Count())
|
---|
160 | .Select(g => g.Key)
|
---|
161 | .DefaultIfEmpty(double.NaN)
|
---|
162 | .First();
|
---|
163 | }
|
---|
164 |
|
---|
165 | public void AddModelsAndPartitions(IEnumerable<IClassificationSolution> solutions) {
|
---|
166 | foreach (var solution in solutions) {
|
---|
167 | var ensembleSolution = solution as ClassificationEnsembleSolution;
|
---|
168 | if (ensembleSolution != null) {
|
---|
169 | var data = from m in ensembleSolution.Model.Models
|
---|
170 | let train = ensembleSolution.trainingPartitions[m]
|
---|
171 | let test = ensembleSolution.testPartitions[m]
|
---|
172 | select new { m, train, test };
|
---|
173 |
|
---|
174 | foreach (var d in data) {
|
---|
175 | Model.Add(d.m);
|
---|
176 | trainingPartitions[d.m] = (IntRange)d.train.Clone();
|
---|
177 | testPartitions[d.m] = (IntRange)d.test.Clone();
|
---|
178 | }
|
---|
179 | } else {
|
---|
180 | Model.Add(solution.Model);
|
---|
181 | trainingPartitions[solution.Model] = (IntRange)solution.ProblemData.TrainingPartition.Clone();
|
---|
182 | testPartitions[solution.Model] = (IntRange)solution.ProblemData.TestPartition.Clone();
|
---|
183 | }
|
---|
184 | }
|
---|
185 |
|
---|
186 | RecalculateResults();
|
---|
187 | }
|
---|
188 |
|
---|
189 | private void AddModelsAndParitions(IEnumerable<IClassificationModel> models, IEnumerable<IntRange> trainingPartitions, IEnumerable<IntRange> testPartitions) {
|
---|
190 | var modelEnumerator = models.GetEnumerator();
|
---|
191 | var trainingPartitionEnumerator = trainingPartitions.GetEnumerator();
|
---|
192 | var testPartitionEnumerator = testPartitions.GetEnumerator();
|
---|
193 |
|
---|
194 | while (modelEnumerator.MoveNext() & trainingPartitionEnumerator.MoveNext() & testPartitionEnumerator.MoveNext()) {
|
---|
195 | this.trainingPartitions[modelEnumerator.Current] = (IntRange)trainingPartitionEnumerator.Current.Clone();
|
---|
196 | this.testPartitions[modelEnumerator.Current] = (IntRange)testPartitionEnumerator.Current.Clone();
|
---|
197 | }
|
---|
198 | if (modelEnumerator.MoveNext() | trainingPartitionEnumerator.MoveNext() | testPartitionEnumerator.MoveNext()) {
|
---|
199 | throw new ArgumentException();
|
---|
200 | }
|
---|
201 | }
|
---|
202 | }
|
---|
203 | }
|
---|