Free cookie consent management tool by TermsFeed Policy Generator

source: branches/MPI/HeuristicLab.Algorithms.DataAnalysis/3.4/Linear/LinearDiscriminantAnalysis.cs @ 6361

Last change on this file since 6361 was 6240, checked in by gkronber, 13 years ago

#1473: Implemented wrapper for ALGLIB random forest for regression.

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