Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis.Views/3.4/Classification/ClassificationSolutionConfusionMatrixView.cs @ 5664

Last change on this file since 5664 was 5664, checked in by gkronber, 13 years ago

#1418 ported ROC, confusion matrix and discriminant function classification views and fixed bug in threshold calculation.

File size: 5.4 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
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.Classification.Views {
30  [View("Classification solution confusion matrix view")]
31  [Content(typeof(IClassificationSolution))]
32  public partial class ClassificationSolutionConfusionMatrixView : AsynchronousContentView {
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        } else {
79          dataGridView.ColumnCount = Content.ProblemData.Classes;
80          dataGridView.RowCount = Content.ProblemData.Classes;
81
82          int i = 0;
83          foreach (string headerText in Content.ProblemData.ClassNames) {
84            dataGridView.Columns[i].HeaderText = "Actual " + headerText;
85            dataGridView.Rows[i].HeaderCell.Value = "Predicted " + headerText;
86            i++;
87          }
88          dataGridView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.ColumnHeader);
89          dataGridView.AutoResizeRowHeadersWidth(DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders);
90
91          FillDataGridView();
92        }
93      }
94    }
95
96    private void FillDataGridView() {
97      if (InvokeRequired) Invoke((Action)FillDataGridView);
98      else {
99        if (Content == null) return;
100
101        double[,] confusionMatrix = new double[Content.ProblemData.Classes, Content.ProblemData.Classes];
102        IEnumerable<int> rows;
103
104        if (cmbSamples.SelectedItem.ToString() == TrainingSamples) {
105          rows = Content.ProblemData.TrainingIndizes;
106        } else if (cmbSamples.SelectedItem.ToString() == TestSamples) {
107          rows = Content.ProblemData.TestIndizes;
108        } else throw new InvalidOperationException();
109
110        Dictionary<double, int> classValueIndexMapping = new Dictionary<double, int>();
111        int index = 0;
112        foreach (double classValue in Content.ProblemData.ClassValues.OrderBy(x => x)) {
113          classValueIndexMapping.Add(classValue, index);
114          index++;
115        }
116
117        double[] targetValues = Content.ProblemData.Dataset.GetEnumeratedVariableValues(Content.ProblemData.TargetVariable, rows).ToArray();
118        double[] predictedValues = Content.GetEstimatedClassValues(rows).ToArray();
119
120        for (int i = 0; i < targetValues.Length; i++) {
121          double targetValue = targetValues[i];
122          double predictedValue = predictedValues[i];
123          int targetIndex = classValueIndexMapping[targetValue];
124          int predictedIndex = classValueIndexMapping[predictedValue];
125
126          confusionMatrix[predictedIndex, targetIndex] += 1;
127        }
128
129        for (int row = 0; row < confusionMatrix.GetLength(0); row++) {
130          for (int col = 0; col < confusionMatrix.GetLength(1); col++) {
131            //TODO add scaling to relative values;
132            dataGridView[col, row].Value = confusionMatrix[row, col];
133          }
134        }
135      }
136    }
137
138    private void cmbSamples_SelectedIndexChanged(object sender, System.EventArgs e) {
139      FillDataGridView();
140    }
141  }
142}
Note: See TracBrowser for help on using the repository browser.