Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ClassificationEnsembleVoting/HeuristicLab.Problems.DataAnalysis.Views/3.4/Classification/ClassificationEnsembleSolutionConfidenceAccuracyDependence.cs @ 8297

Last change on this file since 8297 was 8297, checked in by sforsten, 12 years ago

#1776:

  • Corrected namespace of IClassificationEnsembleSolutionWeightCalculator interface
  • Corrected calculation of confidence for test and training samples in ClassificationEnsembleSolutionEstimatedClassValuesView
  • Added overload method GetConfidence to IClassificationEnsembleSolutionWeightCalculator to calculate more than one point at a time (maybe additional methods for training and test confidence could improve the performance remarkably)
  • Added ClassificationEnsembleSolutionConfidenceAccuracyDependence to see how accuracy would behave, if only samples with high confidence would be classified
File size: 8.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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 System.Windows.Forms.DataVisualization.Charting;
27using HeuristicLab.Data;
28using HeuristicLab.MainForm;
29using HeuristicLab.Problems.DataAnalysis.Interfaces;
30
31namespace HeuristicLab.Problems.DataAnalysis.Views {
32  [View("Confidence Accuracy Dependence")]
33  [Content(typeof(IClassificationEnsembleSolution))]
34  public partial class ClassificationEnsembleSolutionConfidenceAccuracyDependence : DataAnalysisSolutionEvaluationView {
35    private const string CONFIDENCE = "Confidence";
36    private const string ACCURACY = "Accuracy";
37    private const string COVEREDPERCENTAGE = "Covered percentage";
38
39    private const string SamplesComboBoxAllSamples = "All Samples";
40    private const string SamplesComboBoxTrainingSamples = "Training Samples";
41    private const string SamplesComboBoxTestSamples = "Test Samples";
42
43    // zero is also a point
44    private const int maxPoints = 101;
45
46    public new ClassificationEnsembleSolution Content {
47      get { return (ClassificationEnsembleSolution)base.Content; }
48      set { base.Content = value; }
49    }
50
51    public ClassificationEnsembleSolutionConfidenceAccuracyDependence()
52      : base() {
53      InitializeComponent();
54
55      SamplesComboBox.Items.AddRange(new string[] { SamplesComboBoxAllSamples, SamplesComboBoxTrainingSamples, SamplesComboBoxTestSamples });
56      SamplesComboBox.SelectedIndex = 0;
57      //configure axis
58      this.chart.CustomizeAllChartAreas();
59      this.chart.ChartAreas[0].CursorX.IsUserSelectionEnabled = true;
60      this.chart.ChartAreas[0].AxisX.ScaleView.Zoomable = true;
61      this.chart.ChartAreas[0].AxisX.IsStartedFromZero = true;
62      this.chart.ChartAreas[0].AxisX.Minimum = 0;
63      this.chart.ChartAreas[0].AxisX.Maximum = 1;
64
65      this.chart.ChartAreas[0].CursorY.IsUserSelectionEnabled = true;
66      this.chart.ChartAreas[0].AxisY.ScaleView.Zoomable = true;
67      this.chart.ChartAreas[0].AxisY.IsStartedFromZero = true;
68      this.chart.ChartAreas[0].AxisY.Minimum = 0;
69      this.chart.ChartAreas[0].AxisY.Maximum = 1;
70    }
71
72    private void RedrawChart() {
73      this.chart.Series.Clear();
74      if (Content != null) {
75
76        Dictionary<string, double[]> points = new Dictionary<string, double[]>();
77        points.Add(CONFIDENCE, new double[maxPoints]);
78        points.Add(ACCURACY, new double[maxPoints]);
79        points.Add(COVEREDPERCENTAGE, new double[maxPoints]);
80
81        IClassificationEnsembleSolutionWeightCalculator weightCalc = Content.WeightCalculator;
82        var solutions = Content.ClassificationSolutions;
83        double[] estimatedClassValues;
84        double[] classValues;
85        OnlineAccuracyCalculator accuracyCalc = new OnlineAccuracyCalculator();
86
87        int rows;
88        double[] confidences;
89
90        if (SamplesComboBox.SelectedItem.ToString().Equals(SamplesComboBoxAllSamples)) {
91          rows = Content.ProblemData.Dataset.Rows;
92          estimatedClassValues = Content.EstimatedClassValues.ToArray();
93          classValues = Content.ProblemData.Dataset.GetDoubleValues(Content.ProblemData.TargetVariable).ToArray();
94          confidences = weightCalc.GetConfidence(solutions, Enumerable.Range(0, rows), estimatedClassValues).ToArray();
95        } else {
96          IntRange range;
97          if (SamplesComboBox.SelectedItem.ToString().Equals(SamplesComboBoxTrainingSamples)) {
98            range = Content.ProblemData.TrainingPartition;
99            estimatedClassValues = Content.EstimatedTrainingClassValues.ToArray();
100          } else if (SamplesComboBox.SelectedItem.ToString().Equals(SamplesComboBoxTestSamples)) {
101            range = Content.ProblemData.TestPartition;
102            estimatedClassValues = Content.EstimatedTestClassValues.ToArray();
103          } else {
104            return;
105          }
106          rows = range.End - range.Start;
107          classValues = Content.ProblemData.Dataset.GetDoubleValues(Content.ProblemData.TargetVariable)
108                                      .Skip(range.Start).Take(range.End - range.Start).ToArray();
109          confidences = new double[rows];
110          int index;
111          for (int i = 0; i < rows; i++) {
112            index = range.Start + i;
113            confidences[i] = weightCalc.GetConfidence(GetRelevantSolutions(SamplesComboBox.SelectedItem.ToString(), solutions, index),
114                                                      index, estimatedClassValues[i]);
115          }
116        }
117
118        for (int i = 0; i < maxPoints; i++) {
119          double confidenceValue = (1.0 / (maxPoints - 1)) * i;
120          int notCovered = 0;
121
122          for (int j = 0; j < rows; j++) {
123            if (confidences[j] >= confidenceValue) {
124              accuracyCalc.Add(classValues[j], estimatedClassValues[j]);
125            } else {
126              notCovered++;
127            }
128          }
129
130          points[CONFIDENCE][i] = confidenceValue;
131          points[ACCURACY][i] = accuracyCalc.Accuracy;
132          points[COVEREDPERCENTAGE][i] = (double)notCovered / (double)rows;
133          accuracyCalc.Reset();
134        }
135
136        this.chart.Series.Add(CONFIDENCE);
137        this.chart.Series[CONFIDENCE].LegendText = CONFIDENCE;
138        this.chart.Series[CONFIDENCE].ChartType = SeriesChartType.FastLine;
139        this.chart.Series[CONFIDENCE].Points.DataBindXY(points[CONFIDENCE], points[CONFIDENCE]);
140
141        this.chart.Series.Add(ACCURACY);
142        this.chart.Series[ACCURACY].LegendText = ACCURACY;
143        this.chart.Series[ACCURACY].ChartType = SeriesChartType.FastLine;
144        this.chart.Series[ACCURACY].Points.DataBindXY(points[CONFIDENCE], points[ACCURACY]);
145
146        this.chart.Series.Add(COVEREDPERCENTAGE);
147        this.chart.Series[COVEREDPERCENTAGE].LegendText = COVEREDPERCENTAGE;
148        this.chart.Series[COVEREDPERCENTAGE].ChartType = SeriesChartType.FastLine;
149        this.chart.Series[COVEREDPERCENTAGE].Points.DataBindXY(points[CONFIDENCE], points[COVEREDPERCENTAGE]);
150      }
151    }
152
153    protected IEnumerable<IClassificationSolution> GetRelevantSolutions(string samplesSelection, IEnumerable<IClassificationSolution> solutions, int curRow) {
154      if (samplesSelection == SamplesComboBoxAllSamples)
155        return solutions;
156      else if (samplesSelection == SamplesComboBoxTrainingSamples)
157        return solutions.Where(s => s.ProblemData.IsTrainingSample(curRow));
158      else if (samplesSelection == SamplesComboBoxTestSamples)
159        return solutions.Where(s => s.ProblemData.IsTestSample(curRow));
160      else
161        return new List<IClassificationSolution>();
162    }
163
164    #region events
165    protected override void RegisterContentEvents() {
166      base.RegisterContentEvents();
167      Content.ModelChanged += new EventHandler(Content_ModelChanged);
168      Content.ProblemDataChanged += new EventHandler(Content_ProblemDataChanged);
169    }
170    protected override void DeregisterContentEvents() {
171      base.DeregisterContentEvents();
172      Content.ModelChanged -= new EventHandler(Content_ModelChanged);
173      Content.ProblemDataChanged -= new EventHandler(Content_ProblemDataChanged);
174    }
175
176    protected override void OnContentChanged() {
177      base.OnContentChanged();
178      RedrawChart();
179    }
180    private void Content_ProblemDataChanged(object sender, EventArgs e) {
181      RedrawChart();
182    }
183    private void Content_ModelChanged(object sender, EventArgs e) {
184      RedrawChart();
185    }
186    private void SamplesComboBox_SelectedIndexChanged(object sender, EventArgs e) {
187      RedrawChart();
188    }
189    #endregion
190  }
191}
Note: See TracBrowser for help on using the repository browser.