Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/Linear/LinearDiscriminantAnalysis.cs @ 12962

Last change on this file since 12962 was 12509, checked in by mkommend, 10 years ago

#2276: Reintegrated branch for dataset refactoring.

File size: 5.7 KB
RevLine 
[5658]1#region License Information
2/* HeuristicLab
[12012]3 * Copyright (C) 2002-2015 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
22using System;
[5777]23using System.Collections.Generic;
[5658]24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
[5777]27using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
[5658]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>
[6240]38  [Item("Linear Discriminant Analysis", "Linear discriminant analysis classification algorithm (wrapper for ALGLIB).")]
[12504]39  [Creatable(CreatableAttribute.Categories.DataAnalysisClassification, Priority = 100)]
[5658]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) {
[12509]67      var dataset = problemData.Dataset;
[5658]68      string targetVariable = problemData.TargetVariable;
69      IEnumerable<string> allowedInputVariables = problemData.AllowedInputVariables;
[8139]70      IEnumerable<int> rows = problemData.TrainingIndices;
[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
[5678]102      var model = LinearDiscriminantAnalysis.CreateDiscriminantFunctionModel(tree, new SymbolicDataAnalysisExpressionTreeInterpreter(), problemData, rows);
[6649]103      SymbolicDiscriminantFunctionClassificationSolution solution = new SymbolicDiscriminantFunctionClassificationSolution(model, (IClassificationProblemData)problemData.Clone());
[5678]104
[5658]105      return solution;
106    }
107    #endregion
[5678]108
109    private static SymbolicDiscriminantFunctionClassificationModel CreateDiscriminantFunctionModel(ISymbolicExpressionTree tree,
110      ISymbolicDataAnalysisExpressionTreeInterpreter interpreter,
111      IClassificationProblemData problemData,
112      IEnumerable<int> rows) {
[8594]113      var model = new SymbolicDiscriminantFunctionClassificationModel(tree, interpreter, new AccuracyMaximizationThresholdCalculator());
114      model.RecalculateModelParameters(problemData, rows);
[8531]115      return model;
[5678]116    }
[5658]117  }
118}
Note: See TracBrowser for help on using the repository browser.