[5658] | 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;
|
---|
[5777] | 23 | using System.Collections.Generic;
|
---|
[5658] | 24 | using System.Linq;
|
---|
| 25 | using HeuristicLab.Common;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
[5777] | 27 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
[5658] | 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>
|
---|
[6240] | 38 | [Item("Linear Discriminant Analysis", "Linear discriminant analysis classification algorithm (wrapper for ALGLIB).")]
|
---|
[5658] | 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;
|
---|
[6182] | 70 | IEnumerable<int> rows = problemData.TrainingIndizes;
|
---|
[5658] | 71 | int nClasses = problemData.ClassNames.Count();
|
---|
| 72 | double[,] inputMatrix = AlglibUtil.PrepareInputMatrix(dataset, allowedInputVariables.Concat(new string[] { targetVariable }), rows);
|
---|
[6002] | 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.");
|
---|
[5658] | 75 |
|
---|
| 76 | // change class values into class index
|
---|
| 77 | int targetVariableColumn = inputMatrix.GetLength(1) - 1;
|
---|
[5664] | 78 | List<double> classValues = problemData.ClassValues.OrderBy(x => x).ToList();
|
---|
[5658] | 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();
|
---|
[5733] | 89 | tree.Root.AddSubtree(startNode);
|
---|
[5658] | 90 | ISymbolicExpressionTreeNode addition = new Addition().CreateTreeNode();
|
---|
[5733] | 91 | startNode.AddSubtree(addition);
|
---|
[5658] | 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];
|
---|
[5733] | 98 | addition.AddSubtree(vNode);
|
---|
[5658] | 99 | col++;
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | ConstantTreeNode cNode = (ConstantTreeNode)new Constant().CreateTreeNode();
|
---|
| 103 | cNode.Value = w[w.Length - 1];
|
---|
[5733] | 104 | addition.AddSubtree(cNode);
|
---|
[5658] | 105 |
|
---|
[5678] | 106 |
|
---|
| 107 | var model = LinearDiscriminantAnalysis.CreateDiscriminantFunctionModel(tree, new SymbolicDataAnalysisExpressionTreeInterpreter(), problemData, rows);
|
---|
[6649] | 108 | SymbolicDiscriminantFunctionClassificationSolution solution = new SymbolicDiscriminantFunctionClassificationSolution(model, (IClassificationProblemData)problemData.Clone());
|
---|
[5678] | 109 |
|
---|
[5658] | 110 | return solution;
|
---|
| 111 | }
|
---|
| 112 | #endregion
|
---|
[5678] | 113 |
|
---|
| 114 | private static SymbolicDiscriminantFunctionClassificationModel CreateDiscriminantFunctionModel(ISymbolicExpressionTree tree,
|
---|
| 115 | ISymbolicDataAnalysisExpressionTreeInterpreter interpreter,
|
---|
| 116 | IClassificationProblemData problemData,
|
---|
| 117 | IEnumerable<int> rows) {
|
---|
[5736] | 118 | return new SymbolicDiscriminantFunctionClassificationModel(tree, interpreter);
|
---|
[5678] | 119 | }
|
---|
[5658] | 120 | }
|
---|
| 121 | }
|
---|