[5678] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2011 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 | using System;
|
---|
[6672] | 22 | using System.Collections.Generic;
|
---|
[5678] | 23 | using System.Linq;
|
---|
| 24 | using System.Windows.Forms;
|
---|
| 25 | using HeuristicLab.Data;
|
---|
| 26 | using HeuristicLab.MainForm;
|
---|
| 27 | using HeuristicLab.MainForm.WindowsForms;
|
---|
| 28 |
|
---|
| 29 | namespace HeuristicLab.Problems.DataAnalysis.Views {
|
---|
[5975] | 30 | [View("Estimated Class Values")]
|
---|
[6672] | 31 | [Content(typeof(ClassificationEnsembleSolution))]
|
---|
| 32 | public partial class ClassificationEnsembleSolutionEstimatedClassValuesView :
|
---|
| 33 | ClassificationSolutionEstimatedClassValuesView {
|
---|
| 34 | private const string RowColumnName = "Row";
|
---|
| 35 | private const string TargetClassValuesColumnName = "TargetVariable";
|
---|
| 36 | private const string EstimatedClassValuesColumnName = "EstimatedClassValues";
|
---|
| 37 | private const string ConfidenceColumnName = "Confidence";
|
---|
[5678] | 38 |
|
---|
[6672] | 39 | private const string SamplesComboBoxAllSamples = "All Samples";
|
---|
| 40 | private const string SamplesComboBoxTrainingSamples = "Training Samples";
|
---|
| 41 | private const string SamplesComboBoxTestSamples = "Test Samples";
|
---|
| 42 |
|
---|
| 43 | public new ClassificationEnsembleSolution Content {
|
---|
| 44 | get { return (ClassificationEnsembleSolution)base.Content; }
|
---|
[6642] | 45 | set { base.Content = value; }
|
---|
[5678] | 46 | }
|
---|
| 47 |
|
---|
[6672] | 48 | public ClassificationEnsembleSolutionEstimatedClassValuesView()
|
---|
[5678] | 49 | : base() {
|
---|
| 50 | InitializeComponent();
|
---|
[6672] | 51 | SamplesComboBox.Items.AddRange(new string[] { SamplesComboBoxAllSamples, SamplesComboBoxTrainingSamples, SamplesComboBoxTestSamples });
|
---|
| 52 | SamplesComboBox.SelectedIndex = 0;
|
---|
[5678] | 53 | }
|
---|
| 54 |
|
---|
[6672] | 55 | private void SamplesComboBox_SelectedIndexChanged(object sender, EventArgs e) {
|
---|
| 56 | UpdateEstimatedValues();
|
---|
| 57 | }
|
---|
| 58 |
|
---|
[6642] | 59 | protected override void UpdateEstimatedValues() {
|
---|
[6672] | 60 | if (InvokeRequired) {
|
---|
| 61 | Invoke((Action)UpdateEstimatedValues);
|
---|
| 62 | return;
|
---|
| 63 | }
|
---|
| 64 | if (Content == null) {
|
---|
| 65 | matrixView.Content = null;
|
---|
| 66 | return;
|
---|
| 67 | }
|
---|
[5678] | 68 |
|
---|
[6672] | 69 | int[] indizes;
|
---|
| 70 | double[] estimatedClassValues;
|
---|
| 71 |
|
---|
| 72 | switch (SamplesComboBox.SelectedItem.ToString()) {
|
---|
| 73 | case SamplesComboBoxAllSamples: {
|
---|
| 74 | indizes = Enumerable.Range(0, Content.ProblemData.Dataset.Rows).ToArray();
|
---|
| 75 | estimatedClassValues = Content.EstimatedClassValues.ToArray();
|
---|
| 76 | break;
|
---|
[5678] | 77 | }
|
---|
[6672] | 78 | case SamplesComboBoxTrainingSamples: {
|
---|
| 79 | indizes = Content.ProblemData.TrainingIndizes.ToArray();
|
---|
| 80 | estimatedClassValues = Content.EstimatedTrainingClassValues.ToArray();
|
---|
| 81 | break;
|
---|
| 82 | }
|
---|
| 83 | case SamplesComboBoxTestSamples: {
|
---|
| 84 | indizes = Content.ProblemData.TestIndizes.ToArray();
|
---|
| 85 | estimatedClassValues = Content.EstimatedTestClassValues.ToArray();
|
---|
| 86 | break;
|
---|
| 87 | }
|
---|
| 88 | default:
|
---|
| 89 | throw new ArgumentException();
|
---|
| 90 | }
|
---|
[5678] | 91 |
|
---|
[6672] | 92 | int classValuesCount = Content.ProblemData.ClassValues.Count;
|
---|
| 93 | int modelCount = Content.Model.Models.Count();
|
---|
| 94 | string[,] values = new string[indizes.Length, 4 + classValuesCount + modelCount];
|
---|
| 95 | double[] target = Content.ProblemData.Dataset.GetVariableValues(Content.ProblemData.TargetVariable);
|
---|
| 96 | List<List<double?>> estimatedValuesVector = GetEstimatedValues(SamplesComboBox.SelectedItem.ToString(), indizes,
|
---|
| 97 | Content.ClassificationSolutions);
|
---|
| 98 |
|
---|
| 99 | for (int i = 0; i < indizes.Length; i++) {
|
---|
| 100 | int row = indizes[i];
|
---|
| 101 | values[i, 0] = row.ToString();
|
---|
| 102 | values[i, 1] = target[i].ToString();
|
---|
| 103 | values[i, 2] = estimatedClassValues[i].ToString();
|
---|
| 104 | var groups = estimatedValuesVector[i].GroupBy(x => x).Select(g => new { Key = g.Key, Count = g.Count() }).ToList();
|
---|
| 105 | var estimationCount = groups.Where(g => g.Key != null).Select(g => g.Count).Sum();
|
---|
| 106 | values[i, 3] = (((double)groups.Where(g => g.Key == estimatedClassValues[i]).Single().Count) / estimationCount).ToString();
|
---|
| 107 | for (int classIndex = 0; classIndex < Content.ProblemData.ClassValues.Count; classIndex++) {
|
---|
| 108 | var group = groups.Where(g => g.Key == Content.ProblemData.ClassValues[classIndex]).SingleOrDefault();
|
---|
| 109 | if (group == null) values[i, 4 + classIndex] = 0.ToString();
|
---|
| 110 | else values[i, 4 + classIndex] = group.Count.ToString();
|
---|
[5678] | 111 | }
|
---|
[6672] | 112 | for (int modelIndex = 0; modelIndex < estimatedValuesVector[i].Count; modelIndex++) {
|
---|
| 113 | values[i, 4 + classValuesCount + modelIndex] = estimatedValuesVector[i][modelIndex] == null
|
---|
| 114 | ? string.Empty
|
---|
| 115 | : estimatedValuesVector[i][modelIndex].ToString();
|
---|
| 116 | }
|
---|
| 117 |
|
---|
[5678] | 118 | }
|
---|
[6672] | 119 |
|
---|
| 120 | StringMatrix matrix = new StringMatrix(values);
|
---|
| 121 | List<string> columnNames = new List<string>() { "Id", TargetClassValuesColumnName, EstimatedClassValuesColumnName, ConfidenceColumnName };
|
---|
| 122 | columnNames.AddRange(Content.ProblemData.ClassNames);
|
---|
| 123 | columnNames.AddRange(Content.Model.Models.Select(m => m.Name));
|
---|
| 124 | matrix.ColumnNames = columnNames;
|
---|
| 125 | matrix.SortableView = true;
|
---|
| 126 | matrixView.Content = matrix;
|
---|
[5678] | 127 | }
|
---|
[6672] | 128 |
|
---|
| 129 | private List<List<double?>> GetEstimatedValues(string samplesSelection, int[] rows, IEnumerable<IClassificationSolution> solutions) {
|
---|
| 130 | List<List<double?>> values = new List<List<double?>>();
|
---|
| 131 | int solutionIndex = 0;
|
---|
| 132 | foreach (var solution in solutions) {
|
---|
| 133 | double[] estimation = solution.GetEstimatedClassValues(rows).ToArray();
|
---|
| 134 | for (int i = 0; i < rows.Length; i++) {
|
---|
| 135 | var row = rows[i];
|
---|
| 136 | if (solutionIndex == 0) values.Add(new List<double?>());
|
---|
| 137 |
|
---|
| 138 | if (samplesSelection == SamplesComboBoxAllSamples)
|
---|
| 139 | values[i].Add(estimation[i]);
|
---|
| 140 | else if (samplesSelection == SamplesComboBoxTrainingSamples && solution.ProblemData.IsTrainingSample(row))
|
---|
| 141 | values[i].Add(estimation[i]);
|
---|
| 142 | else if (samplesSelection == SamplesComboBoxTestSamples && solution.ProblemData.IsTestSample(row))
|
---|
| 143 | values[i].Add(estimation[i]);
|
---|
| 144 | else
|
---|
| 145 | values[i].Add(null);
|
---|
| 146 | }
|
---|
| 147 | solutionIndex++;
|
---|
| 148 | }
|
---|
| 149 | return values;
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 |
|
---|
| 153 |
|
---|
[5678] | 154 | }
|
---|
| 155 | }
|
---|