Free cookie consent management tool by TermsFeed Policy Generator

source: branches/NCA/HeuristicLab.Algorithms.NCA/3.3/Initialization/LDAInitializer.cs @ 8425

Last change on this file since 8425 was 8425, checked in by abeham, 12 years ago

#1913: Added several initialization methods (LDA, PCA, and Random)

File size: 3.0 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.Collections.Generic;
23using System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27using HeuristicLab.Problems.DataAnalysis;
28
29namespace HeuristicLab.Algorithms.NCA {
30  [Item("LDA", "Initializes the matrix by performing a linear discriminant analysis.")]
31  [StorableClass]
32  public class LDAInitializer : Item, INCAInitializer {
33
34    [StorableConstructor]
35    protected LDAInitializer(bool deserializing) : base(deserializing) { }
36    protected LDAInitializer(LDAInitializer original, Cloner cloner) : base(original, cloner) { }
37    public LDAInitializer() : base() { }
38
39    public override IDeepCloneable Clone(Cloner cloner) {
40      return new LDAInitializer(this, cloner);
41    }
42
43    public double[] Initialize(IClassificationProblemData data, int dimensions) {
44      var instances = data.TrainingIndices.Count();
45      var attributes = data.AllowedInputVariables.Count();
46
47      var ldaDs = new double[instances, attributes + 1];
48      int row, col = 0;
49      foreach (var variable in data.AllowedInputVariables) {
50        row = 0;
51        foreach (var value in data.Dataset.GetDoubleValues(variable, data.TrainingIndices)) {
52          ldaDs[row, col] = value;
53          row++;
54        }
55        col++;
56      }
57      row = 0;
58      var uniqueClasses = new Dictionary<double, int>();
59      foreach (var label in data.Dataset.GetDoubleValues(data.TargetVariable, data.TrainingIndices)) {
60        if (!uniqueClasses.ContainsKey(label))
61          uniqueClasses[label] = uniqueClasses.Count;
62        ldaDs[row++, attributes] = label;
63      }
64      for (row = 0; row < instances; row++)
65        ldaDs[row, attributes] = uniqueClasses[ldaDs[row, attributes]];
66
67      int info;
68      double[,] matrix;
69      alglib.fisherldan(ldaDs, instances, attributes, uniqueClasses.Count, out info, out matrix);
70
71      var result = new double[attributes * dimensions];
72      for (int i = 0; i < attributes; i++)
73        for (int j = 0; j < dimensions; j++)
74          result[i * dimensions + j] = matrix[i, j];
75
76      return result;
77    }
78
79  }
80}
Note: See TracBrowser for help on using the repository browser.