Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis.Classification.Views/3.3/ConfusionMatrixView.cs @ 6934

Last change on this file since 6934 was 5445, checked in by swagner, 14 years ago

Updated year of copyrights (#1406)

File size: 5.7 KB
RevLine 
[4417]1#region License Information
2/* HeuristicLab
[5445]3 * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[4417]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("Confusion Matrix View")]
31  [Content(typeof(SymbolicClassificationSolution))]
32  public partial class ConfusionMatrixView : AsynchronousContentView {
33    private const string TrainingSamples = "Training";
34    private const string TestSamples = "Test";
35    public ConfusionMatrixView() {
36      InitializeComponent();
37      cmbSamples.Items.Add(TrainingSamples);
38      cmbSamples.Items.Add(TestSamples);
39      cmbSamples.SelectedIndex = 0;
40    }
41
42    public new SymbolicClassificationSolution Content {
43      get { return (SymbolicClassificationSolution)base.Content; }
44      set { base.Content = value; }
45    }
46
47    protected override void RegisterContentEvents() {
48      base.RegisterContentEvents();
49      Content.EstimatedValuesChanged += new EventHandler(Content_EstimatedValuesChanged);
50      Content.ProblemDataChanged += new EventHandler(Content_ProblemDataChanged);
51      Content.ThresholdsChanged += new EventHandler(Content_ThresholdsChanged);
52    }
53
54
55    protected override void DeregisterContentEvents() {
56      base.DeregisterContentEvents();
57      Content.EstimatedValuesChanged -= new EventHandler(Content_EstimatedValuesChanged);
58      Content.ProblemDataChanged -= new EventHandler(Content_ProblemDataChanged);
59      Content.ThresholdsChanged -= new EventHandler(Content_ThresholdsChanged);
60    }
61
62    private void Content_EstimatedValuesChanged(object sender, EventArgs e) {
63      FillDataGridView();
64    }
65    private void Content_ProblemDataChanged(object sender, EventArgs e) {
66      UpdateDataGridView();
67    }
68    private void Content_ThresholdsChanged(object sender, EventArgs e) {
69      FillDataGridView();
70    }
71
72    protected override void OnContentChanged() {
73      base.OnContentChanged();
74      UpdateDataGridView();
75    }
76
77    private void UpdateDataGridView() {
78      if (InvokeRequired) Invoke((Action)UpdateDataGridView);
79      else {
80        if (Content == null) {
81          dataGridView.RowCount = 1;
82          dataGridView.ColumnCount = 1;
83        } else {
84          dataGridView.ColumnCount = Content.ProblemData.NumberOfClasses;
85          dataGridView.RowCount = Content.ProblemData.NumberOfClasses;
86
87          int i = 0;
88          foreach (string headerText in Content.ProblemData.ClassNames) {
89            dataGridView.Columns[i].HeaderText = "Actual " + headerText;
90            dataGridView.Rows[i].HeaderCell.Value = "Predicted " + headerText;
91            i++;
92          }
93          dataGridView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.ColumnHeader);
94          dataGridView.AutoResizeRowHeadersWidth(DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders);
95
96          FillDataGridView();
97        }
98      }
99    }
100
101    private void FillDataGridView() {
102      if (InvokeRequired) Invoke((Action)FillDataGridView);
103      else {
104        if (Content == null) return;
105
106        double[,] confusionMatrix = new double[Content.ProblemData.NumberOfClasses, Content.ProblemData.NumberOfClasses];
[4469]107        IEnumerable<int> rows;
[4417]108
109        if (cmbSamples.SelectedItem.ToString() == TrainingSamples) {
[4469]110          rows = Content.ProblemData.TrainingIndizes;
[4417]111        } else if (cmbSamples.SelectedItem.ToString() == TestSamples) {
[4469]112          rows = Content.ProblemData.TestIndizes;
[4417]113        } else throw new InvalidOperationException();
114
115        Dictionary<double, int> classValueIndexMapping = new Dictionary<double, int>();
116        int index = 0;
117        foreach (double classValue in Content.ProblemData.SortedClassValues) {
118          classValueIndexMapping.Add(classValue, index);
119          index++;
120        }
121
[4469]122        double[] targetValues = Content.ProblemData.Dataset.GetEnumeratedVariableValues(Content.ProblemData.TargetVariable.Value, rows).ToArray();
123        double[] predictedValues = Content.GetEstimatedClassValues(rows).ToArray();
[4417]124
125        for (int i = 0; i < targetValues.Length; i++) {
126          double targetValue = targetValues[i];
127          double predictedValue = predictedValues[i];
128          int targetIndex = classValueIndexMapping[targetValue];
129          int predictedIndex = classValueIndexMapping[predictedValue];
130
131          confusionMatrix[predictedIndex, targetIndex] += 1;
132        }
133
134        for (int row = 0; row < confusionMatrix.GetLength(0); row++) {
135          for (int col = 0; col < confusionMatrix.GetLength(1); col++) {
136            //TODO add scaling to relative values;
137            dataGridView[col, row].Value = confusionMatrix[row, col];
138          }
139        }
140      }
141    }
142
143    private void cmbSamples_SelectedIndexChanged(object sender, System.EventArgs e) {
144      FillDataGridView();
145    }
146  }
147}
Note: See TracBrowser for help on using the repository browser.