Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Algorithms.DataAnalysis/3.4/NearestNeighbour/NearestNeighbourRegression.cs @ 16147

Last change on this file since 16147 was 15584, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers on stable

File size: 5.1 KB
RevLine 
[6577]1#region License Information
2/* HeuristicLab
[15584]3 * Copyright (C) 2002-2018 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;
[15061]23using System.Threading;
[6577]24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Optimization;
[8465]28using HeuristicLab.Parameters;
[6577]29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30using HeuristicLab.Problems.DataAnalysis;
31
32namespace HeuristicLab.Algorithms.DataAnalysis {
33  /// <summary>
[6583]34  /// Nearest neighbour regression data analysis algorithm.
[6577]35  /// </summary>
[13297]36  [Item("Nearest Neighbour Regression (kNN)", "Nearest neighbour regression data analysis algorithm (wrapper for ALGLIB).")]
[12708]37  [Creatable(CreatableAttribute.Categories.DataAnalysisRegression, Priority = 150)]
[6577]38  [StorableClass]
[6583]39  public sealed class NearestNeighbourRegression : FixedDataAnalysisAlgorithm<IRegressionProblem> {
40    private const string KParameterName = "K";
41    private const string NearestNeighbourRegressionModelResultName = "Nearest neighbour regression solution";
[14308]42    private const string WeightsParameterName = "Weights";
[6578]43
44    #region parameter properties
[6583]45    public IFixedValueParameter<IntValue> KParameter {
46      get { return (IFixedValueParameter<IntValue>)Parameters[KParameterName]; }
[6578]47    }
[14308]48
49    public IValueParameter<DoubleArray> WeightsParameter {
50      get { return (IValueParameter<DoubleArray>)Parameters[WeightsParameterName]; }
51    }
[6578]52    #endregion
53    #region properties
[6583]54    public int K {
55      get { return KParameter.Value.Value; }
[6578]56      set {
[6583]57        if (value <= 0) throw new ArgumentException("K must be larger than zero.", "K");
58        else KParameter.Value.Value = value;
[6578]59      }
60    }
[14308]61
62    public DoubleArray Weights {
63      get { return WeightsParameter.Value; }
64      set { WeightsParameter.Value = value; }
65    }
[6578]66    #endregion
67
[6577]68    [StorableConstructor]
[6583]69    private NearestNeighbourRegression(bool deserializing) : base(deserializing) { }
70    private NearestNeighbourRegression(NearestNeighbourRegression original, Cloner cloner)
[6577]71      : base(original, cloner) {
72    }
[6583]73    public NearestNeighbourRegression()
[6577]74      : base() {
[6583]75      Parameters.Add(new FixedValueParameter<IntValue>(KParameterName, "The number of nearest neighbours to consider for regression.", new IntValue(3)));
[14308]76      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]77      Problem = new RegressionProblem();
78    }
[14308]79
[6577]80    [StorableHook(HookType.AfterDeserialization)]
[14308]81    private void AfterDeserialization() {
82      // BackwardsCompatibility3.3
83      #region Backwards compatible code, remove with 3.4
84      if (!Parameters.ContainsKey(WeightsParameterName)) {
85        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)"));
86      }
87      #endregion
88    }
[6577]89
90    public override IDeepCloneable Clone(Cloner cloner) {
[6583]91      return new NearestNeighbourRegression(this, cloner);
[6577]92    }
93
[6583]94    #region nearest neighbour
[15061]95    protected override void Run(CancellationToken cancellationToken) {
[14308]96      double[] weights = null;
97      if (Weights != null) weights = Weights.CloneAsArray();
98      var solution = CreateNearestNeighbourRegressionSolution(Problem.ProblemData, K, weights);
[6583]99      Results.Add(new Result(NearestNeighbourRegressionModelResultName, "The nearest neighbour regression solution.", solution));
[6577]100    }
101
[14308]102    public static IRegressionSolution CreateNearestNeighbourRegressionSolution(IRegressionProblemData problemData, int k, double[] weights = null) {
[8465]103      var clonedProblemData = (IRegressionProblemData)problemData.Clone();
[14308]104      return new NearestNeighbourRegressionSolution(Train(problemData, k, weights), clonedProblemData);
[8465]105    }
[6577]106
[14308]107    public static INearestNeighbourModel Train(IRegressionProblemData problemData, int k, double[] weights = null) {
[8465]108      return new NearestNeighbourModel(problemData.Dataset,
109        problemData.TrainingIndices,
110        k,
111        problemData.TargetVariable,
[14308]112        problemData.AllowedInputVariables,
113        weights);
[6577]114    }
115    #endregion
116  }
117}
Note: See TracBrowser for help on using the repository browser.