[5678] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[11170] | 3 | * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[5678] | 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;
|
---|
[6680] | 23 | using System.Drawing;
|
---|
[5678] | 24 | using System.Linq;
|
---|
| 25 | using System.Windows.Forms;
|
---|
[7022] | 26 | using HeuristicLab.Common;
|
---|
[5678] | 27 | using HeuristicLab.Data;
|
---|
| 28 | using HeuristicLab.MainForm;
|
---|
| 29 | using HeuristicLab.MainForm.WindowsForms;
|
---|
| 30 |
|
---|
| 31 | namespace HeuristicLab.Problems.DataAnalysis.Views {
|
---|
[5975] | 32 | [View("Estimated Class Values")]
|
---|
[6672] | 33 | [Content(typeof(ClassificationEnsembleSolution))]
|
---|
| 34 | public partial class ClassificationEnsembleSolutionEstimatedClassValuesView :
|
---|
| 35 | ClassificationSolutionEstimatedClassValuesView {
|
---|
| 36 | private const string RowColumnName = "Row";
|
---|
[6680] | 37 | private const string TargetClassValuesColumnName = "Target Variable";
|
---|
| 38 | private const string EstimatedClassValuesColumnName = "Estimated Class Values";
|
---|
| 39 | private const string CorrectClassificationColumnName = "Correct Classification";
|
---|
[6672] | 40 | private const string ConfidenceColumnName = "Confidence";
|
---|
[5678] | 41 |
|
---|
[6672] | 42 | private const string SamplesComboBoxAllSamples = "All Samples";
|
---|
| 43 | private const string SamplesComboBoxTrainingSamples = "Training Samples";
|
---|
| 44 | private const string SamplesComboBoxTestSamples = "Test Samples";
|
---|
| 45 |
|
---|
| 46 | public new ClassificationEnsembleSolution Content {
|
---|
| 47 | get { return (ClassificationEnsembleSolution)base.Content; }
|
---|
[6642] | 48 | set { base.Content = value; }
|
---|
[5678] | 49 | }
|
---|
| 50 |
|
---|
[6672] | 51 | public ClassificationEnsembleSolutionEstimatedClassValuesView()
|
---|
[5678] | 52 | : base() {
|
---|
| 53 | InitializeComponent();
|
---|
[6672] | 54 | SamplesComboBox.Items.AddRange(new string[] { SamplesComboBoxAllSamples, SamplesComboBoxTrainingSamples, SamplesComboBoxTestSamples });
|
---|
| 55 | SamplesComboBox.SelectedIndex = 0;
|
---|
[6680] | 56 | matrixView.DataGridView.RowPrePaint += new DataGridViewRowPrePaintEventHandler(DataGridView_RowPrePaint);
|
---|
[5678] | 57 | }
|
---|
| 58 |
|
---|
[6680] | 59 |
|
---|
| 60 |
|
---|
[6672] | 61 | private void SamplesComboBox_SelectedIndexChanged(object sender, EventArgs e) {
|
---|
| 62 | UpdateEstimatedValues();
|
---|
| 63 | }
|
---|
| 64 |
|
---|
[6642] | 65 | protected override void UpdateEstimatedValues() {
|
---|
[6672] | 66 | if (InvokeRequired) {
|
---|
| 67 | Invoke((Action)UpdateEstimatedValues);
|
---|
| 68 | return;
|
---|
| 69 | }
|
---|
| 70 | if (Content == null) {
|
---|
| 71 | matrixView.Content = null;
|
---|
| 72 | return;
|
---|
| 73 | }
|
---|
[5678] | 74 |
|
---|
[8139] | 75 | int[] indices;
|
---|
[6672] | 76 | double[] estimatedClassValues;
|
---|
| 77 |
|
---|
| 78 | switch (SamplesComboBox.SelectedItem.ToString()) {
|
---|
| 79 | case SamplesComboBoxAllSamples: {
|
---|
[8139] | 80 | indices = Enumerable.Range(0, Content.ProblemData.Dataset.Rows).ToArray();
|
---|
[6672] | 81 | estimatedClassValues = Content.EstimatedClassValues.ToArray();
|
---|
| 82 | break;
|
---|
[5678] | 83 | }
|
---|
[6672] | 84 | case SamplesComboBoxTrainingSamples: {
|
---|
[8139] | 85 | indices = Content.ProblemData.TrainingIndices.ToArray();
|
---|
[6672] | 86 | estimatedClassValues = Content.EstimatedTrainingClassValues.ToArray();
|
---|
| 87 | break;
|
---|
| 88 | }
|
---|
| 89 | case SamplesComboBoxTestSamples: {
|
---|
[8139] | 90 | indices = Content.ProblemData.TestIndices.ToArray();
|
---|
[6672] | 91 | estimatedClassValues = Content.EstimatedTestClassValues.ToArray();
|
---|
| 92 | break;
|
---|
| 93 | }
|
---|
| 94 | default:
|
---|
| 95 | throw new ArgumentException();
|
---|
| 96 | }
|
---|
[5678] | 97 |
|
---|
[8554] | 98 | int classValuesCount = Content.ProblemData.Classes;
|
---|
[7022] | 99 | int solutionsCount = Content.ClassificationSolutions.Count();
|
---|
[8139] | 100 | string[,] values = new string[indices.Length, 5 + classValuesCount + solutionsCount];
|
---|
[6740] | 101 | double[] target = Content.ProblemData.Dataset.GetDoubleValues(Content.ProblemData.TargetVariable).ToArray();
|
---|
[8139] | 102 | List<List<double?>> estimatedValuesVector = GetEstimatedValues(SamplesComboBox.SelectedItem.ToString(), indices,
|
---|
[6672] | 103 | Content.ClassificationSolutions);
|
---|
| 104 |
|
---|
[8139] | 105 | for (int i = 0; i < indices.Length; i++) {
|
---|
| 106 | int row = indices[i];
|
---|
[6672] | 107 | values[i, 0] = row.ToString();
|
---|
[8859] | 108 | values[i, 1] = target[row].ToString();
|
---|
[6982] | 109 | //display only indices and target values if no models are present
|
---|
[7022] | 110 | if (solutionsCount > 0) {
|
---|
| 111 | values[i, 2] = estimatedClassValues[i].ToString();
|
---|
[8859] | 112 | values[i, 3] = (target[row].IsAlmost(estimatedClassValues[i])).ToString();
|
---|
[7022] | 113 | var groups =
|
---|
| 114 | estimatedValuesVector[i].GroupBy(x => x).Select(g => new { Key = g.Key, Count = g.Count() }).ToList();
|
---|
| 115 | var estimationCount = groups.Where(g => g.Key != null).Select(g => g.Count).Sum();
|
---|
[8859] | 116 | // take care of divide by zero
|
---|
| 117 | if (estimationCount != 0) {
|
---|
| 118 | values[i, 4] = (((double)groups.Where(g => g.Key == estimatedClassValues[i]).Single().Count) / estimationCount).ToString();
|
---|
| 119 | } else {
|
---|
| 120 | values[i, 4] = double.NaN.ToString();
|
---|
| 121 | }
|
---|
[8554] | 122 | for (int classIndex = 0; classIndex < Content.ProblemData.Classes; classIndex++) {
|
---|
| 123 | var group = groups.Where(g => g.Key == Content.ProblemData.ClassValues.ElementAt(classIndex)).SingleOrDefault();
|
---|
[7022] | 124 | if (group == null) values[i, 5 + classIndex] = 0.ToString();
|
---|
| 125 | else values[i, 5 + classIndex] = group.Count.ToString();
|
---|
| 126 | }
|
---|
| 127 | for (int modelIndex = 0; modelIndex < estimatedValuesVector[i].Count; modelIndex++) {
|
---|
| 128 | values[i, 5 + classValuesCount + modelIndex] = estimatedValuesVector[i][modelIndex] == null
|
---|
| 129 | ? string.Empty
|
---|
| 130 | : estimatedValuesVector[i][modelIndex].ToString();
|
---|
| 131 | }
|
---|
[5678] | 132 | }
|
---|
| 133 | }
|
---|
[6672] | 134 |
|
---|
| 135 | StringMatrix matrix = new StringMatrix(values);
|
---|
[6680] | 136 | List<string> columnNames = new List<string>() { "Id", TargetClassValuesColumnName, EstimatedClassValuesColumnName, CorrectClassificationColumnName, ConfidenceColumnName };
|
---|
[6672] | 137 | columnNames.AddRange(Content.ProblemData.ClassNames);
|
---|
| 138 | columnNames.AddRange(Content.Model.Models.Select(m => m.Name));
|
---|
| 139 | matrix.ColumnNames = columnNames;
|
---|
| 140 | matrix.SortableView = true;
|
---|
| 141 | matrixView.Content = matrix;
|
---|
[5678] | 142 | }
|
---|
[6672] | 143 |
|
---|
| 144 | private List<List<double?>> GetEstimatedValues(string samplesSelection, int[] rows, IEnumerable<IClassificationSolution> solutions) {
|
---|
| 145 | List<List<double?>> values = new List<List<double?>>();
|
---|
| 146 | int solutionIndex = 0;
|
---|
| 147 | foreach (var solution in solutions) {
|
---|
| 148 | double[] estimation = solution.GetEstimatedClassValues(rows).ToArray();
|
---|
| 149 | for (int i = 0; i < rows.Length; i++) {
|
---|
| 150 | var row = rows[i];
|
---|
| 151 | if (solutionIndex == 0) values.Add(new List<double?>());
|
---|
| 152 |
|
---|
| 153 | if (samplesSelection == SamplesComboBoxAllSamples)
|
---|
| 154 | values[i].Add(estimation[i]);
|
---|
| 155 | else if (samplesSelection == SamplesComboBoxTrainingSamples && solution.ProblemData.IsTrainingSample(row))
|
---|
| 156 | values[i].Add(estimation[i]);
|
---|
| 157 | else if (samplesSelection == SamplesComboBoxTestSamples && solution.ProblemData.IsTestSample(row))
|
---|
| 158 | values[i].Add(estimation[i]);
|
---|
| 159 | else
|
---|
| 160 | values[i].Add(null);
|
---|
| 161 | }
|
---|
| 162 | solutionIndex++;
|
---|
| 163 | }
|
---|
| 164 | return values;
|
---|
| 165 | }
|
---|
| 166 |
|
---|
[6680] | 167 | private void DataGridView_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e) {
|
---|
| 168 | if (InvokeRequired) {
|
---|
| 169 | Invoke(new EventHandler<DataGridViewRowPrePaintEventArgs>(DataGridView_RowPrePaint), sender, e);
|
---|
| 170 | return;
|
---|
| 171 | }
|
---|
[6982] | 172 | var cellValue = matrixView.DataGridView[3, e.RowIndex].Value.ToString();
|
---|
| 173 | if (string.IsNullOrEmpty(cellValue)) return;
|
---|
| 174 | bool correctClassified = bool.Parse(cellValue);
|
---|
[6680] | 175 | matrixView.DataGridView.Rows[e.RowIndex].DefaultCellStyle.ForeColor = correctClassified ? Color.MediumSeaGreen : Color.Red;
|
---|
| 176 | }
|
---|
[5678] | 177 | }
|
---|
| 178 | }
|
---|