Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistentDataStructures/HeuristicLab.Problems.DataAnalysis.Views/3.4/Classification/ClassificationSolutionConfusionMatrixView.cs @ 14648

Last change on this file since 14648 was 14309, checked in by gkronber, 8 years ago

#2632: merged r14255 from trunk to stable

File size: 6.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Windows.Forms;
26using HeuristicLab.MainForm;
27using HeuristicLab.MainForm.WindowsForms;
28
29namespace HeuristicLab.Problems.DataAnalysis.Views {
30  [View("Confusion Matrix")]
31  [Content(typeof(IClassificationSolution))]
32  public partial class ClassificationSolutionConfusionMatrixView : DataAnalysisSolutionEvaluationView {
33    private const string TrainingSamples = "Training";
34    private const string TestSamples = "Test";
35    public ClassificationSolutionConfusionMatrixView() {
36      InitializeComponent();
37      cmbSamples.Items.Add(TrainingSamples);
38      cmbSamples.Items.Add(TestSamples);
39      cmbSamples.SelectedIndex = 0;
40    }
41
42    public new IClassificationSolution Content {
43      get { return (IClassificationSolution)base.Content; }
44      set { base.Content = value; }
45    }
46
47    protected override void RegisterContentEvents() {
48      base.RegisterContentEvents();
49      Content.ModelChanged += new EventHandler(Content_ModelChanged);
50      Content.ProblemDataChanged += new EventHandler(Content_ProblemDataChanged);
51    }
52
53
54    protected override void DeregisterContentEvents() {
55      base.DeregisterContentEvents();
56      Content.ModelChanged -= new EventHandler(Content_ModelChanged);
57      Content.ProblemDataChanged -= new EventHandler(Content_ProblemDataChanged);
58    }
59
60    private void Content_ModelChanged(object sender, EventArgs e) {
61      FillDataGridView();
62    }
63    private void Content_ProblemDataChanged(object sender, EventArgs e) {
64      UpdateDataGridView();
65    }
66
67    protected override void OnContentChanged() {
68      base.OnContentChanged();
69      UpdateDataGridView();
70    }
71
72    private void UpdateDataGridView() {
73      if (InvokeRequired) Invoke((Action)UpdateDataGridView);
74      else {
75        if (Content == null) {
76          dataGridView.RowCount = 1;
77          dataGridView.ColumnCount = 1;
78          dataGridView.TopLeftHeaderCell.Value = string.Empty;
79        } else {
80          dataGridView.ColumnCount = Content.ProblemData.Classes + 1;
81          dataGridView.RowCount = Content.ProblemData.Classes + 1;
82
83          int i = 0;
84          foreach (string headerText in Content.ProblemData.ClassNames) {
85            dataGridView.Columns[i].HeaderText = "Actual " + headerText;
86            dataGridView.Rows[i].HeaderCell.Value = "Predicted " + headerText;
87            i++;
88          }
89          dataGridView.Columns[i].HeaderText = "Actual not classified";
90          dataGridView.Rows[i].HeaderCell.Value = "Predicted not classified";
91
92          dataGridView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.ColumnHeader);
93          dataGridView.AutoResizeRowHeadersWidth(DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders);
94
95          dataGridView.TopLeftHeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
96          dataGridView.TopLeftHeaderCell.Value = Content.Model.TargetVariable;
97
98          FillDataGridView();
99        }
100      }
101    }
102
103    private void FillDataGridView() {
104      if (InvokeRequired) Invoke((Action)FillDataGridView);
105      else {
106        if (Content == null) return;
107
108        double[,] confusionMatrix = new double[Content.ProblemData.Classes + 1, Content.ProblemData.Classes + 1];
109        IEnumerable<int> rows;
110
111        double[] predictedValues;
112        if (cmbSamples.SelectedItem.ToString() == TrainingSamples) {
113          rows = Content.ProblemData.TrainingIndices;
114          predictedValues = Content.EstimatedTrainingClassValues.ToArray();
115        } else if (cmbSamples.SelectedItem.ToString() == TestSamples) {
116          rows = Content.ProblemData.TestIndices;
117          predictedValues = Content.EstimatedTestClassValues.ToArray();
118        } else throw new InvalidOperationException();
119
120        double[] targetValues = Content.ProblemData.Dataset.GetDoubleValues(Content.ProblemData.TargetVariable, rows).ToArray();
121
122        Dictionary<double, int> classValueIndexMapping = new Dictionary<double, int>();
123        int index = 0;
124        foreach (double classValue in Content.ProblemData.ClassValues.OrderBy(x => x)) {
125          classValueIndexMapping.Add(classValue, index);
126          index++;
127        }
128
129        for (int i = 0; i < targetValues.Length; i++) {
130          double targetValue = targetValues[i];
131          double predictedValue = predictedValues[i];
132          int targetIndex;
133          int predictedIndex;
134          if (!classValueIndexMapping.TryGetValue(targetValue, out targetIndex)) {
135            targetIndex = Content.ProblemData.Classes;
136          }
137          if (!classValueIndexMapping.TryGetValue(predictedValue, out predictedIndex)) {
138            predictedIndex = Content.ProblemData.Classes;
139          }
140
141          confusionMatrix[predictedIndex, targetIndex] += 1;
142        }
143
144        for (int row = 0; row < confusionMatrix.GetLength(0); row++) {
145          for (int col = 0; col < confusionMatrix.GetLength(1); col++) {
146            //TODO add scaling to relative values;
147            dataGridView[col, row].Value = confusionMatrix[row, col];
148          }
149        }
150      }
151    }
152
153    private void cmbSamples_SelectedIndexChanged(object sender, System.EventArgs e) {
154      FillDataGridView();
155    }
156  }
157}
Note: See TracBrowser for help on using the repository browser.