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 model in Model.Models) {
|
---|
57 | trainingPartitions[model] = (IntRange)ProblemData.TrainingPartition.Clone();
|
---|
58 | testPartitions[model] = (IntRange)ProblemData.TestPartition.Clone();
|
---|
59 | }
|
---|
60 | RecalculateResults();
|
---|
61 | }
|
---|
62 | public ClassificationEnsembleSolution(IEnumerable<IClassificationModel> models, IClassificationProblemData problemData)
|
---|
63 | : base(new ClassificationEnsembleModel(models), new ClassificationEnsembleProblemData(problemData)) {
|
---|
64 | this.name = ItemName;
|
---|
65 | this.description = ItemDescription;
|
---|
66 | trainingPartitions = new Dictionary<IClassificationModel, IntRange>();
|
---|
67 | testPartitions = new Dictionary<IClassificationModel, IntRange>();
|
---|
68 | foreach (var model in models) {
|
---|
69 | trainingPartitions[model] = (IntRange)problemData.TrainingPartition.Clone();
|
---|
70 | testPartitions[model] = (IntRange)problemData.TestPartition.Clone();
|
---|
71 | }
|
---|
72 | RecalculateResults();
|
---|
73 | }
|
---|
74 |
|
---|
75 | public ClassificationEnsembleSolution(IEnumerable<IClassificationModel> models, IClassificationProblemData problemData, IEnumerable<IntRange> trainingPartitions, IEnumerable<IntRange> testPartitions)
|
---|
76 | : base(new ClassificationEnsembleModel(models), new ClassificationEnsembleProblemData(problemData)) {
|
---|
77 | this.trainingPartitions = new Dictionary<IClassificationModel, IntRange>();
|
---|
78 | this.testPartitions = new Dictionary<IClassificationModel, IntRange>();
|
---|
79 | var modelEnumerator = models.GetEnumerator();
|
---|
80 | var trainingPartitionEnumerator = trainingPartitions.GetEnumerator();
|
---|
81 | var testPartitionEnumerator = testPartitions.GetEnumerator();
|
---|
82 | while (modelEnumerator.MoveNext() & trainingPartitionEnumerator.MoveNext() & testPartitionEnumerator.MoveNext()) {
|
---|
83 | this.trainingPartitions[modelEnumerator.Current] = (IntRange)trainingPartitionEnumerator.Current.Clone();
|
---|
84 | this.testPartitions[modelEnumerator.Current] = (IntRange)testPartitionEnumerator.Current.Clone();
|
---|
85 | }
|
---|
86 | if (modelEnumerator.MoveNext() | trainingPartitionEnumerator.MoveNext() | testPartitionEnumerator.MoveNext()) {
|
---|
87 | throw new ArgumentException();
|
---|
88 | }
|
---|
89 | RecalculateResults();
|
---|
90 | }
|
---|
91 |
|
---|
92 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
93 | return new ClassificationEnsembleSolution(this, cloner);
|
---|
94 | }
|
---|
95 |
|
---|
96 | public override IEnumerable<double> EstimatedTrainingClassValues {
|
---|
97 | get {
|
---|
98 | var rows = ProblemData.TrainingIndizes;
|
---|
99 | var estimatedValuesEnumerators = (from model in Model.Models
|
---|
100 | select new { Model = model, EstimatedValuesEnumerator = model.GetEstimatedClassValues(ProblemData.Dataset, rows).GetEnumerator() })
|
---|
101 | .ToList();
|
---|
102 | var rowsEnumerator = rows.GetEnumerator();
|
---|
103 | // aggregate to make sure that MoveNext is called for all enumerators
|
---|
104 | while (rowsEnumerator.MoveNext() & estimatedValuesEnumerators.Select(en => en.EstimatedValuesEnumerator.MoveNext()).Aggregate(true, (acc, b) => acc & b)) {
|
---|
105 | int currentRow = rowsEnumerator.Current;
|
---|
106 |
|
---|
107 | var selectedEnumerators = from pair in estimatedValuesEnumerators
|
---|
108 | where trainingPartitions == null || !trainingPartitions.ContainsKey(pair.Model) ||
|
---|
109 | (trainingPartitions[pair.Model].Start <= currentRow && currentRow < trainingPartitions[pair.Model].End)
|
---|
110 | select pair.EstimatedValuesEnumerator;
|
---|
111 | yield return AggregateEstimatedClassValues(selectedEnumerators.Select(x => x.Current));
|
---|
112 | }
|
---|
113 | }
|
---|
114 | }
|
---|
115 |
|
---|
116 | public override IEnumerable<double> EstimatedTestClassValues {
|
---|
117 | get {
|
---|
118 | var rows = ProblemData.TestIndizes;
|
---|
119 | var estimatedValuesEnumerators = (from model in Model.Models
|
---|
120 | select new { Model = model, EstimatedValuesEnumerator = model.GetEstimatedClassValues(ProblemData.Dataset, rows).GetEnumerator() })
|
---|
121 | .ToList();
|
---|
122 | var rowsEnumerator = ProblemData.TestIndizes.GetEnumerator();
|
---|
123 | // aggregate to make sure that MoveNext is called for all enumerators
|
---|
124 | while (rowsEnumerator.MoveNext() & estimatedValuesEnumerators.Select(en => en.EstimatedValuesEnumerator.MoveNext()).Aggregate(true, (acc, b) => acc & b)) {
|
---|
125 | int currentRow = rowsEnumerator.Current;
|
---|
126 |
|
---|
127 | var selectedEnumerators = from pair in estimatedValuesEnumerators
|
---|
128 | where testPartitions == null || !testPartitions.ContainsKey(pair.Model) ||
|
---|
129 | (testPartitions[pair.Model].Start <= currentRow && currentRow < testPartitions[pair.Model].End)
|
---|
130 | select pair.EstimatedValuesEnumerator;
|
---|
131 |
|
---|
132 | yield return AggregateEstimatedClassValues(selectedEnumerators.Select(x => x.Current));
|
---|
133 | }
|
---|
134 | }
|
---|
135 | }
|
---|
136 |
|
---|
137 | public override IEnumerable<double> GetEstimatedClassValues(IEnumerable<int> rows) {
|
---|
138 | return from xs in GetEstimatedClassValueVectors(ProblemData.Dataset, rows)
|
---|
139 | select AggregateEstimatedClassValues(xs);
|
---|
140 | }
|
---|
141 |
|
---|
142 | public IEnumerable<IEnumerable<double>> GetEstimatedClassValueVectors(Dataset dataset, IEnumerable<int> rows) {
|
---|
143 | var estimatedValuesEnumerators = (from model in Model.Models
|
---|
144 | select model.GetEstimatedClassValues(dataset, rows).GetEnumerator())
|
---|
145 | .ToList();
|
---|
146 |
|
---|
147 | while (estimatedValuesEnumerators.All(en => en.MoveNext())) {
|
---|
148 | yield return from enumerator in estimatedValuesEnumerators
|
---|
149 | select enumerator.Current;
|
---|
150 | }
|
---|
151 | }
|
---|
152 |
|
---|
153 | private double AggregateEstimatedClassValues(IEnumerable<double> estimatedClassValues) {
|
---|
154 | return estimatedClassValues
|
---|
155 | .GroupBy(x => x)
|
---|
156 | .OrderBy(g => -g.Count())
|
---|
157 | .Select(g => g.Key)
|
---|
158 | .First();
|
---|
159 | }
|
---|
160 | }
|
---|
161 | }
|
---|