Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1776:

  • merged r8810:8862 from trunk into branch
  • fixed exception in ClassificationEnsembleSolutionAccuracyToCoveredSamples, if no estimated values are in a partition, so nothing can be shown
File size: 9.8 KB
RevLine 
[5678]1#region License Information
2/* HeuristicLab
[7259]3 * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[5678]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
[8811]21
[5678]22using System;
[6672]23using System.Collections.Generic;
[6680]24using System.Drawing;
[5678]25using System.Linq;
26using System.Windows.Forms;
[7022]27using HeuristicLab.Common;
[5678]28using HeuristicLab.Data;
29using HeuristicLab.MainForm;
30using HeuristicLab.MainForm.WindowsForms;
31
32namespace HeuristicLab.Problems.DataAnalysis.Views {
[5975]33  [View("Estimated Class Values")]
[6672]34  [Content(typeof(ClassificationEnsembleSolution))]
35  public partial class ClassificationEnsembleSolutionEstimatedClassValuesView :
36    ClassificationSolutionEstimatedClassValuesView {
37    private const string RowColumnName = "Row";
[6680]38    private const string TargetClassValuesColumnName = "Target Variable";
39    private const string EstimatedClassValuesColumnName = "Estimated Class Values";
40    private const string CorrectClassificationColumnName = "Correct Classification";
[6672]41    private const string ConfidenceColumnName = "Confidence";
[5678]42
[6672]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; }
[6642]49      set { base.Content = value; }
[5678]50    }
51
[6672]52    public ClassificationEnsembleSolutionEstimatedClassValuesView()
[5678]53      : base() {
54      InitializeComponent();
[6672]55      SamplesComboBox.Items.AddRange(new string[] { SamplesComboBoxAllSamples, SamplesComboBoxTrainingSamples, SamplesComboBoxTestSamples });
56      SamplesComboBox.SelectedIndex = 0;
[6680]57      matrixView.DataGridView.RowPrePaint += new DataGridViewRowPrePaintEventHandler(DataGridView_RowPrePaint);
[5678]58    }
59
[6672]60    private void SamplesComboBox_SelectedIndexChanged(object sender, EventArgs e) {
61      UpdateEstimatedValues();
62    }
63
[6642]64    protected override void UpdateEstimatedValues() {
[6672]65      if (InvokeRequired) {
66        Invoke((Action)UpdateEstimatedValues);
67        return;
68      }
69      if (Content == null) {
70        matrixView.Content = null;
71        return;
72      }
[5678]73
[6672]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;
[5678]82          }
[6672]83        case SamplesComboBoxTrainingSamples: {
[8508]84            indizes = Content.ProblemData.TrainingIndices.ToArray();
[6672]85            estimatedClassValues = Content.EstimatedTrainingClassValues.ToArray();
86            break;
87          }
88        case SamplesComboBoxTestSamples: {
[8508]89            indizes = Content.ProblemData.TestIndices.ToArray();
[6672]90            estimatedClassValues = Content.EstimatedTestClassValues.ToArray();
91            break;
92          }
93        default:
94          throw new ArgumentException();
95      }
[5678]96
[7562]97      IEnumerable<IClassificationSolution> solutions = Content.ClassificationSolutions.CheckedItems;
[8811]98      int classValuesCount = Content.ProblemData.Classes;
[7562]99      int solutionsCount = solutions.Count();
[7022]100      string[,] values = new string[indizes.Length, 5 + classValuesCount + solutionsCount];
[6740]101      double[] target = Content.ProblemData.Dataset.GetDoubleValues(Content.ProblemData.TargetVariable).ToArray();
[6672]102      List<List<double?>> estimatedValuesVector = GetEstimatedValues(SamplesComboBox.SelectedItem.ToString(), indizes,
[7562]103                                                            solutions);
[6672]104
[7562]105      IClassificationEnsembleSolutionWeightCalculator weightCalc = Content.WeightCalculator;
106
[8101]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
[8297]113      double[] confidences = null;
[8814]114
115      if (SamplesComboBox.SelectedItem.ToString().Equals(SamplesComboBoxAllSamples)) {
116        confidences = weightCalc.GetConfidence(solutions,
117                                               indizes,
118                                               estimatedClassValues,
119                                               weightCalc.GetAllClassDelegate()).ToArray();
120      } else if (SamplesComboBox.SelectedItem.ToString().Equals(SamplesComboBoxTrainingSamples)) {
121        confidences = weightCalc.GetConfidence(solutions,
122                                               indizes,
123                                               estimatedClassValues,
124                                               weightCalc.GetTrainingClassDelegate()).ToArray();
125      } else if (SamplesComboBox.SelectedItem.ToString().Equals(SamplesComboBoxTestSamples)) {
126        confidences = weightCalc.GetConfidence(solutions,
127                                               indizes,
128                                               estimatedClassValues,
129                                               weightCalc.GetTestClassDelegate()).ToArray();
[8297]130      }
131
[6672]132      for (int i = 0; i < indizes.Length; i++) {
133        int row = indizes[i];
134        values[i, 0] = row.ToString();
[8863]135        values[i, 1] = target[row].ToString();
[6982]136        //display only indices and target values if no models are present
[7022]137        if (solutionsCount > 0) {
138          values[i, 2] = estimatedClassValues[i].ToString();
[8863]139          correctClassified = target[row].IsAlmost(estimatedClassValues[i]);
[8101]140          values[i, 3] = correctClassified.ToString();
[8814]141          curConfidence = confidences[i];
[8101]142          if (correctClassified) {
143            confidence[0] += curConfidence;
144            classified[0]++;
145          } else {
146            confidence[1] += curConfidence;
147            classified[1]++;
148          }
[7464]149
[8101]150          values[i, 4] = curConfidence.ToString();
151
[7022]152          var groups =
153            estimatedValuesVector[i].GroupBy(x => x).Select(g => new { Key = g.Key, Count = g.Count() }).ToList();
[8811]154
155          for (int classIndex = 0; classIndex < Content.ProblemData.Classes; classIndex++) {
156            var group = groups.Where(g => g.Key == Content.ProblemData.ClassValues.ElementAt(classIndex)).SingleOrDefault();
[7022]157            if (group == null) values[i, 5 + classIndex] = 0.ToString();
158            else values[i, 5 + classIndex] = group.Count.ToString();
159          }
160          for (int modelIndex = 0; modelIndex < estimatedValuesVector[i].Count; modelIndex++) {
161            values[i, 5 + classValuesCount + modelIndex] = estimatedValuesVector[i][modelIndex] == null
162                                                             ? string.Empty
163                                                             : estimatedValuesVector[i][modelIndex].ToString();
164          }
[5678]165        }
166      }
[6672]167
[8101]168      CorrectClassifiedConfidence.Text = (confidence[0] / (double)classified[0]).ToString();
169      WrongClassifiedConfidence.Text = (confidence[1] / (double)classified[1]).ToString();
170
[6672]171      StringMatrix matrix = new StringMatrix(values);
[6680]172      List<string> columnNames = new List<string>() { "Id", TargetClassValuesColumnName, EstimatedClassValuesColumnName, CorrectClassificationColumnName, ConfidenceColumnName };
[6672]173      columnNames.AddRange(Content.ProblemData.ClassNames);
[7562]174      columnNames.AddRange(Content.ClassificationSolutions.CheckedItems.Select(s => s.Model.Name));//.Model.Models.Select(m => m.Name));
[6672]175      matrix.ColumnNames = columnNames;
176      matrix.SortableView = true;
177      matrixView.Content = matrix;
[5678]178    }
[6672]179
180    private List<List<double?>> GetEstimatedValues(string samplesSelection, int[] rows, IEnumerable<IClassificationSolution> solutions) {
181      List<List<double?>> values = new List<List<double?>>();
182      int solutionIndex = 0;
183      foreach (var solution in solutions) {
184        double[] estimation = solution.GetEstimatedClassValues(rows).ToArray();
185        for (int i = 0; i < rows.Length; i++) {
186          var row = rows[i];
187          if (solutionIndex == 0) values.Add(new List<double?>());
188
189          if (samplesSelection == SamplesComboBoxAllSamples)
190            values[i].Add(estimation[i]);
191          else if (samplesSelection == SamplesComboBoxTrainingSamples && solution.ProblemData.IsTrainingSample(row))
192            values[i].Add(estimation[i]);
193          else if (samplesSelection == SamplesComboBoxTestSamples && solution.ProblemData.IsTestSample(row))
194            values[i].Add(estimation[i]);
195          else
196            values[i].Add(null);
197        }
198        solutionIndex++;
199      }
200      return values;
201    }
202
[6680]203    private void DataGridView_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e) {
204      if (InvokeRequired) {
205        Invoke(new EventHandler<DataGridViewRowPrePaintEventArgs>(DataGridView_RowPrePaint), sender, e);
206        return;
207      }
[6982]208      var cellValue = matrixView.DataGridView[3, e.RowIndex].Value.ToString();
209      if (string.IsNullOrEmpty(cellValue)) return;
210      bool correctClassified = bool.Parse(cellValue);
[6680]211      matrixView.DataGridView.Rows[e.RowIndex].DefaultCellStyle.ForeColor = correctClassified ? Color.MediumSeaGreen : Color.Red;
212    }
[5678]213  }
214}
Note: See TracBrowser for help on using the repository browser.