Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 11301 was 11171, checked in by ascheibe, 10 years ago

#2115 merged r11170 (copyright update) into trunk

File size: 5.8 KB
RevLine 
[4417]1#region License Information
2/* HeuristicLab
[11171]3 * Copyright (C) 2002-2014 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;
26using HeuristicLab.MainForm;
27using HeuristicLab.MainForm.WindowsForms;
28
[5829]29namespace HeuristicLab.Problems.DataAnalysis.Views {
[5975]30  [View("Confusion Matrix")]
[5664]31  [Content(typeof(IClassificationSolution))]
[6642]32  public partial class ClassificationSolutionConfusionMatrixView : DataAnalysisSolutionEvaluationView {
[4417]33    private const string TrainingSamples = "Training";
34    private const string TestSamples = "Test";
[5664]35    public ClassificationSolutionConfusionMatrixView() {
[4417]36      InitializeComponent();
37      cmbSamples.Items.Add(TrainingSamples);
38      cmbSamples.Items.Add(TestSamples);
39      cmbSamples.SelectedIndex = 0;
40    }
41
[5664]42    public new IClassificationSolution Content {
43      get { return (IClassificationSolution)base.Content; }
[4417]44      set { base.Content = value; }
45    }
46
47    protected override void RegisterContentEvents() {
48      base.RegisterContentEvents();
[5664]49      Content.ModelChanged += new EventHandler(Content_ModelChanged);
[4417]50      Content.ProblemDataChanged += new EventHandler(Content_ProblemDataChanged);
51    }
52
53
54    protected override void DeregisterContentEvents() {
55      base.DeregisterContentEvents();
[5664]56      Content.ModelChanged -= new EventHandler(Content_ModelChanged);
[4417]57      Content.ProblemDataChanged -= new EventHandler(Content_ProblemDataChanged);
58    }
59
[5664]60    private void Content_ModelChanged(object sender, EventArgs e) {
[4417]61      FillDataGridView();
62    }
63    private void Content_ProblemDataChanged(object sender, EventArgs e) {
64      UpdateDataGridView();
65    }
66
67    protected override void OnContentChanged() {
68      base.OnContentChanged();
69      UpdateDataGridView();
70    }
71
72    private void UpdateDataGridView() {
73      if (InvokeRequired) Invoke((Action)UpdateDataGridView);
74      else {
75        if (Content == null) {
76          dataGridView.RowCount = 1;
77          dataGridView.ColumnCount = 1;
78        } else {
[6520]79          dataGridView.ColumnCount = Content.ProblemData.Classes + 1;
80          dataGridView.RowCount = Content.ProblemData.Classes + 1;
[4417]81
82          int i = 0;
83          foreach (string headerText in Content.ProblemData.ClassNames) {
84            dataGridView.Columns[i].HeaderText = "Actual " + headerText;
85            dataGridView.Rows[i].HeaderCell.Value = "Predicted " + headerText;
86            i++;
87          }
[6520]88          dataGridView.Columns[i].HeaderText = "Actual not classified";
89          dataGridView.Rows[i].HeaderCell.Value = "Predicted not classified";
90
[4417]91          dataGridView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.ColumnHeader);
92          dataGridView.AutoResizeRowHeadersWidth(DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders);
93
94          FillDataGridView();
95        }
96      }
97    }
98
99    private void FillDataGridView() {
100      if (InvokeRequired) Invoke((Action)FillDataGridView);
101      else {
102        if (Content == null) return;
103
[6520]104        double[,] confusionMatrix = new double[Content.ProblemData.Classes + 1, Content.ProblemData.Classes + 1];
[4469]105        IEnumerable<int> rows;
[4417]106
[6239]107        double[] predictedValues;
[4417]108        if (cmbSamples.SelectedItem.ToString() == TrainingSamples) {
[8139]109          rows = Content.ProblemData.TrainingIndices;
[6239]110          predictedValues = Content.EstimatedTrainingClassValues.ToArray();
[4417]111        } else if (cmbSamples.SelectedItem.ToString() == TestSamples) {
[8139]112          rows = Content.ProblemData.TestIndices;
[6520]113          predictedValues = Content.EstimatedTestClassValues.ToArray();
[4417]114        } else throw new InvalidOperationException();
115
[6740]116        double[] targetValues = Content.ProblemData.Dataset.GetDoubleValues(Content.ProblemData.TargetVariable, rows).ToArray();
[6239]117
[4417]118        Dictionary<double, int> classValueIndexMapping = new Dictionary<double, int>();
119        int index = 0;
[5664]120        foreach (double classValue in Content.ProblemData.ClassValues.OrderBy(x => x)) {
[4417]121          classValueIndexMapping.Add(classValue, index);
122          index++;
123        }
124
125        for (int i = 0; i < targetValues.Length; i++) {
126          double targetValue = targetValues[i];
127          double predictedValue = predictedValues[i];
[6520]128          int targetIndex;
129          int predictedIndex;
130          if (!classValueIndexMapping.TryGetValue(targetValue, out targetIndex)) {
131            targetIndex = Content.ProblemData.Classes;
132          }
133          if (!classValueIndexMapping.TryGetValue(predictedValue, out predictedIndex)) {
134            predictedIndex = Content.ProblemData.Classes;
135          }
[4417]136
137          confusionMatrix[predictedIndex, targetIndex] += 1;
138        }
139
140        for (int row = 0; row < confusionMatrix.GetLength(0); row++) {
141          for (int col = 0; col < confusionMatrix.GetLength(1); col++) {
142            //TODO add scaling to relative values;
143            dataGridView[col, row].Value = confusionMatrix[row, col];
144          }
145        }
146      }
147    }
148
149    private void cmbSamples_SelectedIndexChanged(object sender, System.EventArgs e) {
150      FillDataGridView();
151    }
152  }
153}
Note: See TracBrowser for help on using the repository browser.