Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 16491 was 16491, checked in by gkronber, 5 years ago

#2942: merged changes from r16408, r16488, r16490 from branch to trunk (manually)

File size: 5.9 KB
RevLine 
[6577]1#region License Information
2/* HeuristicLab
[15583]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;
[14523]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>
[13238]36  [Item("Nearest Neighbour Regression (kNN)", "Nearest neighbour regression data analysis algorithm (wrapper for ALGLIB).")]
[12504]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";
[14235]42    private const string WeightsParameterName = "Weights";
[16491]43    private const string SelfMatchParameterName = "SelfMatch";
[6578]44
45    #region parameter properties
[6583]46    public IFixedValueParameter<IntValue> KParameter {
47      get { return (IFixedValueParameter<IntValue>)Parameters[KParameterName]; }
[6578]48    }
[16491]49    public IFixedValueParameter<BoolValue> SelfMatchParameter {
50      get { return (IFixedValueParameter<BoolValue>)Parameters[SelfMatchParameterName]; }
51    }
[14235]52    public IValueParameter<DoubleArray> WeightsParameter {
53      get { return (IValueParameter<DoubleArray>)Parameters[WeightsParameterName]; }
54    }
[6578]55    #endregion
56    #region properties
[6583]57    public int K {
58      get { return KParameter.Value.Value; }
[6578]59      set {
[6583]60        if (value <= 0) throw new ArgumentException("K must be larger than zero.", "K");
61        else KParameter.Value.Value = value;
[6578]62      }
63    }
[16491]64    public bool SelfMatch {
65      get { return SelfMatchParameter.Value.Value; }
66      set { SelfMatchParameter.Value.Value = value; }
67    }
[14235]68    public DoubleArray Weights {
69      get { return WeightsParameter.Value; }
70      set { WeightsParameter.Value = value; }
71    }
[6578]72    #endregion
73
[6577]74    [StorableConstructor]
[6583]75    private NearestNeighbourRegression(bool deserializing) : base(deserializing) { }
76    private NearestNeighbourRegression(NearestNeighbourRegression original, Cloner cloner)
[6577]77      : base(original, cloner) {
78    }
[6583]79    public NearestNeighbourRegression()
[6577]80      : base() {
[6583]81      Parameters.Add(new FixedValueParameter<IntValue>(KParameterName, "The number of nearest neighbours to consider for regression.", new IntValue(3)));
[14235]82      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)"));
[16491]83      Parameters.Add(new FixedValueParameter<BoolValue>(SelfMatchParameterName, "Should we use equal points for classification?", new BoolValue(false)));
[6577]84      Problem = new RegressionProblem();
85    }
[14235]86
[6577]87    [StorableHook(HookType.AfterDeserialization)]
[14235]88    private void AfterDeserialization() {
89      // BackwardsCompatibility3.3
90      #region Backwards compatible code, remove with 3.4
91      if (!Parameters.ContainsKey(WeightsParameterName)) {
92        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)"));
93      }
[16491]94      if (!Parameters.ContainsKey(SelfMatchParameterName)) {
95        Parameters.Add(new FixedValueParameter<BoolValue>(SelfMatchParameterName, "Should we use equal points for classification?", new BoolValue(false)));
96      }
[14235]97      #endregion
98    }
[6577]99
100    public override IDeepCloneable Clone(Cloner cloner) {
[6583]101      return new NearestNeighbourRegression(this, cloner);
[6577]102    }
103
[6583]104    #region nearest neighbour
[14523]105    protected override void Run(CancellationToken cancellationToken) {
[14235]106      double[] weights = null;
107      if (Weights != null) weights = Weights.CloneAsArray();
[16491]108      var solution = CreateNearestNeighbourRegressionSolution(Problem.ProblemData, K, SelfMatch, weights);
[6583]109      Results.Add(new Result(NearestNeighbourRegressionModelResultName, "The nearest neighbour regression solution.", solution));
[6577]110    }
111
[16491]112    public static IRegressionSolution CreateNearestNeighbourRegressionSolution(IRegressionProblemData problemData, int k, bool selfMatch = false, double[] weights = null) {
[8465]113      var clonedProblemData = (IRegressionProblemData)problemData.Clone();
[16491]114      return new NearestNeighbourRegressionSolution(Train(problemData, k, selfMatch, weights), clonedProblemData);
[8465]115    }
[6577]116
[16491]117    public static INearestNeighbourModel Train(IRegressionProblemData problemData, int k, bool selfMatch = false, double[] weights = null) {
[8465]118      return new NearestNeighbourModel(problemData.Dataset,
119        problemData.TrainingIndices,
120        k,
[16491]121        selfMatch,
[8465]122        problemData.TargetVariable,
[14235]123        problemData.AllowedInputVariables,
124        weights);
[6577]125    }
126    #endregion
127  }
128}
Note: See TracBrowser for help on using the repository browser.