Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1450 implemented menu item to merge all data analysis solutions in a run collection into ensemble solutions and fixed a few bugs related to ensemble solutions.

File size: 5.9 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 + 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          FillDataGridView();
96        }
97      }
98    }
99
100    private void FillDataGridView() {
101      if (InvokeRequired) Invoke((Action)FillDataGridView);
102      else {
103        if (Content == null) return;
104
105        double[,] confusionMatrix = new double[Content.ProblemData.Classes + 1, Content.ProblemData.Classes + 1];
106        IEnumerable<int> rows;
107
108        double[] predictedValues;
109        if (cmbSamples.SelectedItem.ToString() == TrainingSamples) {
110          rows = Content.ProblemData.TrainingIndizes;
111          predictedValues = Content.EstimatedTrainingClassValues.ToArray();
112        } else if (cmbSamples.SelectedItem.ToString() == TestSamples) {
113          rows = Content.ProblemData.TestIndizes;
114          predictedValues = Content.EstimatedTestClassValues.ToArray();
115        } else throw new InvalidOperationException();
116
117        double[] targetValues = Content.ProblemData.Dataset.GetEnumeratedVariableValues(Content.ProblemData.TargetVariable, rows).ToArray();
118
119        Dictionary<double, int> classValueIndexMapping = new Dictionary<double, int>();
120        int index = 0;
121        foreach (double classValue in Content.ProblemData.ClassValues.OrderBy(x => x)) {
122          classValueIndexMapping.Add(classValue, index);
123          index++;
124        }
125
126        for (int i = 0; i < targetValues.Length; i++) {
127          double targetValue = targetValues[i];
128          double predictedValue = predictedValues[i];
129          int targetIndex;
130          int predictedIndex;
131          if (!classValueIndexMapping.TryGetValue(targetValue, out targetIndex)) {
132            targetIndex = Content.ProblemData.Classes;
133          }
134          if (!classValueIndexMapping.TryGetValue(predictedValue, out predictedIndex)) {
135            predictedIndex = Content.ProblemData.Classes;
136          }
137
138          confusionMatrix[predictedIndex, targetIndex] += 1;
139        }
140
141        for (int row = 0; row < confusionMatrix.GetLength(0); row++) {
142          for (int col = 0; col < confusionMatrix.GetLength(1); col++) {
143            //TODO add scaling to relative values;
144            dataGridView[col, row].Value = confusionMatrix[row, col];
145          }
146        }
147      }
148    }
149
150    private void cmbSamples_SelectedIndexChanged(object sender, System.EventArgs e) {
151      FillDataGridView();
152    }
153  }
154}
Note: See TracBrowser for help on using the repository browser.