Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 6239 was 6239, checked in by gkronber, 12 years ago

#1450: implemented support for ensemble solutions for classification.

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