[8435] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[12395] | 3 | * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[8435] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using System.Windows.Forms;
|
---|
| 26 | using HeuristicLab.Analysis;
|
---|
| 27 | using HeuristicLab.Common;
|
---|
| 28 | using HeuristicLab.MainForm;
|
---|
| 29 | using HeuristicLab.MainForm.WindowsForms;
|
---|
| 30 |
|
---|
| 31 | namespace HeuristicLab.Problems.DataAnalysis.Views {
|
---|
| 32 | [View("Cluster Visualization")]
|
---|
| 33 | [Content(typeof(IClusteringSolution), IsDefaultView = false)]
|
---|
| 34 | public partial class ClusteringSolutionVisualizationView : DataAnalysisSolutionEvaluationView {
|
---|
| 35 | private ViewHost viewHost = new ViewHost();
|
---|
| 36 | private ScatterPlot scatterPlot = new ScatterPlot();
|
---|
| 37 |
|
---|
| 38 | public new IClusteringSolution Content {
|
---|
| 39 | get { return (IClusteringSolution)base.Content; }
|
---|
| 40 | set { base.Content = value; }
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | public ClusteringSolutionVisualizationView() {
|
---|
| 44 | InitializeComponent();
|
---|
| 45 | viewHost.Dock = DockStyle.Fill;
|
---|
| 46 | splitContainer.Panel2.Controls.Add(viewHost);
|
---|
| 47 | rangeComboBox.SelectedIndex = 0;
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | protected override void OnContentChanged() {
|
---|
| 51 | base.OnContentChanged();
|
---|
| 52 | if (Content == null) {
|
---|
| 53 | viewHost.Content = null;
|
---|
| 54 | scatterPlot.Rows.Clear();
|
---|
| 55 | } else {
|
---|
| 56 | UpdateScatterPlot();
|
---|
| 57 | viewHost.Content = scatterPlot;
|
---|
| 58 | }
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | protected override void SetEnabledStateOfControls() {
|
---|
| 62 | base.SetEnabledStateOfControls();
|
---|
| 63 | rangeComboBox.Enabled = Content != null;
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | private void UpdateScatterPlot() {
|
---|
| 67 | scatterPlot.Rows.Clear();
|
---|
| 68 |
|
---|
| 69 | IEnumerable<int> range = null;
|
---|
| 70 | if (rangeComboBox.SelectedIndex == 0) range = Content.ProblemData.TrainingIndices;
|
---|
| 71 | else if (rangeComboBox.SelectedIndex == 1) range = Content.ProblemData.TestIndices;
|
---|
| 72 | else range = Enumerable.Range(0, Content.ProblemData.Dataset.Rows);
|
---|
| 73 |
|
---|
| 74 | IDictionary<int, Tuple<double, string>> classes = Content.Model.GetClusterValues(Content.ProblemData.Dataset, Enumerable.Range(0, Content.ProblemData.Dataset.Rows))
|
---|
| 75 | .Select((v, i) => new { Row = i, Cluster = (double)v })
|
---|
| 76 | .ToDictionary(x => x.Row, y => Tuple.Create(y.Cluster, "Cluster " + y.Cluster));
|
---|
| 77 |
|
---|
| 78 | var rows = classes.Values.Select(x => x.Item2).Distinct().ToDictionary(c => c, c => new ScatterPlotDataRow(c, string.Empty, Enumerable.Empty<Point2D<double>>()));
|
---|
| 79 |
|
---|
[11205] | 80 | var reduced = PCAReduce(Content.ProblemData.Dataset, range, Content.ProblemData.AllowedInputVariables);
|
---|
[8435] | 81 |
|
---|
| 82 | int idx = 0;
|
---|
| 83 | foreach (var r in range) {
|
---|
| 84 | rows[classes[r].Item2].Points.Add(new Point2D<double>(reduced[idx, 0], reduced[idx, 1]));
|
---|
| 85 | idx++;
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | scatterPlot.Rows.AddRange(rows.Values);
|
---|
| 89 | }
|
---|
| 90 |
|
---|
[11205] | 91 | private static double[,] PCAReduce(Dataset dataset, IEnumerable<int> rows, IEnumerable<string> variables) {
|
---|
[8435] | 92 | var instances = rows.ToArray();
|
---|
| 93 | var attributes = variables.ToArray();
|
---|
| 94 | var data = new double[instances.Length, attributes.Length + 1];
|
---|
| 95 |
|
---|
| 96 | for (int j = 0; j < attributes.Length; j++) {
|
---|
| 97 | int i = 0;
|
---|
| 98 | var values = dataset.GetDoubleValues(attributes[j], instances);
|
---|
| 99 | foreach (var v in values) {
|
---|
| 100 | data[i++, j] = v;
|
---|
| 101 | }
|
---|
| 102 | }
|
---|
| 103 | int info;
|
---|
| 104 | double[] variances;
|
---|
| 105 | var matrix = new double[0, 0];
|
---|
| 106 | alglib.pcabuildbasis(data, instances.Length, attributes.Length, out info, out variances, out matrix);
|
---|
| 107 |
|
---|
| 108 | var result = new double[instances.Length, 2];
|
---|
| 109 | int r = 0;
|
---|
| 110 | foreach (var inst in instances) {
|
---|
| 111 | int i = 0;
|
---|
| 112 | foreach (var attrib in attributes) {
|
---|
| 113 | double val = dataset.GetDoubleValue(attrib, inst);
|
---|
| 114 | for (int j = 0; j < result.GetLength(1); j++)
|
---|
| 115 | result[r, j] += val * matrix[i, j];
|
---|
| 116 | i++;
|
---|
| 117 | }
|
---|
| 118 | r++;
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | return result;
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | #region Event Handlers
|
---|
| 125 | private void rangeComboBox_SelectedIndexChanged(object sender, System.EventArgs e) {
|
---|
| 126 | if (Content != null) UpdateScatterPlot();
|
---|
| 127 | }
|
---|
| 128 | #endregion
|
---|
| 129 | }
|
---|
| 130 | }
|
---|