[5658] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[14185] | 3 | * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[5658] | 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;
|
---|
[14836] | 25 | using System.Threading;
|
---|
[5658] | 26 | using HeuristicLab.Common;
|
---|
| 27 | using HeuristicLab.Core;
|
---|
[5777] | 28 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
[5658] | 29 | using HeuristicLab.Optimization;
|
---|
| 30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 31 | using HeuristicLab.Problems.DataAnalysis;
|
---|
| 32 | using HeuristicLab.Problems.DataAnalysis.Symbolic;
|
---|
| 33 | using HeuristicLab.Problems.DataAnalysis.Symbolic.Classification;
|
---|
| 34 |
|
---|
| 35 | namespace HeuristicLab.Algorithms.DataAnalysis {
|
---|
| 36 | /// <summary>
|
---|
| 37 | /// Linear discriminant analysis classification algorithm.
|
---|
| 38 | /// </summary>
|
---|
[14836] | 39 | [Item("Linear Discriminant Analysis (LDA)", "Linear discriminant analysis classification algorithm (wrapper for ALGLIB).")]
|
---|
[12504] | 40 | [Creatable(CreatableAttribute.Categories.DataAnalysisClassification, Priority = 100)]
|
---|
[5658] | 41 | [StorableClass]
|
---|
| 42 | public sealed class LinearDiscriminantAnalysis : FixedDataAnalysisAlgorithm<IClassificationProblem> {
|
---|
| 43 | private const string LinearDiscriminantAnalysisSolutionResultName = "Linear discriminant analysis solution";
|
---|
| 44 |
|
---|
| 45 | [StorableConstructor]
|
---|
| 46 | private LinearDiscriminantAnalysis(bool deserializing) : base(deserializing) { }
|
---|
| 47 | private LinearDiscriminantAnalysis(LinearDiscriminantAnalysis original, Cloner cloner)
|
---|
| 48 | : base(original, cloner) {
|
---|
| 49 | }
|
---|
| 50 | public LinearDiscriminantAnalysis()
|
---|
| 51 | : base() {
|
---|
| 52 | Problem = new ClassificationProblem();
|
---|
| 53 | }
|
---|
| 54 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 55 | private void AfterDeserialization() { }
|
---|
| 56 |
|
---|
| 57 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 58 | return new LinearDiscriminantAnalysis(this, cloner);
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | #region Fisher LDA
|
---|
[14836] | 62 | protected override void Run(CancellationToken cancellationToken) {
|
---|
[5658] | 63 | var solution = CreateLinearDiscriminantAnalysisSolution(Problem.ProblemData);
|
---|
| 64 | Results.Add(new Result(LinearDiscriminantAnalysisSolutionResultName, "The linear discriminant analysis.", solution));
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | public static IClassificationSolution CreateLinearDiscriminantAnalysisSolution(IClassificationProblemData problemData) {
|
---|
[12509] | 68 | var dataset = problemData.Dataset;
|
---|
[5658] | 69 | string targetVariable = problemData.TargetVariable;
|
---|
| 70 | IEnumerable<string> allowedInputVariables = problemData.AllowedInputVariables;
|
---|
[8139] | 71 | IEnumerable<int> rows = problemData.TrainingIndices;
|
---|
[5658] | 72 | int nClasses = problemData.ClassNames.Count();
|
---|
[14836] | 73 | var doubleVariableNames = allowedInputVariables.Where(dataset.VariableHasType<double>).ToArray();
|
---|
| 74 | var factorVariableNames = allowedInputVariables.Where(dataset.VariableHasType<string>).ToArray();
|
---|
| 75 | double[,] inputMatrix = AlglibUtil.PrepareInputMatrix(dataset, doubleVariableNames.Concat(new string[] { targetVariable }), rows);
|
---|
| 76 |
|
---|
| 77 | var factorVariables = AlglibUtil.GetFactorVariableValues(dataset, factorVariableNames, rows);
|
---|
| 78 | double[,] factorMatrix = AlglibUtil.PrepareInputMatrix(dataset, factorVariables, rows);
|
---|
| 79 |
|
---|
| 80 | inputMatrix = factorMatrix.HorzCat(inputMatrix);
|
---|
| 81 |
|
---|
[6002] | 82 | if (inputMatrix.Cast<double>().Any(x => double.IsNaN(x) || double.IsInfinity(x)))
|
---|
| 83 | throw new NotSupportedException("Linear discriminant analysis does not support NaN or infinity values in the input dataset.");
|
---|
[5658] | 84 |
|
---|
| 85 | // change class values into class index
|
---|
| 86 | int targetVariableColumn = inputMatrix.GetLength(1) - 1;
|
---|
[5664] | 87 | List<double> classValues = problemData.ClassValues.OrderBy(x => x).ToList();
|
---|
[5658] | 88 | for (int row = 0; row < inputMatrix.GetLength(0); row++) {
|
---|
| 89 | inputMatrix[row, targetVariableColumn] = classValues.IndexOf(inputMatrix[row, targetVariableColumn]);
|
---|
| 90 | }
|
---|
| 91 | int info;
|
---|
| 92 | double[] w;
|
---|
[14836] | 93 | alglib.fisherlda(inputMatrix, inputMatrix.GetLength(0), inputMatrix.GetLength(1) - 1, nClasses, out info, out w);
|
---|
[5658] | 94 | if (info < 1) throw new ArgumentException("Error in calculation of linear discriminant analysis solution");
|
---|
| 95 |
|
---|
| 96 | ISymbolicExpressionTree tree = new SymbolicExpressionTree(new ProgramRootSymbol().CreateTreeNode());
|
---|
| 97 | ISymbolicExpressionTreeNode startNode = new StartSymbol().CreateTreeNode();
|
---|
[5733] | 98 | tree.Root.AddSubtree(startNode);
|
---|
[5658] | 99 | ISymbolicExpressionTreeNode addition = new Addition().CreateTreeNode();
|
---|
[5733] | 100 | startNode.AddSubtree(addition);
|
---|
[5658] | 101 |
|
---|
| 102 | int col = 0;
|
---|
[14836] | 103 | foreach (var kvp in factorVariables) {
|
---|
| 104 | var varName = kvp.Key;
|
---|
| 105 | foreach (var cat in kvp.Value) {
|
---|
| 106 | BinaryFactorVariableTreeNode vNode =
|
---|
| 107 | (BinaryFactorVariableTreeNode)new HeuristicLab.Problems.DataAnalysis.Symbolic.BinaryFactorVariable().CreateTreeNode();
|
---|
| 108 | vNode.VariableName = varName;
|
---|
| 109 | vNode.VariableValue = cat;
|
---|
| 110 | vNode.Weight = w[col];
|
---|
| 111 | addition.AddSubtree(vNode);
|
---|
| 112 | col++;
|
---|
| 113 | }
|
---|
| 114 | }
|
---|
| 115 | foreach (string column in doubleVariableNames) {
|
---|
[5658] | 116 | VariableTreeNode vNode = (VariableTreeNode)new HeuristicLab.Problems.DataAnalysis.Symbolic.Variable().CreateTreeNode();
|
---|
| 117 | vNode.VariableName = column;
|
---|
| 118 | vNode.Weight = w[col];
|
---|
[5733] | 119 | addition.AddSubtree(vNode);
|
---|
[5658] | 120 | col++;
|
---|
| 121 | }
|
---|
| 122 |
|
---|
[14836] | 123 | var model = CreateDiscriminantFunctionModel(tree, new SymbolicDataAnalysisExpressionTreeLinearInterpreter(), problemData, rows);
|
---|
[6649] | 124 | SymbolicDiscriminantFunctionClassificationSolution solution = new SymbolicDiscriminantFunctionClassificationSolution(model, (IClassificationProblemData)problemData.Clone());
|
---|
[5678] | 125 |
|
---|
[5658] | 126 | return solution;
|
---|
| 127 | }
|
---|
| 128 | #endregion
|
---|
[5678] | 129 |
|
---|
| 130 | private static SymbolicDiscriminantFunctionClassificationModel CreateDiscriminantFunctionModel(ISymbolicExpressionTree tree,
|
---|
| 131 | ISymbolicDataAnalysisExpressionTreeInterpreter interpreter,
|
---|
| 132 | IClassificationProblemData problemData,
|
---|
| 133 | IEnumerable<int> rows) {
|
---|
[13941] | 134 | var model = new SymbolicDiscriminantFunctionClassificationModel(problemData.TargetVariable, tree, interpreter, new AccuracyMaximizationThresholdCalculator());
|
---|
[8594] | 135 | model.RecalculateModelParameters(problemData, rows);
|
---|
[8531] | 136 | return model;
|
---|
[5678] | 137 | }
|
---|
[5658] | 138 | }
|
---|
| 139 | }
|
---|