Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis Refactoring/HeuristicLab.Algorithms.DataAnalysis/3.4/Linear/LinearDiscriminantAnalysis.cs @ 5658

Last change on this file since 5658 was 5658, checked in by gkronber, 12 years ago

#1418 implemented wrapper for LDA (linear discriminant analysis) implemented in alglib.

File size: 5.3 KB
Line 
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
22using System;
23using System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Optimization;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30using HeuristicLab.Problems.DataAnalysis;
31using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
32using System.Collections.Generic;
33using HeuristicLab.Problems.DataAnalysis.Symbolic;
34using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;
35using HeuristicLab.Problems.DataAnalysis.Symbolic.Classification;
36
37namespace HeuristicLab.Algorithms.DataAnalysis {
38  /// <summary>
39  /// Linear discriminant analysis classification algorithm.
40  /// </summary>
41  [Item("Linear Discriminant Analysis", "Linear discriminant analysis classification algorithm.")]
42  [Creatable("Data Analysis")]
43  [StorableClass]
44  public sealed class LinearDiscriminantAnalysis : FixedDataAnalysisAlgorithm<IClassificationProblem> {
45    private const string LinearDiscriminantAnalysisSolutionResultName = "Linear discriminant analysis solution";
46
47    [StorableConstructor]
48    private LinearDiscriminantAnalysis(bool deserializing) : base(deserializing) { }
49    private LinearDiscriminantAnalysis(LinearDiscriminantAnalysis original, Cloner cloner)
50      : base(original, cloner) {
51    }
52    public LinearDiscriminantAnalysis()
53      : base() {
54      Problem = new ClassificationProblem();
55    }
56    [StorableHook(HookType.AfterDeserialization)]
57    private void AfterDeserialization() { }
58
59    public override IDeepCloneable Clone(Cloner cloner) {
60      return new LinearDiscriminantAnalysis(this, cloner);
61    }
62
63    #region Fisher LDA
64    protected override void Run() {
65      var solution = CreateLinearDiscriminantAnalysisSolution(Problem.ProblemData);
66      Results.Add(new Result(LinearDiscriminantAnalysisSolutionResultName, "The linear discriminant analysis.", solution));
67    }
68
69    public static IClassificationSolution CreateLinearDiscriminantAnalysisSolution(IClassificationProblemData problemData) {
70      Dataset dataset = problemData.Dataset;
71      string targetVariable = problemData.TargetVariable;
72      IEnumerable<string> allowedInputVariables = problemData.AllowedInputVariables;
73      int samplesStart = problemData.TrainingPartitionStart.Value;
74      int samplesEnd = problemData.TrainingPartitionEnd.Value;
75      IEnumerable<int> rows = Enumerable.Range(samplesStart, samplesEnd - samplesStart);
76      int nClasses = problemData.ClassNames.Count();
77      double[,] inputMatrix = AlglibUtil.PrepareInputMatrix(dataset, allowedInputVariables.Concat(new string[] { targetVariable }), rows);
78
79      // change class values into class index
80      int targetVariableColumn = inputMatrix.GetLength(1) - 1;
81      List<double> classValues = problemData.ClassValues.OrderBy(x=>x).ToList();
82      for (int row = 0; row < inputMatrix.GetLength(0); row++) {
83        inputMatrix[row, targetVariableColumn] = classValues.IndexOf(inputMatrix[row, targetVariableColumn]);
84      }
85      int info;
86      double[] w;
87      alglib.fisherlda(inputMatrix, inputMatrix.GetLength(0), allowedInputVariables.Count(), nClasses, out info, out w);
88      if (info < 1) throw new ArgumentException("Error in calculation of linear discriminant analysis solution");
89
90      ISymbolicExpressionTree tree = new SymbolicExpressionTree(new ProgramRootSymbol().CreateTreeNode());
91      ISymbolicExpressionTreeNode startNode = new StartSymbol().CreateTreeNode();
92      tree.Root.AddSubTree(startNode);
93      ISymbolicExpressionTreeNode addition = new Addition().CreateTreeNode();
94      startNode.AddSubTree(addition);
95
96      int col = 0;
97      foreach (string column in allowedInputVariables) {
98        VariableTreeNode vNode = (VariableTreeNode)new HeuristicLab.Problems.DataAnalysis.Symbolic.Variable().CreateTreeNode();
99        vNode.VariableName = column;
100        vNode.Weight = w[col];
101        addition.AddSubTree(vNode);
102        col++;
103      }
104
105      ConstantTreeNode cNode = (ConstantTreeNode)new Constant().CreateTreeNode();
106      cNode.Value = w[w.Length - 1];
107      addition.AddSubTree(cNode);
108
109      var model = new SymbolicDiscriminantFunctionClassificationModel(tree, new SymbolicDataAnalysisExpressionTreeInterpreter(), classValues);
110      SymbolicDiscriminantFunctionClassificationSolution solution = new SymbolicDiscriminantFunctionClassificationSolution(model, problemData);
111      return solution;
112    }
113    #endregion
114  }
115}
Note: See TracBrowser for help on using the repository browser.