Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GP-MoveOperators/HeuristicLab.Algorithms.DataAnalysis/3.4/NearestNeighbour/NearestNeighbourRegression.cs @ 7801

Last change on this file since 7801 was 7259, checked in by swagner, 13 years ago

Updated year of copyrights to 2012 (#1716)

File size: 4.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
29using HeuristicLab.Optimization;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31using HeuristicLab.Problems.DataAnalysis;
32using HeuristicLab.Problems.DataAnalysis.Symbolic;
33using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;
34using HeuristicLab.Parameters;
35
36namespace HeuristicLab.Algorithms.DataAnalysis {
37  /// <summary>
38  /// Nearest neighbour regression data analysis algorithm.
39  /// </summary>
40  [Item("Nearest Neighbour Regression", "Nearest neighbour regression data analysis algorithm (wrapper for ALGLIB).")]
41  [Creatable("Data Analysis")]
42  [StorableClass]
43  public sealed class NearestNeighbourRegression : FixedDataAnalysisAlgorithm<IRegressionProblem> {
44    private const string KParameterName = "K";
45    private const string NearestNeighbourRegressionModelResultName = "Nearest neighbour regression solution";
46
47    #region parameter properties
48    public IFixedValueParameter<IntValue> KParameter {
49      get { return (IFixedValueParameter<IntValue>)Parameters[KParameterName]; }
50    }
51    #endregion
52    #region properties
53    public int K {
54      get { return KParameter.Value.Value; }
55      set {
56        if (value <= 0) throw new ArgumentException("K must be larger than zero.", "K");
57        else KParameter.Value.Value = value;
58      }
59    }
60    #endregion
61
62    [StorableConstructor]
63    private NearestNeighbourRegression(bool deserializing) : base(deserializing) { }
64    private NearestNeighbourRegression(NearestNeighbourRegression original, Cloner cloner)
65      : base(original, cloner) {
66    }
67    public NearestNeighbourRegression()
68      : base() {
69      Parameters.Add(new FixedValueParameter<IntValue>(KParameterName, "The number of nearest neighbours to consider for regression.", new IntValue(3)));
70      Problem = new RegressionProblem();
71    }
72    [StorableHook(HookType.AfterDeserialization)]
73    private void AfterDeserialization() { }
74
75    public override IDeepCloneable Clone(Cloner cloner) {
76      return new NearestNeighbourRegression(this, cloner);
77    }
78
79    #region nearest neighbour
80    protected override void Run() {
81      var solution = CreateNearestNeighbourRegressionSolution(Problem.ProblemData, K);
82      Results.Add(new Result(NearestNeighbourRegressionModelResultName, "The nearest neighbour regression solution.", solution));
83    }
84
85    public static IRegressionSolution CreateNearestNeighbourRegressionSolution(IRegressionProblemData problemData, int k) {
86      Dataset dataset = problemData.Dataset;
87      string targetVariable = problemData.TargetVariable;
88      IEnumerable<string> allowedInputVariables = problemData.AllowedInputVariables;
89      IEnumerable<int> rows = problemData.TrainingIndizes;
90      double[,] inputMatrix = AlglibUtil.PrepareInputMatrix(dataset, allowedInputVariables.Concat(new string[] { targetVariable }), rows);
91      if (inputMatrix.Cast<double>().Any(x => double.IsNaN(x) || double.IsInfinity(x)))
92        throw new NotSupportedException("Nearest neighbour regression does not support NaN or infinity values in the input dataset.");
93
94      alglib.nearestneighbor.kdtree kdtree = new alglib.nearestneighbor.kdtree();
95
96      int nRows = inputMatrix.GetLength(0);
97
98      alglib.nearestneighbor.kdtreebuild(inputMatrix, nRows, inputMatrix.GetLength(1) - 1, 1, 2, kdtree);
99
100      return new NearestNeighbourRegressionSolution((IRegressionProblemData)problemData.Clone(), new NearestNeighbourModel(kdtree, k, targetVariable, allowedInputVariables));
101    }
102    #endregion
103  }
104}
Note: See TracBrowser for help on using the repository browser.