Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Classification/HeuristicLab.Problems.DataAnalysis.Classification.Views/3.3/ConfusionMatrixView.cs @ 4417

Last change on this file since 4417 was 4417, checked in by mkommend, 14 years ago

adapted classification analyzer and readded views project (ticket #939)

File size: 5.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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("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];
107        int start;
108        int end;
109
110        if (cmbSamples.SelectedItem.ToString() == TrainingSamples) {
111          start = Content.ProblemData.TrainingSamplesStart.Value;
112          end = Content.ProblemData.TrainingSamplesEnd.Value;
113        } else if (cmbSamples.SelectedItem.ToString() == TestSamples) {
114          start = Content.ProblemData.TestSamplesStart.Value;
115          end = Content.ProblemData.TestSamplesEnd.Value;
116        } else throw new InvalidOperationException();
117
118        Dictionary<double, int> classValueIndexMapping = new Dictionary<double, int>();
119        int index = 0;
120        foreach (double classValue in Content.ProblemData.SortedClassValues) {
121          classValueIndexMapping.Add(classValue, index);
122          index++;
123        }
124
125        double[] targetValues = Content.ProblemData.Dataset.GetVariableValues(Content.ProblemData.TargetVariable.Value, start, end);
126        double[] predictedValues = Content.EstimatedClassValues.Skip(start).Take(end - start).ToArray();
127
128        for (int i = 0; i < targetValues.Length; i++) {
129          double targetValue = targetValues[i];
130          double predictedValue = predictedValues[i];
131          int targetIndex = classValueIndexMapping[targetValue];
132          int predictedIndex = classValueIndexMapping[predictedValue];
133
134          confusionMatrix[predictedIndex, targetIndex] += 1;
135        }
136
137        for (int row = 0; row < confusionMatrix.GetLength(0); row++) {
138          for (int col = 0; col < confusionMatrix.GetLength(1); col++) {
139            //TODO add scaling to relative values;
140            dataGridView[col, row].Value = confusionMatrix[row, col];
141          }
142        }
143      }
144    }
145
146    private void cmbSamples_SelectedIndexChanged(object sender, System.EventArgs e) {
147      FillDataGridView();
148    }
149  }
150}
Note: See TracBrowser for help on using the repository browser.