Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/NearestNeighbour/NearestNeighbourRegression.cs @ 9844

Last change on this file since 9844 was 9456, checked in by swagner, 12 years ago

Updated copyright year and added some missing license headers (#1889)

File size: 3.7 KB
RevLine 
[6577]1#region License Information
2/* HeuristicLab
[9456]3 * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[6577]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 HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Optimization;
[8465]27using HeuristicLab.Parameters;
[6577]28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29using HeuristicLab.Problems.DataAnalysis;
30
31namespace HeuristicLab.Algorithms.DataAnalysis {
32  /// <summary>
[6583]33  /// Nearest neighbour regression data analysis algorithm.
[6577]34  /// </summary>
[6583]35  [Item("Nearest Neighbour Regression", "Nearest neighbour regression data analysis algorithm (wrapper for ALGLIB).")]
[6577]36  [Creatable("Data Analysis")]
37  [StorableClass]
[6583]38  public sealed class NearestNeighbourRegression : FixedDataAnalysisAlgorithm<IRegressionProblem> {
39    private const string KParameterName = "K";
40    private const string NearestNeighbourRegressionModelResultName = "Nearest neighbour regression solution";
[6578]41
42    #region parameter properties
[6583]43    public IFixedValueParameter<IntValue> KParameter {
44      get { return (IFixedValueParameter<IntValue>)Parameters[KParameterName]; }
[6578]45    }
46    #endregion
47    #region properties
[6583]48    public int K {
49      get { return KParameter.Value.Value; }
[6578]50      set {
[6583]51        if (value <= 0) throw new ArgumentException("K must be larger than zero.", "K");
52        else KParameter.Value.Value = value;
[6578]53      }
54    }
55    #endregion
56
[6577]57    [StorableConstructor]
[6583]58    private NearestNeighbourRegression(bool deserializing) : base(deserializing) { }
59    private NearestNeighbourRegression(NearestNeighbourRegression original, Cloner cloner)
[6577]60      : base(original, cloner) {
61    }
[6583]62    public NearestNeighbourRegression()
[6577]63      : base() {
[6583]64      Parameters.Add(new FixedValueParameter<IntValue>(KParameterName, "The number of nearest neighbours to consider for regression.", new IntValue(3)));
[6577]65      Problem = new RegressionProblem();
66    }
67    [StorableHook(HookType.AfterDeserialization)]
68    private void AfterDeserialization() { }
69
70    public override IDeepCloneable Clone(Cloner cloner) {
[6583]71      return new NearestNeighbourRegression(this, cloner);
[6577]72    }
73
[6583]74    #region nearest neighbour
[6577]75    protected override void Run() {
[6583]76      var solution = CreateNearestNeighbourRegressionSolution(Problem.ProblemData, K);
77      Results.Add(new Result(NearestNeighbourRegressionModelResultName, "The nearest neighbour regression solution.", solution));
[6577]78    }
79
[6583]80    public static IRegressionSolution CreateNearestNeighbourRegressionSolution(IRegressionProblemData problemData, int k) {
[8465]81      var clonedProblemData = (IRegressionProblemData)problemData.Clone();
82      return new NearestNeighbourRegressionSolution(clonedProblemData, Train(problemData, k));
83    }
[6577]84
[8465]85    public static INearestNeighbourModel Train(IRegressionProblemData problemData, int k) {
86      return new NearestNeighbourModel(problemData.Dataset,
87        problemData.TrainingIndices,
88        k,
89        problemData.TargetVariable,
90        problemData.AllowedInputVariables);
[6577]91    }
92    #endregion
93  }
94}
Note: See TracBrowser for help on using the repository browser.