Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 18190 was 17931, checked in by gkronber, 4 years ago

#3117: update alglib to version 3.17

File size: 5.0 KB
RevLine 
[6577]1#region License Information
2/* HeuristicLab
[17180]3 * Copyright (C) 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;
[16565]29using HEAL.Attic;
[6577]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)]
[16565]38  [StorableType("3F940BE0-4F44-4F7F-A3EE-E47423C7F22D")]
[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";
[6578]43
44    #region parameter properties
[6583]45    public IFixedValueParameter<IntValue> KParameter {
46      get { return (IFixedValueParameter<IntValue>)Parameters[KParameterName]; }
[6578]47    }
[14235]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    }
[14235]60    public DoubleArray Weights {
61      get { return WeightsParameter.Value; }
62      set { WeightsParameter.Value = value; }
63    }
[6578]64    #endregion
65
[6577]66    [StorableConstructor]
[16565]67    private NearestNeighbourRegression(StorableConstructorFlag _) : base(_) { }
[6583]68    private NearestNeighbourRegression(NearestNeighbourRegression original, Cloner cloner)
[6577]69      : base(original, cloner) {
70    }
[6583]71    public NearestNeighbourRegression()
[6577]72      : base() {
[6583]73      Parameters.Add(new FixedValueParameter<IntValue>(KParameterName, "The number of nearest neighbours to consider for regression.", new IntValue(3)));
[14235]74      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]75      Problem = new RegressionProblem();
76    }
[14235]77
[6577]78    [StorableHook(HookType.AfterDeserialization)]
[14235]79    private void AfterDeserialization() {
80      // BackwardsCompatibility3.3
81      #region Backwards compatible code, remove with 3.4
82      if (!Parameters.ContainsKey(WeightsParameterName)) {
83        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)"));
84      }
85      #endregion
86    }
[6577]87
88    public override IDeepCloneable Clone(Cloner cloner) {
[6583]89      return new NearestNeighbourRegression(this, cloner);
[6577]90    }
91
[6583]92    #region nearest neighbour
[14523]93    protected override void Run(CancellationToken cancellationToken) {
[14235]94      double[] weights = null;
95      if (Weights != null) weights = Weights.CloneAsArray();
[17931]96      var solution = CreateNearestNeighbourRegressionSolution(Problem.ProblemData, K, weights);
[6583]97      Results.Add(new Result(NearestNeighbourRegressionModelResultName, "The nearest neighbour regression solution.", solution));
[6577]98    }
99
[17931]100    public static IRegressionSolution CreateNearestNeighbourRegressionSolution(IRegressionProblemData problemData, int k, double[] weights = null) {
[8465]101      var clonedProblemData = (IRegressionProblemData)problemData.Clone();
[17931]102      return new NearestNeighbourRegressionSolution(Train(problemData, k, weights), clonedProblemData);
[8465]103    }
[6577]104
[17931]105    public static INearestNeighbourModel Train(IRegressionProblemData problemData, int k, double[] weights = null) {
[8465]106      return new NearestNeighbourModel(problemData.Dataset,
107        problemData.TrainingIndices,
108        k,
109        problemData.TargetVariable,
[14235]110        problemData.AllowedInputVariables,
111        weights);
[6577]112    }
113    #endregion
114  }
115}
Note: See TracBrowser for help on using the repository browser.