Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceSpeedUp/HeuristicLab.Algorithms.DataAnalysis/3.4/NearestNeighbour/NearestNeighbourRegression.cs @ 15866

Last change on this file since 15866 was 6760, checked in by epitzer, 13 years ago

#1530 integrate changes from trunk

File size: 4.5 KB
RevLine 
[6577]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
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>
[6583]38  /// Nearest neighbour regression data analysis algorithm.
[6577]39  /// </summary>
[6583]40  [Item("Nearest Neighbour Regression", "Nearest neighbour regression data analysis algorithm (wrapper for ALGLIB).")]
[6577]41  [Creatable("Data Analysis")]
42  [StorableClass]
[6583]43  public sealed class NearestNeighbourRegression : FixedDataAnalysisAlgorithm<IRegressionProblem> {
44    private const string KParameterName = "K";
45    private const string NearestNeighbourRegressionModelResultName = "Nearest neighbour regression solution";
[6578]46
47    #region parameter properties
[6583]48    public IFixedValueParameter<IntValue> KParameter {
49      get { return (IFixedValueParameter<IntValue>)Parameters[KParameterName]; }
[6578]50    }
51    #endregion
52    #region properties
[6583]53    public int K {
54      get { return KParameter.Value.Value; }
[6578]55      set {
[6583]56        if (value <= 0) throw new ArgumentException("K must be larger than zero.", "K");
57        else KParameter.Value.Value = value;
[6578]58      }
59    }
60    #endregion
61
[6577]62    [StorableConstructor]
[6583]63    private NearestNeighbourRegression(bool deserializing) : base(deserializing) { }
64    private NearestNeighbourRegression(NearestNeighbourRegression original, Cloner cloner)
[6577]65      : base(original, cloner) {
66    }
[6583]67    public NearestNeighbourRegression()
[6577]68      : base() {
[6583]69      Parameters.Add(new FixedValueParameter<IntValue>(KParameterName, "The number of nearest neighbours to consider for regression.", new IntValue(3)));
[6577]70      Problem = new RegressionProblem();
71    }
72    [StorableHook(HookType.AfterDeserialization)]
73    private void AfterDeserialization() { }
74
75    public override IDeepCloneable Clone(Cloner cloner) {
[6583]76      return new NearestNeighbourRegression(this, cloner);
[6577]77    }
78
[6583]79    #region nearest neighbour
[6577]80    protected override void Run() {
[6583]81      var solution = CreateNearestNeighbourRegressionSolution(Problem.ProblemData, K);
82      Results.Add(new Result(NearestNeighbourRegressionModelResultName, "The nearest neighbour regression solution.", solution));
[6577]83    }
84
[6583]85    public static IRegressionSolution CreateNearestNeighbourRegressionSolution(IRegressionProblemData problemData, int k) {
[6577]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)))
[6583]92        throw new NotSupportedException("Nearest neighbour regression does not support NaN or infinity values in the input dataset.");
[6577]93
[6583]94      alglib.nearestneighbor.kdtree kdtree = new alglib.nearestneighbor.kdtree();
[6577]95
96      int nRows = inputMatrix.GetLength(0);
97
[6583]98      alglib.nearestneighbor.kdtreebuild(inputMatrix, nRows, inputMatrix.GetLength(1) - 1, 1, 2, kdtree);
[6577]99
[6649]100      return new NearestNeighbourRegressionSolution((IRegressionProblemData)problemData.Clone(), new NearestNeighbourModel(kdtree, k, targetVariable, allowedInputVariables));
[6577]101    }
102    #endregion
103  }
104}
Note: See TracBrowser for help on using the repository browser.