Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 12901 was 12012, checked in by ascheibe, 10 years ago

#2212 merged r12008, r12009, r12010 back into trunk

File size: 5.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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.MainForm;
27using HeuristicLab.MainForm.WindowsForms;
28
29namespace HeuristicLab.Problems.DataAnalysis.Views {
30  [View("Confusion Matrix")]
31  [Content(typeof(IClassificationSolution))]
32  public partial class ClassificationSolutionConfusionMatrixView : DataAnalysisSolutionEvaluationView {
33    private const string TrainingSamples = "Training";
34    private const string TestSamples = "Test";
35    public ClassificationSolutionConfusionMatrixView() {
36      InitializeComponent();
37      cmbSamples.Items.Add(TrainingSamples);
38      cmbSamples.Items.Add(TestSamples);
39      cmbSamples.SelectedIndex = 0;
40    }
41
42    public new IClassificationSolution Content {
43      get { return (IClassificationSolution)base.Content; }
44      set { base.Content = value; }
45    }
46
47    protected override void RegisterContentEvents() {
48      base.RegisterContentEvents();
49      Content.ModelChanged += new EventHandler(Content_ModelChanged);
50      Content.ProblemDataChanged += new EventHandler(Content_ProblemDataChanged);
51    }
52
53
54    protected override void DeregisterContentEvents() {
55      base.DeregisterContentEvents();
56      Content.ModelChanged -= new EventHandler(Content_ModelChanged);
57      Content.ProblemDataChanged -= new EventHandler(Content_ProblemDataChanged);
58    }
59
60    private void Content_ModelChanged(object sender, EventArgs e) {
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 {
79          dataGridView.ColumnCount = Content.ProblemData.Classes + 1;
80          dataGridView.RowCount = Content.ProblemData.Classes + 1;
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          }
88          dataGridView.Columns[i].HeaderText = "Actual not classified";
89          dataGridView.Rows[i].HeaderCell.Value = "Predicted not classified";
90
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
104        double[,] confusionMatrix = new double[Content.ProblemData.Classes + 1, Content.ProblemData.Classes + 1];
105        IEnumerable<int> rows;
106
107        double[] predictedValues;
108        if (cmbSamples.SelectedItem.ToString() == TrainingSamples) {
109          rows = Content.ProblemData.TrainingIndices;
110          predictedValues = Content.EstimatedTrainingClassValues.ToArray();
111        } else if (cmbSamples.SelectedItem.ToString() == TestSamples) {
112          rows = Content.ProblemData.TestIndices;
113          predictedValues = Content.EstimatedTestClassValues.ToArray();
114        } else throw new InvalidOperationException();
115
116        double[] targetValues = Content.ProblemData.Dataset.GetDoubleValues(Content.ProblemData.TargetVariable, rows).ToArray();
117
118        Dictionary<double, int> classValueIndexMapping = new Dictionary<double, int>();
119        int index = 0;
120        foreach (double classValue in Content.ProblemData.ClassValues.OrderBy(x => x)) {
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];
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          }
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.