Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis.Views/3.4/Classification/ClassificationSolutionConfusionMatrixView.cs @ 5975

Last change on this file since 5975 was 5975, checked in by mkommend, 13 years ago

#1313: Updated view names to use spaces instead of !camelCasing.

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.Core.Views;
27using HeuristicLab.MainForm;
28using HeuristicLab.MainForm.WindowsForms;
29
30namespace HeuristicLab.Problems.DataAnalysis.Views {
31  [View("Confusion Matrix")]
32  [Content(typeof(IClassificationSolution))]
33  public partial class ClassificationSolutionConfusionMatrixView : ItemView, IClassificationSolutionEvaluationView {
34    private const string TrainingSamples = "Training";
35    private const string TestSamples = "Test";
36    public ClassificationSolutionConfusionMatrixView() {
37      InitializeComponent();
38      cmbSamples.Items.Add(TrainingSamples);
39      cmbSamples.Items.Add(TestSamples);
40      cmbSamples.SelectedIndex = 0;
41    }
42
43    public new IClassificationSolution Content {
44      get { return (IClassificationSolution)base.Content; }
45      set { base.Content = value; }
46    }
47
48    protected override void RegisterContentEvents() {
49      base.RegisterContentEvents();
50      Content.ModelChanged += new EventHandler(Content_ModelChanged);
51      Content.ProblemDataChanged += new EventHandler(Content_ProblemDataChanged);
52    }
53
54
55    protected override void DeregisterContentEvents() {
56      base.DeregisterContentEvents();
57      Content.ModelChanged -= new EventHandler(Content_ModelChanged);
58      Content.ProblemDataChanged -= new EventHandler(Content_ProblemDataChanged);
59    }
60
61    private void Content_ModelChanged(object sender, EventArgs e) {
62      FillDataGridView();
63    }
64    private void Content_ProblemDataChanged(object sender, EventArgs e) {
65      UpdateDataGridView();
66    }
67
68    protected override void OnContentChanged() {
69      base.OnContentChanged();
70      UpdateDataGridView();
71    }
72
73    private void UpdateDataGridView() {
74      if (InvokeRequired) Invoke((Action)UpdateDataGridView);
75      else {
76        if (Content == null) {
77          dataGridView.RowCount = 1;
78          dataGridView.ColumnCount = 1;
79        } else {
80          dataGridView.ColumnCount = Content.ProblemData.Classes;
81          dataGridView.RowCount = Content.ProblemData.Classes;
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.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.ColumnHeader);
90          dataGridView.AutoResizeRowHeadersWidth(DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders);
91
92          FillDataGridView();
93        }
94      }
95    }
96
97    private void FillDataGridView() {
98      if (InvokeRequired) Invoke((Action)FillDataGridView);
99      else {
100        if (Content == null) return;
101
102        double[,] confusionMatrix = new double[Content.ProblemData.Classes, Content.ProblemData.Classes];
103        IEnumerable<int> rows;
104
105        if (cmbSamples.SelectedItem.ToString() == TrainingSamples) {
106          rows = Content.ProblemData.TrainingIndizes;
107        } else if (cmbSamples.SelectedItem.ToString() == TestSamples) {
108          rows = Content.ProblemData.TestIndizes;
109        } else throw new InvalidOperationException();
110
111        Dictionary<double, int> classValueIndexMapping = new Dictionary<double, int>();
112        int index = 0;
113        foreach (double classValue in Content.ProblemData.ClassValues.OrderBy(x => x)) {
114          classValueIndexMapping.Add(classValue, index);
115          index++;
116        }
117
118        double[] targetValues = Content.ProblemData.Dataset.GetEnumeratedVariableValues(Content.ProblemData.TargetVariable, rows).ToArray();
119        double[] predictedValues = Content.GetEstimatedClassValues(rows).ToArray();
120
121        for (int i = 0; i < targetValues.Length; i++) {
122          double targetValue = targetValues[i];
123          double predictedValue = predictedValues[i];
124          int targetIndex = classValueIndexMapping[targetValue];
125          int predictedIndex = classValueIndexMapping[predictedValue];
126
127          confusionMatrix[predictedIndex, targetIndex] += 1;
128        }
129
130        for (int row = 0; row < confusionMatrix.GetLength(0); row++) {
131          for (int col = 0; col < confusionMatrix.GetLength(1); col++) {
132            //TODO add scaling to relative values;
133            dataGridView[col, row].Value = confusionMatrix[row, col];
134          }
135        }
136      }
137    }
138
139    private void cmbSamples_SelectedIndexChanged(object sender, System.EventArgs e) {
140      FillDataGridView();
141    }
142  }
143}
Note: See TracBrowser for help on using the repository browser.