Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis.Views/3.4/Classification/ClassificationSolutionComparisonView.cs @ 13780

Last change on this file since 13780 was 13104, checked in by gkronber, 9 years ago

#1998: fixed a problem in Classification/ClassificationSolutionComparisonView

File size: 6.8 KB
RevLine 
[9119]1#region License Information
2/* HeuristicLab
[13098]3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[9119]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.Algorithms.DataAnalysis;
27using HeuristicLab.MainForm;
28using HeuristicLab.Problems.DataAnalysis.OnlineCalculators;
29
30namespace HeuristicLab.Problems.DataAnalysis.Views.Classification {
[13102]31  [View("Solution Comparison")]
[9119]32  [Content(typeof(IClassificationSolution))]
33  public partial class ClassificationSolutionComparisonView : DataAnalysisSolutionEvaluationView {
[9135]34    private List<IClassificationSolution> solutions;
35
[9119]36    public ClassificationSolutionComparisonView() {
37      InitializeComponent();
38    }
39
40    public new IClassificationSolution Content {
41      get { return (IClassificationSolution)base.Content; }
42      set { base.Content = value; }
43    }
44
45    protected override void RegisterContentEvents() {
46      base.RegisterContentEvents();
47      Content.ModelChanged += new EventHandler(Content_ModelChanged);
48      Content.ProblemDataChanged += new EventHandler(Content_ProblemDataChanged);
49    }
50    protected override void DeregisterContentEvents() {
51      base.DeregisterContentEvents();
52      Content.ModelChanged -= new EventHandler(Content_ModelChanged);
53      Content.ProblemDataChanged -= new EventHandler(Content_ProblemDataChanged);
54    }
55
56    protected virtual void Content_ModelChanged(object sender, EventArgs e) {
57      if (InvokeRequired) Invoke((Action<object, EventArgs>)Content_ModelChanged, sender, e);
58      else UpdateDataGridView();
59    }
60    protected virtual void Content_ProblemDataChanged(object sender, EventArgs e) {
61      if (InvokeRequired) Invoke((Action<object, EventArgs>)Content_ProblemDataChanged, sender, e);
62      else {
63        UpdateDataGridView();
64      }
65    }
66    protected override void OnContentChanged() {
67      base.OnContentChanged();
68      UpdateDataGridView();
69    }
70
71    private void UpdateDataGridView() {
72      if (InvokeRequired) {
73        Invoke((Action)UpdateDataGridView);
74      } else {
75        if (Content == null) {
76          dataGridView.Rows.Clear();
77          dataGridView.Columns.Clear();
[13104]78          solutions = null;
[9119]79        } else {
80
81          IClassificationProblemData problemData = Content.ProblemData;
[13086]82          var dataset = problemData.Dataset;
[9135]83          solutions = new List<IClassificationSolution>() { Content };
[9119]84          solutions.AddRange(GenerateClassificationSolutions(problemData));
85
86          dataGridView.ColumnCount = 4;
87          dataGridView.RowCount = solutions.Count();
[13089]88          dataGridView.Columns[0].HeaderText = "Accuracy (training)";
89          dataGridView.Columns[1].HeaderText = "Accuracy (test)";
90          dataGridView.Columns[2].HeaderText = "Matthews Correlation Coefficient (training)";
91          dataGridView.Columns[3].HeaderText = "Matthews Correlation Coefficient (test)";
[9119]92          if (problemData.Classes == 2) {
93            dataGridView.ColumnCount = 6;
[13089]94            dataGridView.Columns[4].HeaderText = "F1 Score (training)";
95            dataGridView.Columns[5].HeaderText = "F1 Score (test)";
[9119]96          }
97
98          for (int row = 0; row < solutions.Count; row++) {
99            var solution = solutions[row];
100
101            dataGridView.Rows[row].HeaderCell.Value = solution.Name;
102            dataGridView[0, row].Value = solution.TrainingAccuracy;
103            dataGridView[1, row].Value = solution.TestAccuracy;
104
105            var trainingIndizes = problemData.TrainingIndices;
106            var originalTrainingValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, trainingIndizes);
107            var estimatedTrainingValues = solution.Model.GetEstimatedClassValues(dataset, trainingIndizes);
108
[10560]109            var testIndices = problemData.TestIndices;
[9119]110            var originalTestValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, testIndices);
111            var estimatedTestValues = solution.Model.GetEstimatedClassValues(dataset, testIndices);
112
113            OnlineCalculatorError errorState;
114            dataGridView[2, row].Value = MatthewsCorrelationCoefficientCalculator.Calculate(originalTrainingValues, estimatedTrainingValues, out errorState);
115            dataGridView[3, row].Value = MatthewsCorrelationCoefficientCalculator.Calculate(originalTestValues, estimatedTestValues, out errorState);
116            if (problemData.Classes == 2) {
117              dataGridView[4, row].Value = FOneScoreCalculator.Calculate(originalTrainingValues, estimatedTrainingValues, out errorState);
118              dataGridView[5, row].Value = FOneScoreCalculator.Calculate(originalTestValues, estimatedTestValues, out errorState);
119            }
120          }
121
122          dataGridView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.ColumnHeader);
123          dataGridView.AutoResizeRowHeadersWidth(DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders);
124        }
125      }
126    }
127
128    private IEnumerable<IClassificationSolution> GenerateClassificationSolutions(IClassificationProblemData problemData) {
[10560]129      var newSolutions = new List<IClassificationSolution>();
[9119]130      var zeroR = ZeroR.CreateZeroRSolution(problemData);
[13089]131      zeroR.Name = "ZeroR Classification Solution";
[10560]132      newSolutions.Add(zeroR);
[13091]133      var oneR = OneR.CreateOneRSolution(problemData);
[13089]134      oneR.Name = "OneR Classification Solution";
[10560]135      newSolutions.Add(oneR);
[9135]136      try {
137        var lda = LinearDiscriminantAnalysis.CreateLinearDiscriminantAnalysisSolution(problemData);
138        lda.Name = "Linear Discriminant Analysis Solution";
[10560]139        newSolutions.Add(lda);
[13104]140      } catch (NotSupportedException) { } catch (ArgumentException) { }
[10560]141      return newSolutions;
[9119]142    }
[9135]143
144    private void dataGridView_MouseDoubleClick(object sender, MouseEventArgs e) {
145      var hittestinfo = dataGridView.HitTest(e.X, e.Y);
146      if (hittestinfo.Type != DataGridViewHitTestType.RowHeader) { return; }
147      if (hittestinfo.RowIndex > solutions.Count) { return; }
148
149      MainFormManager.MainForm.ShowContent(solutions[hittestinfo.RowIndex]);
150    }
[9119]151  }
152}
Note: See TracBrowser for help on using the repository browser.