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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
28 | using HeuristicLab.Optimization;
|
---|
29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
30 | using HeuristicLab.Problems.DataAnalysis;
|
---|
31 | using HeuristicLab.Problems.DataAnalysis.Symbolic;
|
---|
32 | using HeuristicLab.Problems.DataAnalysis.Symbolic.Classification;
|
---|
33 |
|
---|
34 | namespace HeuristicLab.Algorithms.DataAnalysis {
|
---|
35 | /// <summary>
|
---|
36 | /// Linear discriminant analysis classification algorithm.
|
---|
37 | /// </summary>
|
---|
38 | [Item("Linear Discriminant Analysis", "Linear discriminant analysis classification algorithm.")]
|
---|
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 | int samplesStart = problemData.TrainingPartition.Start;
|
---|
71 | int samplesEnd = problemData.TrainingPartition.End;
|
---|
72 | IEnumerable<int> rows = Enumerable.Range(samplesStart, samplesEnd - samplesStart);
|
---|
73 | int nClasses = problemData.ClassNames.Count();
|
---|
74 | double[,] inputMatrix = AlglibUtil.PrepareInputMatrix(dataset, allowedInputVariables.Concat(new string[] { targetVariable }), rows);
|
---|
75 | if (inputMatrix.Cast<double>().Any(x => double.IsNaN(x) || double.IsInfinity(x)))
|
---|
76 | throw new NotSupportedException("Linear discriminant analysis does not support NaN or infinity values in the input dataset.");
|
---|
77 |
|
---|
78 | // change class values into class index
|
---|
79 | int targetVariableColumn = inputMatrix.GetLength(1) - 1;
|
---|
80 | List<double> classValues = problemData.ClassValues.OrderBy(x => x).ToList();
|
---|
81 | for (int row = 0; row < inputMatrix.GetLength(0); row++) {
|
---|
82 | inputMatrix[row, targetVariableColumn] = classValues.IndexOf(inputMatrix[row, targetVariableColumn]);
|
---|
83 | }
|
---|
84 | int info;
|
---|
85 | double[] w;
|
---|
86 | alglib.fisherlda(inputMatrix, inputMatrix.GetLength(0), allowedInputVariables.Count(), nClasses, out info, out w);
|
---|
87 | if (info < 1) throw new ArgumentException("Error in calculation of linear discriminant analysis solution");
|
---|
88 |
|
---|
89 | ISymbolicExpressionTree tree = new SymbolicExpressionTree(new ProgramRootSymbol().CreateTreeNode());
|
---|
90 | ISymbolicExpressionTreeNode startNode = new StartSymbol().CreateTreeNode();
|
---|
91 | tree.Root.AddSubtree(startNode);
|
---|
92 | ISymbolicExpressionTreeNode addition = new Addition().CreateTreeNode();
|
---|
93 | startNode.AddSubtree(addition);
|
---|
94 |
|
---|
95 | int col = 0;
|
---|
96 | foreach (string column in allowedInputVariables) {
|
---|
97 | VariableTreeNode vNode = (VariableTreeNode)new HeuristicLab.Problems.DataAnalysis.Symbolic.Variable().CreateTreeNode();
|
---|
98 | vNode.VariableName = column;
|
---|
99 | vNode.Weight = w[col];
|
---|
100 | addition.AddSubtree(vNode);
|
---|
101 | col++;
|
---|
102 | }
|
---|
103 |
|
---|
104 | ConstantTreeNode cNode = (ConstantTreeNode)new Constant().CreateTreeNode();
|
---|
105 | cNode.Value = w[w.Length - 1];
|
---|
106 | addition.AddSubtree(cNode);
|
---|
107 |
|
---|
108 |
|
---|
109 | var model = LinearDiscriminantAnalysis.CreateDiscriminantFunctionModel(tree, new SymbolicDataAnalysisExpressionTreeInterpreter(), problemData, rows);
|
---|
110 | SymbolicDiscriminantFunctionClassificationSolution solution = new SymbolicDiscriminantFunctionClassificationSolution(model, problemData);
|
---|
111 |
|
---|
112 | return solution;
|
---|
113 | }
|
---|
114 | #endregion
|
---|
115 |
|
---|
116 | private static SymbolicDiscriminantFunctionClassificationModel CreateDiscriminantFunctionModel(ISymbolicExpressionTree tree,
|
---|
117 | ISymbolicDataAnalysisExpressionTreeInterpreter interpreter,
|
---|
118 | IClassificationProblemData problemData,
|
---|
119 | IEnumerable<int> rows) {
|
---|
120 | return new SymbolicDiscriminantFunctionClassificationModel(tree, interpreter);
|
---|
121 | }
|
---|
122 | }
|
---|
123 | }
|
---|