Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ClassificationEnsembleVoting/HeuristicLab.Problems.DataAnalysis.Views/3.4/Classification/ClassificationEnsembleSolutionEstimatedClassValuesView.cs @ 8297

Last change on this file since 8297 was 8297, checked in by sforsten, 12 years ago

#1776:

  • Corrected namespace of IClassificationEnsembleSolutionWeightCalculator interface
  • Corrected calculation of confidence for test and training samples in ClassificationEnsembleSolutionEstimatedClassValuesView
  • Added overload method GetConfidence to IClassificationEnsembleSolutionWeightCalculator to calculate more than one point at a time (maybe additional methods for training and test confidence could improve the performance remarkably)
  • Added ClassificationEnsembleSolutionConfidenceAccuracyDependence to see how accuracy would behave, if only samples with high confidence would be classified
File size: 10.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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.Drawing;
24using System.Linq;
25using System.Windows.Forms;
26using HeuristicLab.Common;
27using HeuristicLab.Data;
28using HeuristicLab.MainForm;
29using HeuristicLab.MainForm.WindowsForms;
30using HeuristicLab.Problems.DataAnalysis.Interfaces;
31
32namespace HeuristicLab.Problems.DataAnalysis.Views {
33  [View("Estimated Class Values")]
34  [Content(typeof(ClassificationEnsembleSolution))]
35  public partial class ClassificationEnsembleSolutionEstimatedClassValuesView :
36    ClassificationSolutionEstimatedClassValuesView {
37    private const string RowColumnName = "Row";
38    private const string TargetClassValuesColumnName = "Target Variable";
39    private const string EstimatedClassValuesColumnName = "Estimated Class Values";
40    private const string CorrectClassificationColumnName = "Correct Classification";
41    private const string ConfidenceColumnName = "Confidence";
42
43    private const string SamplesComboBoxAllSamples = "All Samples";
44    private const string SamplesComboBoxTrainingSamples = "Training Samples";
45    private const string SamplesComboBoxTestSamples = "Test Samples";
46
47    public new ClassificationEnsembleSolution Content {
48      get { return (ClassificationEnsembleSolution)base.Content; }
49      set { base.Content = value; }
50    }
51
52    public ClassificationEnsembleSolutionEstimatedClassValuesView()
53      : base() {
54      InitializeComponent();
55      SamplesComboBox.Items.AddRange(new string[] { SamplesComboBoxAllSamples, SamplesComboBoxTrainingSamples, SamplesComboBoxTestSamples });
56      SamplesComboBox.SelectedIndex = 0;
57      matrixView.DataGridView.RowPrePaint += new DataGridViewRowPrePaintEventHandler(DataGridView_RowPrePaint);
58    }
59
60    private void SamplesComboBox_SelectedIndexChanged(object sender, EventArgs e) {
61      UpdateEstimatedValues();
62    }
63
64    protected override void UpdateEstimatedValues() {
65      if (InvokeRequired) {
66        Invoke((Action)UpdateEstimatedValues);
67        return;
68      }
69      if (Content == null) {
70        matrixView.Content = null;
71        return;
72      }
73
74      int[] indizes;
75      double[] estimatedClassValues;
76
77      switch (SamplesComboBox.SelectedItem.ToString()) {
78        case SamplesComboBoxAllSamples: {
79            indizes = Enumerable.Range(0, Content.ProblemData.Dataset.Rows).ToArray();
80            estimatedClassValues = Content.EstimatedClassValues.ToArray();
81            break;
82          }
83        case SamplesComboBoxTrainingSamples: {
84            indizes = Content.ProblemData.TrainingIndizes.ToArray();
85            estimatedClassValues = Content.EstimatedTrainingClassValues.ToArray();
86            break;
87          }
88        case SamplesComboBoxTestSamples: {
89            indizes = Content.ProblemData.TestIndizes.ToArray();
90            estimatedClassValues = Content.EstimatedTestClassValues.ToArray();
91            break;
92          }
93        default:
94          throw new ArgumentException();
95      }
96
97      IEnumerable<IClassificationSolution> solutions = Content.ClassificationSolutions.CheckedItems;
98      int classValuesCount = Content.ProblemData.ClassValues.Count;
99      int solutionsCount = solutions.Count();
100      string[,] values = new string[indizes.Length, 5 + classValuesCount + solutionsCount];
101      double[] target = Content.ProblemData.Dataset.GetDoubleValues(Content.ProblemData.TargetVariable).ToArray();
102      List<List<double?>> estimatedValuesVector = GetEstimatedValues(SamplesComboBox.SelectedItem.ToString(), indizes,
103                                                            solutions);
104
105      IClassificationEnsembleSolutionWeightCalculator weightCalc = Content.WeightCalculator;
106
107      // needed to calculate average confidences of correct and wrong estimated classes
108      bool correctClassified;
109      double[] confidence = new double[2];
110      int[] classified = new int[2];
111      double curConfidence;
112
113      double[] confidences = null;
114      if (SamplesComboBox.SelectedItem.ToString() == SamplesComboBoxAllSamples) {
115        confidences = weightCalc.GetConfidence(solutions, indizes, estimatedClassValues).ToArray();
116      }
117
118      for (int i = 0; i < indizes.Length; i++) {
119        int row = indizes[i];
120        values[i, 0] = row.ToString();
121        values[i, 1] = target[i].ToString();
122        //display only indices and target values if no models are present
123        if (solutionsCount > 0) {
124          values[i, 2] = estimatedClassValues[i].ToString();
125          correctClassified = target[i].IsAlmost(estimatedClassValues[i]);
126          values[i, 3] = correctClassified.ToString();
127          if (SamplesComboBox.SelectedItem.ToString() == SamplesComboBoxAllSamples) {
128            curConfidence = confidences[i];
129          } else {
130            curConfidence = weightCalc.GetConfidence(GetRelevantSolutions(SamplesComboBox.SelectedItem.ToString(), solutions, row),
131                                                     indizes[i], estimatedClassValues[i]);
132          }
133          if (correctClassified) {
134            confidence[0] += curConfidence;
135            classified[0]++;
136          } else {
137            confidence[1] += curConfidence;
138            classified[1]++;
139          }
140
141          values[i, 4] = curConfidence.ToString();
142
143          var groups =
144            estimatedValuesVector[i].GroupBy(x => x).Select(g => new { Key = g.Key, Count = g.Count() }).ToList();
145          for (int classIndex = 0; classIndex < Content.ProblemData.ClassValues.Count; classIndex++) {
146            var group = groups.Where(g => g.Key == Content.ProblemData.ClassValues[classIndex]).SingleOrDefault();
147            if (group == null) values[i, 5 + classIndex] = 0.ToString();
148            else values[i, 5 + classIndex] = group.Count.ToString();
149          }
150          for (int modelIndex = 0; modelIndex < estimatedValuesVector[i].Count; modelIndex++) {
151            values[i, 5 + classValuesCount + modelIndex] = estimatedValuesVector[i][modelIndex] == null
152                                                             ? string.Empty
153                                                             : estimatedValuesVector[i][modelIndex].ToString();
154          }
155        }
156      }
157
158      CorrectClassifiedConfidence.Text = (confidence[0] / (double)classified[0]).ToString();
159      WrongClassifiedConfidence.Text = (confidence[1] / (double)classified[1]).ToString();
160
161      StringMatrix matrix = new StringMatrix(values);
162      List<string> columnNames = new List<string>() { "Id", TargetClassValuesColumnName, EstimatedClassValuesColumnName, CorrectClassificationColumnName, ConfidenceColumnName };
163      columnNames.AddRange(Content.ProblemData.ClassNames);
164      columnNames.AddRange(Content.ClassificationSolutions.CheckedItems.Select(s => s.Model.Name));//.Model.Models.Select(m => m.Name));
165      matrix.ColumnNames = columnNames;
166      matrix.SortableView = true;
167      matrixView.Content = matrix;
168    }
169
170    protected IEnumerable<IClassificationSolution> GetRelevantSolutions(string samplesSelection, IEnumerable<IClassificationSolution> solutions, int curRow) {
171      if (samplesSelection == SamplesComboBoxAllSamples)
172        return solutions;
173      else if (samplesSelection == SamplesComboBoxTrainingSamples)
174        return solutions.Where(s => s.ProblemData.IsTrainingSample(curRow));
175      else if (samplesSelection == SamplesComboBoxTestSamples)
176        return solutions.Where(s => s.ProblemData.IsTestSample(curRow));
177      else
178        return new List<IClassificationSolution>();
179    }
180
181    private IEnumerable<int> FindAllIndices(List<double?> list, double value) {
182      List<int> indices = new List<int>();
183      for (int i = 0; i < list.Count; i++) {
184        if (list[i].Equals(value))
185          indices.Add(i);
186      }
187      return indices;
188    }
189
190    private List<List<double?>> GetEstimatedValues(string samplesSelection, int[] rows, IEnumerable<IClassificationSolution> solutions) {
191      List<List<double?>> values = new List<List<double?>>();
192      int solutionIndex = 0;
193      foreach (var solution in solutions) {
194        double[] estimation = solution.GetEstimatedClassValues(rows).ToArray();
195        for (int i = 0; i < rows.Length; i++) {
196          var row = rows[i];
197          if (solutionIndex == 0) values.Add(new List<double?>());
198
199          if (samplesSelection == SamplesComboBoxAllSamples)
200            values[i].Add(estimation[i]);
201          else if (samplesSelection == SamplesComboBoxTrainingSamples && solution.ProblemData.IsTrainingSample(row))
202            values[i].Add(estimation[i]);
203          else if (samplesSelection == SamplesComboBoxTestSamples && solution.ProblemData.IsTestSample(row))
204            values[i].Add(estimation[i]);
205          else
206            values[i].Add(null);
207        }
208        solutionIndex++;
209      }
210      return values;
211    }
212
213    private void DataGridView_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e) {
214      if (InvokeRequired) {
215        Invoke(new EventHandler<DataGridViewRowPrePaintEventArgs>(DataGridView_RowPrePaint), sender, e);
216        return;
217      }
218      var cellValue = matrixView.DataGridView[3, e.RowIndex].Value.ToString();
219      if (string.IsNullOrEmpty(cellValue)) return;
220      bool correctClassified = bool.Parse(cellValue);
221      matrixView.DataGridView.Rows[e.RowIndex].DefaultCellStyle.ForeColor = correctClassified ? Color.MediumSeaGreen : Color.Red;
222    }
223  }
224}
Note: See TracBrowser for help on using the repository browser.