[5816] | 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 |
|
---|
[6589] | 22 | using System;
|
---|
[5816] | 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
[6613] | 25 | using HeuristicLab.Collections;
|
---|
[5816] | 26 | using HeuristicLab.Common;
|
---|
| 27 | using HeuristicLab.Core;
|
---|
[6589] | 28 | using HeuristicLab.Data;
|
---|
[5816] | 29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 30 |
|
---|
| 31 | namespace HeuristicLab.Problems.DataAnalysis {
|
---|
| 32 | /// <summary>
|
---|
| 33 | /// Represents classification solutions that contain an ensemble of multiple classification models
|
---|
| 34 | /// </summary>
|
---|
| 35 | [StorableClass]
|
---|
| 36 | [Item("Classification Ensemble Solution", "A classification solution that contains an ensemble of multiple classification models")]
|
---|
[6666] | 37 | [Creatable("Data Analysis - Ensembles")]
|
---|
[6592] | 38 | public sealed class ClassificationEnsembleSolution : ClassificationSolution, IClassificationEnsembleSolution {
|
---|
[6239] | 39 | public new IClassificationEnsembleModel Model {
|
---|
| 40 | get { return (IClassificationEnsembleModel)base.Model; }
|
---|
| 41 | }
|
---|
[6666] | 42 | public new ClassificationEnsembleProblemData ProblemData {
|
---|
| 43 | get { return (ClassificationEnsembleProblemData)base.ProblemData; }
|
---|
| 44 | set { base.ProblemData = value; }
|
---|
| 45 | }
|
---|
[6239] | 46 |
|
---|
[6613] | 47 | private readonly ItemCollection<IClassificationSolution> classificationSolutions;
|
---|
| 48 | public IItemCollection<IClassificationSolution> ClassificationSolutions {
|
---|
| 49 | get { return classificationSolutions; }
|
---|
| 50 | }
|
---|
| 51 |
|
---|
[5816] | 52 | [Storable]
|
---|
[6239] | 53 | private Dictionary<IClassificationModel, IntRange> trainingPartitions;
|
---|
| 54 | [Storable]
|
---|
| 55 | private Dictionary<IClassificationModel, IntRange> testPartitions;
|
---|
| 56 |
|
---|
[6613] | 57 | [StorableConstructor]
|
---|
| 58 | private ClassificationEnsembleSolution(bool deserializing)
|
---|
| 59 | : base(deserializing) {
|
---|
| 60 | classificationSolutions = new ItemCollection<IClassificationSolution>();
|
---|
| 61 | }
|
---|
| 62 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 63 | private void AfterDeserialization() {
|
---|
| 64 | foreach (var model in Model.Models) {
|
---|
| 65 | IClassificationProblemData problemData = (IClassificationProblemData)ProblemData.Clone();
|
---|
| 66 | problemData.TrainingPartition.Start = trainingPartitions[model].Start;
|
---|
| 67 | problemData.TrainingPartition.End = trainingPartitions[model].End;
|
---|
| 68 | problemData.TestPartition.Start = testPartitions[model].Start;
|
---|
| 69 | problemData.TestPartition.End = testPartitions[model].End;
|
---|
[6239] | 70 |
|
---|
[6613] | 71 | classificationSolutions.Add(model.CreateClassificationSolution(problemData));
|
---|
| 72 | }
|
---|
| 73 | RegisterClassificationSolutionsEventHandler();
|
---|
| 74 | }
|
---|
| 75 |
|
---|
[6592] | 76 | private ClassificationEnsembleSolution(ClassificationEnsembleSolution original, Cloner cloner)
|
---|
[5816] | 77 | : base(original, cloner) {
|
---|
[6239] | 78 | trainingPartitions = new Dictionary<IClassificationModel, IntRange>();
|
---|
| 79 | testPartitions = new Dictionary<IClassificationModel, IntRange>();
|
---|
[6302] | 80 | foreach (var pair in original.trainingPartitions) {
|
---|
| 81 | trainingPartitions[cloner.Clone(pair.Key)] = cloner.Clone(pair.Value);
|
---|
[6239] | 82 | }
|
---|
[6302] | 83 | foreach (var pair in original.testPartitions) {
|
---|
| 84 | testPartitions[cloner.Clone(pair.Key)] = cloner.Clone(pair.Value);
|
---|
| 85 | }
|
---|
[6613] | 86 |
|
---|
| 87 | classificationSolutions = cloner.Clone(original.classificationSolutions);
|
---|
| 88 | RegisterClassificationSolutionsEventHandler();
|
---|
[5816] | 89 | }
|
---|
[6613] | 90 |
|
---|
[6666] | 91 | public ClassificationEnsembleSolution()
|
---|
| 92 | : base(new ClassificationEnsembleModel(), ClassificationEnsembleProblemData.EmptyProblemData) {
|
---|
| 93 | trainingPartitions = new Dictionary<IClassificationModel, IntRange>();
|
---|
| 94 | testPartitions = new Dictionary<IClassificationModel, IntRange>();
|
---|
| 95 | classificationSolutions = new ItemCollection<IClassificationSolution>();
|
---|
| 96 |
|
---|
| 97 | RegisterClassificationSolutionsEventHandler();
|
---|
| 98 | }
|
---|
| 99 |
|
---|
[6239] | 100 | public ClassificationEnsembleSolution(IEnumerable<IClassificationModel> models, IClassificationProblemData problemData)
|
---|
[6613] | 101 | : this(models, problemData,
|
---|
| 102 | models.Select(m => (IntRange)problemData.TrainingPartition.Clone()),
|
---|
| 103 | models.Select(m => (IntRange)problemData.TestPartition.Clone())
|
---|
| 104 | ) { }
|
---|
[5816] | 105 |
|
---|
[6239] | 106 | public ClassificationEnsembleSolution(IEnumerable<IClassificationModel> models, IClassificationProblemData problemData, IEnumerable<IntRange> trainingPartitions, IEnumerable<IntRange> testPartitions)
|
---|
[6613] | 107 | : base(new ClassificationEnsembleModel(Enumerable.Empty<IClassificationModel>()), new ClassificationEnsembleProblemData(problemData)) {
|
---|
[6239] | 108 | this.trainingPartitions = new Dictionary<IClassificationModel, IntRange>();
|
---|
| 109 | this.testPartitions = new Dictionary<IClassificationModel, IntRange>();
|
---|
[6613] | 110 | this.classificationSolutions = new ItemCollection<IClassificationSolution>();
|
---|
| 111 |
|
---|
| 112 | List<IClassificationSolution> solutions = new List<IClassificationSolution>();
|
---|
| 113 | var modelEnumerator = models.GetEnumerator();
|
---|
| 114 | var trainingPartitionEnumerator = trainingPartitions.GetEnumerator();
|
---|
| 115 | var testPartitionEnumerator = testPartitions.GetEnumerator();
|
---|
| 116 |
|
---|
| 117 | while (modelEnumerator.MoveNext() & trainingPartitionEnumerator.MoveNext() & testPartitionEnumerator.MoveNext()) {
|
---|
| 118 | var p = (IClassificationProblemData)problemData.Clone();
|
---|
| 119 | p.TrainingPartition.Start = trainingPartitionEnumerator.Current.Start;
|
---|
| 120 | p.TrainingPartition.End = trainingPartitionEnumerator.Current.End;
|
---|
| 121 | p.TestPartition.Start = testPartitionEnumerator.Current.Start;
|
---|
| 122 | p.TestPartition.End = testPartitionEnumerator.Current.End;
|
---|
| 123 |
|
---|
| 124 | solutions.Add(modelEnumerator.Current.CreateClassificationSolution(p));
|
---|
| 125 | }
|
---|
| 126 | if (modelEnumerator.MoveNext() | trainingPartitionEnumerator.MoveNext() | testPartitionEnumerator.MoveNext()) {
|
---|
| 127 | throw new ArgumentException();
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | RegisterClassificationSolutionsEventHandler();
|
---|
| 131 | classificationSolutions.AddRange(solutions);
|
---|
[6239] | 132 | }
|
---|
| 133 |
|
---|
[5816] | 134 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 135 | return new ClassificationEnsembleSolution(this, cloner);
|
---|
| 136 | }
|
---|
[6613] | 137 | private void RegisterClassificationSolutionsEventHandler() {
|
---|
| 138 | classificationSolutions.ItemsAdded += new CollectionItemsChangedEventHandler<IClassificationSolution>(classificationSolutions_ItemsAdded);
|
---|
| 139 | classificationSolutions.ItemsRemoved += new CollectionItemsChangedEventHandler<IClassificationSolution>(classificationSolutions_ItemsRemoved);
|
---|
| 140 | classificationSolutions.CollectionReset += new CollectionItemsChangedEventHandler<IClassificationSolution>(classificationSolutions_CollectionReset);
|
---|
| 141 | }
|
---|
[5816] | 142 |
|
---|
[6589] | 143 | protected override void RecalculateResults() {
|
---|
| 144 | CalculateResults();
|
---|
| 145 | }
|
---|
| 146 |
|
---|
[6613] | 147 | #region Evaluation
|
---|
[6239] | 148 | public override IEnumerable<double> EstimatedTrainingClassValues {
|
---|
| 149 | get {
|
---|
| 150 | var rows = ProblemData.TrainingIndizes;
|
---|
| 151 | var estimatedValuesEnumerators = (from model in Model.Models
|
---|
| 152 | select new { Model = model, EstimatedValuesEnumerator = model.GetEstimatedClassValues(ProblemData.Dataset, rows).GetEnumerator() })
|
---|
| 153 | .ToList();
|
---|
| 154 | var rowsEnumerator = rows.GetEnumerator();
|
---|
| 155 | // aggregate to make sure that MoveNext is called for all enumerators
|
---|
| 156 | while (rowsEnumerator.MoveNext() & estimatedValuesEnumerators.Select(en => en.EstimatedValuesEnumerator.MoveNext()).Aggregate(true, (acc, b) => acc & b)) {
|
---|
| 157 | int currentRow = rowsEnumerator.Current;
|
---|
[5816] | 158 |
|
---|
[6239] | 159 | var selectedEnumerators = from pair in estimatedValuesEnumerators
|
---|
[6254] | 160 | where RowIsTrainingForModel(currentRow, pair.Model) && !RowIsTestForModel(currentRow, pair.Model)
|
---|
[6239] | 161 | select pair.EstimatedValuesEnumerator;
|
---|
| 162 | yield return AggregateEstimatedClassValues(selectedEnumerators.Select(x => x.Current));
|
---|
| 163 | }
|
---|
| 164 | }
|
---|
| 165 | }
|
---|
| 166 |
|
---|
| 167 | public override IEnumerable<double> EstimatedTestClassValues {
|
---|
| 168 | get {
|
---|
| 169 | var rows = ProblemData.TestIndizes;
|
---|
| 170 | var estimatedValuesEnumerators = (from model in Model.Models
|
---|
| 171 | select new { Model = model, EstimatedValuesEnumerator = model.GetEstimatedClassValues(ProblemData.Dataset, rows).GetEnumerator() })
|
---|
| 172 | .ToList();
|
---|
| 173 | var rowsEnumerator = ProblemData.TestIndizes.GetEnumerator();
|
---|
| 174 | // aggregate to make sure that MoveNext is called for all enumerators
|
---|
| 175 | while (rowsEnumerator.MoveNext() & estimatedValuesEnumerators.Select(en => en.EstimatedValuesEnumerator.MoveNext()).Aggregate(true, (acc, b) => acc & b)) {
|
---|
| 176 | int currentRow = rowsEnumerator.Current;
|
---|
| 177 |
|
---|
| 178 | var selectedEnumerators = from pair in estimatedValuesEnumerators
|
---|
[6254] | 179 | where RowIsTestForModel(currentRow, pair.Model)
|
---|
[6239] | 180 | select pair.EstimatedValuesEnumerator;
|
---|
| 181 |
|
---|
| 182 | yield return AggregateEstimatedClassValues(selectedEnumerators.Select(x => x.Current));
|
---|
| 183 | }
|
---|
| 184 | }
|
---|
| 185 | }
|
---|
| 186 |
|
---|
[6254] | 187 | private bool RowIsTrainingForModel(int currentRow, IClassificationModel model) {
|
---|
| 188 | return trainingPartitions == null || !trainingPartitions.ContainsKey(model) ||
|
---|
| 189 | (trainingPartitions[model].Start <= currentRow && currentRow < trainingPartitions[model].End);
|
---|
| 190 | }
|
---|
| 191 |
|
---|
| 192 | private bool RowIsTestForModel(int currentRow, IClassificationModel model) {
|
---|
| 193 | return testPartitions == null || !testPartitions.ContainsKey(model) ||
|
---|
| 194 | (testPartitions[model].Start <= currentRow && currentRow < testPartitions[model].End);
|
---|
| 195 | }
|
---|
| 196 |
|
---|
[6239] | 197 | public override IEnumerable<double> GetEstimatedClassValues(IEnumerable<int> rows) {
|
---|
| 198 | return from xs in GetEstimatedClassValueVectors(ProblemData.Dataset, rows)
|
---|
| 199 | select AggregateEstimatedClassValues(xs);
|
---|
| 200 | }
|
---|
| 201 |
|
---|
[5816] | 202 | public IEnumerable<IEnumerable<double>> GetEstimatedClassValueVectors(Dataset dataset, IEnumerable<int> rows) {
|
---|
[6982] | 203 | if (!Model.Models.Any()) yield break;
|
---|
[6239] | 204 | var estimatedValuesEnumerators = (from model in Model.Models
|
---|
[5816] | 205 | select model.GetEstimatedClassValues(dataset, rows).GetEnumerator())
|
---|
| 206 | .ToList();
|
---|
| 207 |
|
---|
| 208 | while (estimatedValuesEnumerators.All(en => en.MoveNext())) {
|
---|
| 209 | yield return from enumerator in estimatedValuesEnumerators
|
---|
| 210 | select enumerator.Current;
|
---|
| 211 | }
|
---|
| 212 | }
|
---|
| 213 |
|
---|
[6239] | 214 | private double AggregateEstimatedClassValues(IEnumerable<double> estimatedClassValues) {
|
---|
| 215 | return estimatedClassValues
|
---|
| 216 | .GroupBy(x => x)
|
---|
| 217 | .OrderBy(g => -g.Count())
|
---|
| 218 | .Select(g => g.Key)
|
---|
[6254] | 219 | .DefaultIfEmpty(double.NaN)
|
---|
[6239] | 220 | .First();
|
---|
[5816] | 221 | }
|
---|
[6613] | 222 | #endregion
|
---|
[6520] | 223 |
|
---|
[6666] | 224 | protected override void OnProblemDataChanged() {
|
---|
| 225 | IClassificationProblemData problemData = new ClassificationProblemData(ProblemData.Dataset,
|
---|
| 226 | ProblemData.AllowedInputVariables,
|
---|
| 227 | ProblemData.TargetVariable);
|
---|
| 228 | problemData.TrainingPartition.Start = ProblemData.TrainingPartition.Start;
|
---|
| 229 | problemData.TrainingPartition.End = ProblemData.TrainingPartition.End;
|
---|
| 230 | problemData.TestPartition.Start = ProblemData.TestPartition.Start;
|
---|
| 231 | problemData.TestPartition.End = ProblemData.TestPartition.End;
|
---|
| 232 |
|
---|
| 233 | foreach (var solution in ClassificationSolutions) {
|
---|
| 234 | if (solution is ClassificationEnsembleSolution)
|
---|
| 235 | solution.ProblemData = ProblemData;
|
---|
| 236 | else
|
---|
| 237 | solution.ProblemData = problemData;
|
---|
| 238 | }
|
---|
| 239 | foreach (var trainingPartition in trainingPartitions.Values) {
|
---|
| 240 | trainingPartition.Start = ProblemData.TrainingPartition.Start;
|
---|
| 241 | trainingPartition.End = ProblemData.TrainingPartition.End;
|
---|
| 242 | }
|
---|
| 243 | foreach (var testPartition in testPartitions.Values) {
|
---|
| 244 | testPartition.Start = ProblemData.TestPartition.Start;
|
---|
| 245 | testPartition.End = ProblemData.TestPartition.End;
|
---|
| 246 | }
|
---|
| 247 |
|
---|
| 248 | base.OnProblemDataChanged();
|
---|
| 249 | }
|
---|
| 250 |
|
---|
[6613] | 251 | public void AddClassificationSolutions(IEnumerable<IClassificationSolution> solutions) {
|
---|
| 252 | classificationSolutions.AddRange(solutions);
|
---|
| 253 | }
|
---|
| 254 | public void RemoveClassificationSolutions(IEnumerable<IClassificationSolution> solutions) {
|
---|
| 255 | classificationSolutions.RemoveRange(solutions);
|
---|
| 256 | }
|
---|
[6520] | 257 |
|
---|
[6613] | 258 | private void classificationSolutions_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IClassificationSolution> e) {
|
---|
| 259 | foreach (var solution in e.Items) AddClassificationSolution(solution);
|
---|
[6520] | 260 | RecalculateResults();
|
---|
| 261 | }
|
---|
[6613] | 262 | private void classificationSolutions_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IClassificationSolution> e) {
|
---|
| 263 | foreach (var solution in e.Items) RemoveClassificationSolution(solution);
|
---|
| 264 | RecalculateResults();
|
---|
| 265 | }
|
---|
| 266 | private void classificationSolutions_CollectionReset(object sender, CollectionItemsChangedEventArgs<IClassificationSolution> e) {
|
---|
| 267 | foreach (var solution in e.OldItems) RemoveClassificationSolution(solution);
|
---|
| 268 | foreach (var solution in e.Items) AddClassificationSolution(solution);
|
---|
| 269 | RecalculateResults();
|
---|
| 270 | }
|
---|
[6520] | 271 |
|
---|
[6613] | 272 | private void AddClassificationSolution(IClassificationSolution solution) {
|
---|
| 273 | if (Model.Models.Contains(solution.Model)) throw new ArgumentException();
|
---|
| 274 | Model.Add(solution.Model);
|
---|
| 275 | trainingPartitions[solution.Model] = solution.ProblemData.TrainingPartition;
|
---|
| 276 | testPartitions[solution.Model] = solution.ProblemData.TestPartition;
|
---|
| 277 | }
|
---|
[6520] | 278 |
|
---|
[6613] | 279 | private void RemoveClassificationSolution(IClassificationSolution solution) {
|
---|
| 280 | if (!Model.Models.Contains(solution.Model)) throw new ArgumentException();
|
---|
| 281 | Model.Remove(solution.Model);
|
---|
| 282 | trainingPartitions.Remove(solution.Model);
|
---|
| 283 | testPartitions.Remove(solution.Model);
|
---|
[6520] | 284 | }
|
---|
[5816] | 285 | }
|
---|
| 286 | }
|
---|