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.Linq;
|
---|
24 | using HeuristicLab.Common;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Data;
|
---|
27 | using HeuristicLab.Optimization;
|
---|
28 | using HeuristicLab.Parameters;
|
---|
29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
30 | using HeuristicLab.Problems.DataAnalysis;
|
---|
31 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
32 | using System.Collections.Generic;
|
---|
33 | using HeuristicLab.Problems.DataAnalysis.Symbolic;
|
---|
34 | using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;
|
---|
35 | using HeuristicLab.Problems.DataAnalysis.Symbolic.Classification;
|
---|
36 |
|
---|
37 | namespace 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 |
|
---|
110 | var model = LinearDiscriminantAnalysis.CreateDiscriminantFunctionModel(tree, new SymbolicDataAnalysisExpressionTreeInterpreter(), problemData, rows);
|
---|
111 | SymbolicDiscriminantFunctionClassificationSolution solution = new SymbolicDiscriminantFunctionClassificationSolution(model, problemData);
|
---|
112 |
|
---|
113 | return solution;
|
---|
114 | }
|
---|
115 | #endregion
|
---|
116 |
|
---|
117 | private static SymbolicDiscriminantFunctionClassificationModel CreateDiscriminantFunctionModel(ISymbolicExpressionTree tree,
|
---|
118 | ISymbolicDataAnalysisExpressionTreeInterpreter interpreter,
|
---|
119 | IClassificationProblemData problemData,
|
---|
120 | IEnumerable<int> rows) {
|
---|
121 | string targetVariable = problemData.TargetVariable;
|
---|
122 | var estimatedValues = interpreter.GetSymbolicExpressionTreeValues(tree, problemData.Dataset, rows);
|
---|
123 | var targetClassValues = problemData.Dataset.GetEnumeratedVariableValues(targetVariable, rows);
|
---|
124 |
|
---|
125 | double[] classValues;
|
---|
126 | double[] thresholds;
|
---|
127 | NormalDistributionCutPointsThresholdCalculator.CalculateThresholds(problemData, estimatedValues, targetClassValues, out classValues, out thresholds);
|
---|
128 | return new SymbolicDiscriminantFunctionClassificationModel(tree, interpreter, classValues, thresholds);
|
---|
129 | }
|
---|
130 | }
|
---|
131 | }
|
---|