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, 13 years ago

#1450: implemented support for ensemble solutions for classification.

File size: 5.5 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;
[5829]26using HeuristicLab.Core.Views;
[4417]27using HeuristicLab.MainForm;
28using HeuristicLab.MainForm.WindowsForms;
29
[5829]30namespace HeuristicLab.Problems.DataAnalysis.Views {
[5975]31  [View("Confusion Matrix")]
[5664]32  [Content(typeof(IClassificationSolution))]
[5829]33  public partial class ClassificationSolutionConfusionMatrixView : ItemView, IClassificationSolutionEvaluationView {
[4417]34    private const string TrainingSamples = "Training";
35    private const string TestSamples = "Test";
[5664]36    public ClassificationSolutionConfusionMatrixView() {
[4417]37      InitializeComponent();
38      cmbSamples.Items.Add(TrainingSamples);
39      cmbSamples.Items.Add(TestSamples);
40      cmbSamples.SelectedIndex = 0;
41    }
42
[5664]43    public new IClassificationSolution Content {
44      get { return (IClassificationSolution)base.Content; }
[4417]45      set { base.Content = value; }
46    }
47
48    protected override void RegisterContentEvents() {
49      base.RegisterContentEvents();
[5664]50      Content.ModelChanged += new EventHandler(Content_ModelChanged);
[4417]51      Content.ProblemDataChanged += new EventHandler(Content_ProblemDataChanged);
52    }
53
54
55    protected override void DeregisterContentEvents() {
56      base.DeregisterContentEvents();
[5664]57      Content.ModelChanged -= new EventHandler(Content_ModelChanged);
[4417]58      Content.ProblemDataChanged -= new EventHandler(Content_ProblemDataChanged);
59    }
60
[5664]61    private void Content_ModelChanged(object sender, EventArgs e) {
[4417]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 {
[5664]80          dataGridView.ColumnCount = Content.ProblemData.Classes;
81          dataGridView.RowCount = Content.ProblemData.Classes;
[4417]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
[5664]102        double[,] confusionMatrix = new double[Content.ProblemData.Classes, Content.ProblemData.Classes];
[4469]103        IEnumerable<int> rows;
[4417]104
[6239]105        double[] predictedValues;
[4417]106        if (cmbSamples.SelectedItem.ToString() == TrainingSamples) {
[4469]107          rows = Content.ProblemData.TrainingIndizes;
[6239]108          predictedValues = Content.EstimatedTrainingClassValues.ToArray();
[4417]109        } else if (cmbSamples.SelectedItem.ToString() == TestSamples) {
[4469]110          rows = Content.ProblemData.TestIndizes;
[6239]111          predictedValues = Content.EstimatedTestClassValues.ToArray();         
[4417]112        } else throw new InvalidOperationException();
113
[6239]114        double[] targetValues = Content.ProblemData.Dataset.GetEnumeratedVariableValues(Content.ProblemData.TargetVariable, rows).ToArray();
115
[4417]116        Dictionary<double, int> classValueIndexMapping = new Dictionary<double, int>();
117        int index = 0;
[5664]118        foreach (double classValue in Content.ProblemData.ClassValues.OrderBy(x => x)) {
[4417]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.