Free cookie consent management tool by TermsFeed Policy Generator

source: branches/symbreg-factors-2650/HeuristicLab.Algorithms.DataAnalysis/3.4/NearestNeighbour/NearestNeighbourRegression.cs @ 14239

Last change on this file since 14239 was 14239, checked in by gkronber, 8 years ago

#2650: merged r14234:14236 from trunk to branch

File size: 5.0 KB
RevLine 
[6577]1#region License Information
2/* HeuristicLab
[14185]3 * Copyright (C) 2002-2016 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>
[13238]35  [Item("Nearest Neighbour Regression (kNN)", "Nearest neighbour regression data analysis algorithm (wrapper for ALGLIB).")]
[12504]36  [Creatable(CreatableAttribute.Categories.DataAnalysisRegression, Priority = 150)]
[6577]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";
[14239]41    private const string WeightsParameterName = "Weights";
[6578]42
43    #region parameter properties
[6583]44    public IFixedValueParameter<IntValue> KParameter {
45      get { return (IFixedValueParameter<IntValue>)Parameters[KParameterName]; }
[6578]46    }
[14239]47
48    public IValueParameter<DoubleArray> WeightsParameter {
49      get { return (IValueParameter<DoubleArray>)Parameters[WeightsParameterName]; }
50    }
[6578]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    }
[14239]60
61    public DoubleArray Weights {
62      get { return WeightsParameter.Value; }
63      set { WeightsParameter.Value = value; }
64    }
[6578]65    #endregion
66
[6577]67    [StorableConstructor]
[6583]68    private NearestNeighbourRegression(bool deserializing) : base(deserializing) { }
69    private NearestNeighbourRegression(NearestNeighbourRegression original, Cloner cloner)
[6577]70      : base(original, cloner) {
71    }
[6583]72    public NearestNeighbourRegression()
[6577]73      : base() {
[6583]74      Parameters.Add(new FixedValueParameter<IntValue>(KParameterName, "The number of nearest neighbours to consider for regression.", new IntValue(3)));
[14239]75      Parameters.Add(new OptionalValueParameter<DoubleArray>(WeightsParameterName, "Optional: use weights to specify individual scaling values for all features. If not set the weights are calculated automatically (each feature is scaled to unit variance)"));
[6577]76      Problem = new RegressionProblem();
77    }
[14239]78
[6577]79    [StorableHook(HookType.AfterDeserialization)]
[14239]80    private void AfterDeserialization() {
81      // BackwardsCompatibility3.3
82      #region Backwards compatible code, remove with 3.4
83      if (!Parameters.ContainsKey(WeightsParameterName)) {
84        Parameters.Add(new OptionalValueParameter<DoubleArray>(WeightsParameterName, "Optional: use weights to specify individual scaling values for all features. If not set the weights are calculated automatically (each feature is scaled to unit variance)"));
85      }
86      #endregion
87    }
[6577]88
89    public override IDeepCloneable Clone(Cloner cloner) {
[6583]90      return new NearestNeighbourRegression(this, cloner);
[6577]91    }
92
[6583]93    #region nearest neighbour
[6577]94    protected override void Run() {
[14239]95      double[] weights = null;
96      if (Weights != null) weights = Weights.CloneAsArray();
97      var solution = CreateNearestNeighbourRegressionSolution(Problem.ProblemData, K, weights);
[6583]98      Results.Add(new Result(NearestNeighbourRegressionModelResultName, "The nearest neighbour regression solution.", solution));
[6577]99    }
100
[14239]101    public static IRegressionSolution CreateNearestNeighbourRegressionSolution(IRegressionProblemData problemData, int k, double[] weights = null) {
[8465]102      var clonedProblemData = (IRegressionProblemData)problemData.Clone();
[14239]103      return new NearestNeighbourRegressionSolution(Train(problemData, k, weights), clonedProblemData);
[8465]104    }
[6577]105
[14239]106    public static INearestNeighbourModel Train(IRegressionProblemData problemData, int k, double[] weights = null) {
[8465]107      return new NearestNeighbourModel(problemData.Dataset,
108        problemData.TrainingIndices,
109        k,
110        problemData.TargetVariable,
[14239]111        problemData.AllowedInputVariables,
112        weights);
[6577]113    }
114    #endregion
115  }
116}
Note: See TracBrowser for help on using the repository browser.