Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 14674 was 14255, checked in by pfleck, 8 years ago

#2632

  • Added the name of the target variable in plots and charts (scatter, line, ...).
  • Renamed MathSymbolicDataAnalysisModelView and added two subclasses for regression and classification that shows the name of the target variable in the equation. (added and used a new Format method to the LatexFormatter that uses the actual target name when encountering the StartSymbol)
File size: 6.1 KB
RevLine 
[4417]1#region License Information
2/* HeuristicLab
[14185]3 * Copyright (C) 2002-2016 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;
[14255]78          dataGridView.TopLeftHeaderCell.Value = string.Empty;
[4417]79        } else {
[6520]80          dataGridView.ColumnCount = Content.ProblemData.Classes + 1;
81          dataGridView.RowCount = Content.ProblemData.Classes + 1;
[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          }
[6520]89          dataGridView.Columns[i].HeaderText = "Actual not classified";
90          dataGridView.Rows[i].HeaderCell.Value = "Predicted not classified";
91
[4417]92          dataGridView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.ColumnHeader);
93          dataGridView.AutoResizeRowHeadersWidth(DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders);
94
[14255]95          dataGridView.TopLeftHeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
96          dataGridView.TopLeftHeaderCell.Value = Content.Model.TargetVariable;
97
[4417]98          FillDataGridView();
99        }
100      }
101    }
102
103    private void FillDataGridView() {
104      if (InvokeRequired) Invoke((Action)FillDataGridView);
105      else {
106        if (Content == null) return;
107
[6520]108        double[,] confusionMatrix = new double[Content.ProblemData.Classes + 1, Content.ProblemData.Classes + 1];
[4469]109        IEnumerable<int> rows;
[4417]110
[6239]111        double[] predictedValues;
[4417]112        if (cmbSamples.SelectedItem.ToString() == TrainingSamples) {
[8139]113          rows = Content.ProblemData.TrainingIndices;
[6239]114          predictedValues = Content.EstimatedTrainingClassValues.ToArray();
[4417]115        } else if (cmbSamples.SelectedItem.ToString() == TestSamples) {
[8139]116          rows = Content.ProblemData.TestIndices;
[6520]117          predictedValues = Content.EstimatedTestClassValues.ToArray();
[4417]118        } else throw new InvalidOperationException();
119
[6740]120        double[] targetValues = Content.ProblemData.Dataset.GetDoubleValues(Content.ProblemData.TargetVariable, rows).ToArray();
[6239]121
[4417]122        Dictionary<double, int> classValueIndexMapping = new Dictionary<double, int>();
123        int index = 0;
[5664]124        foreach (double classValue in Content.ProblemData.ClassValues.OrderBy(x => x)) {
[4417]125          classValueIndexMapping.Add(classValue, index);
126          index++;
127        }
128
129        for (int i = 0; i < targetValues.Length; i++) {
130          double targetValue = targetValues[i];
131          double predictedValue = predictedValues[i];
[6520]132          int targetIndex;
133          int predictedIndex;
134          if (!classValueIndexMapping.TryGetValue(targetValue, out targetIndex)) {
135            targetIndex = Content.ProblemData.Classes;
136          }
137          if (!classValueIndexMapping.TryGetValue(predictedValue, out predictedIndex)) {
138            predictedIndex = Content.ProblemData.Classes;
139          }
[4417]140
141          confusionMatrix[predictedIndex, targetIndex] += 1;
142        }
143
144        for (int row = 0; row < confusionMatrix.GetLength(0); row++) {
145          for (int col = 0; col < confusionMatrix.GetLength(1); col++) {
146            //TODO add scaling to relative values;
147            dataGridView[col, row].Value = confusionMatrix[row, col];
148          }
149        }
150      }
151    }
152
153    private void cmbSamples_SelectedIndexChanged(object sender, System.EventArgs e) {
154      FillDataGridView();
155    }
156  }
157}
Note: See TracBrowser for help on using the repository browser.