Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis.Views/3.4/Classification/ClassificationEnsembleSolutionEstimatedClassValuesView.cs @ 6672

Last change on this file since 6672 was 6672, checked in by mkommend, 11 years ago

#1592: Implemented new estimated class values view for ClassificationEnsembleSolutions.

File size: 6.8 KB
Line 
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
21using System;
22using System.Collections.Generic;
23using System.Linq;
24using System.Windows.Forms;
25using HeuristicLab.Data;
26using HeuristicLab.MainForm;
27using HeuristicLab.MainForm.WindowsForms;
28
29namespace HeuristicLab.Problems.DataAnalysis.Views {
30  [View("Estimated Class Values")]
31  [Content(typeof(ClassificationEnsembleSolution))]
32  public partial class ClassificationEnsembleSolutionEstimatedClassValuesView :
33    ClassificationSolutionEstimatedClassValuesView {
34    private const string RowColumnName = "Row";
35    private const string TargetClassValuesColumnName = "TargetVariable";
36    private const string EstimatedClassValuesColumnName = "EstimatedClassValues";
37    private const string ConfidenceColumnName = "Confidence";
38
39    private const string SamplesComboBoxAllSamples = "All Samples";
40    private const string SamplesComboBoxTrainingSamples = "Training Samples";
41    private const string SamplesComboBoxTestSamples = "Test Samples";
42
43    public new ClassificationEnsembleSolution Content {
44      get { return (ClassificationEnsembleSolution)base.Content; }
45      set { base.Content = value; }
46    }
47
48    public ClassificationEnsembleSolutionEstimatedClassValuesView()
49      : base() {
50      InitializeComponent();
51      SamplesComboBox.Items.AddRange(new string[] { SamplesComboBoxAllSamples, SamplesComboBoxTrainingSamples, SamplesComboBoxTestSamples });
52      SamplesComboBox.SelectedIndex = 0;
53    }
54
55    private void SamplesComboBox_SelectedIndexChanged(object sender, EventArgs e) {
56      UpdateEstimatedValues();
57    }
58
59    protected override void UpdateEstimatedValues() {
60      if (InvokeRequired) {
61        Invoke((Action)UpdateEstimatedValues);
62        return;
63      }
64      if (Content == null) {
65        matrixView.Content = null;
66        return;
67      }
68
69      int[] indizes;
70      double[] estimatedClassValues;
71
72      switch (SamplesComboBox.SelectedItem.ToString()) {
73        case SamplesComboBoxAllSamples: {
74            indizes = Enumerable.Range(0, Content.ProblemData.Dataset.Rows).ToArray();
75            estimatedClassValues = Content.EstimatedClassValues.ToArray();
76            break;
77          }
78        case SamplesComboBoxTrainingSamples: {
79            indizes = Content.ProblemData.TrainingIndizes.ToArray();
80            estimatedClassValues = Content.EstimatedTrainingClassValues.ToArray();
81            break;
82          }
83        case SamplesComboBoxTestSamples: {
84            indizes = Content.ProblemData.TestIndizes.ToArray();
85            estimatedClassValues = Content.EstimatedTestClassValues.ToArray();
86            break;
87          }
88        default:
89          throw new ArgumentException();
90      }
91
92      int classValuesCount = Content.ProblemData.ClassValues.Count;
93      int modelCount = Content.Model.Models.Count();
94      string[,] values = new string[indizes.Length, 4 + classValuesCount + modelCount];
95      double[] target = Content.ProblemData.Dataset.GetVariableValues(Content.ProblemData.TargetVariable);
96      List<List<double?>> estimatedValuesVector = GetEstimatedValues(SamplesComboBox.SelectedItem.ToString(), indizes,
97                                                            Content.ClassificationSolutions);
98
99      for (int i = 0; i < indizes.Length; i++) {
100        int row = indizes[i];
101        values[i, 0] = row.ToString();
102        values[i, 1] = target[i].ToString();
103        values[i, 2] = estimatedClassValues[i].ToString();
104        var groups = estimatedValuesVector[i].GroupBy(x => x).Select(g => new { Key = g.Key, Count = g.Count() }).ToList();
105        var estimationCount = groups.Where(g => g.Key != null).Select(g => g.Count).Sum();
106        values[i, 3] = (((double)groups.Where(g => g.Key == estimatedClassValues[i]).Single().Count) / estimationCount).ToString();
107        for (int classIndex = 0; classIndex < Content.ProblemData.ClassValues.Count; classIndex++) {
108          var group = groups.Where(g => g.Key == Content.ProblemData.ClassValues[classIndex]).SingleOrDefault();
109          if (group == null) values[i, 4 + classIndex] = 0.ToString();
110          else values[i, 4 + classIndex] = group.Count.ToString();
111        }
112        for (int modelIndex = 0; modelIndex < estimatedValuesVector[i].Count; modelIndex++) {
113          values[i, 4 + classValuesCount + modelIndex] = estimatedValuesVector[i][modelIndex] == null
114                                                           ? string.Empty
115                                                           : estimatedValuesVector[i][modelIndex].ToString();
116        }
117
118      }
119
120      StringMatrix matrix = new StringMatrix(values);
121      List<string> columnNames = new List<string>() { "Id", TargetClassValuesColumnName, EstimatedClassValuesColumnName, ConfidenceColumnName };
122      columnNames.AddRange(Content.ProblemData.ClassNames);
123      columnNames.AddRange(Content.Model.Models.Select(m => m.Name));
124      matrix.ColumnNames = columnNames;
125      matrix.SortableView = true;
126      matrixView.Content = matrix;
127    }
128
129    private List<List<double?>> GetEstimatedValues(string samplesSelection, int[] rows, IEnumerable<IClassificationSolution> solutions) {
130      List<List<double?>> values = new List<List<double?>>();
131      int solutionIndex = 0;
132      foreach (var solution in solutions) {
133        double[] estimation = solution.GetEstimatedClassValues(rows).ToArray();
134        for (int i = 0; i < rows.Length; i++) {
135          var row = rows[i];
136          if (solutionIndex == 0) values.Add(new List<double?>());
137
138          if (samplesSelection == SamplesComboBoxAllSamples)
139            values[i].Add(estimation[i]);
140          else if (samplesSelection == SamplesComboBoxTrainingSamples && solution.ProblemData.IsTrainingSample(row))
141            values[i].Add(estimation[i]);
142          else if (samplesSelection == SamplesComboBoxTestSamples && solution.ProblemData.IsTestSample(row))
143            values[i].Add(estimation[i]);
144          else
145            values[i].Add(null);
146        }
147        solutionIndex++;
148      }
149      return values;
150    }
151
152
153
154  }
155}
Note: See TracBrowser for help on using the repository browser.