source: trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/NearestNeighbour/NearestNeighbourClassification.cs @ 14235

Last change on this file since 14235 was 14235, checked in by gkronber, 3 years ago

#2652: added scaling and optional specification of feature-weights for kNN

File size: 5.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
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 System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Optimization;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30using HeuristicLab.Problems.DataAnalysis;
31
32namespace HeuristicLab.Algorithms.DataAnalysis {
33  /// <summary>
34  /// Nearest neighbour classification data analysis algorithm.
35  /// </summary>
36  [Item("Nearest Neighbour Classification (kNN)", "Nearest neighbour classification data analysis algorithm (wrapper for ALGLIB).")]
37  [Creatable(CreatableAttribute.Categories.DataAnalysisClassification, Priority = 150)]
38  [StorableClass]
39  public sealed class NearestNeighbourClassification : FixedDataAnalysisAlgorithm<IClassificationProblem> {
40    private const string KParameterName = "K";
41    private const string NearestNeighbourClassificationModelResultName = "Nearest neighbour classification solution";
42    private const string WeightsParameterName = "Weights";
43
44
45    #region parameter properties
46    public IFixedValueParameter<IntValue> KParameter {
47      get { return (IFixedValueParameter<IntValue>)Parameters[KParameterName]; }
48    }
49    public IValueParameter<DoubleArray> WeightsParameter {
50      get { return (IValueParameter<DoubleArray>)Parameters[WeightsParameterName]; }
51    }
52    #endregion
53    #region properties
54    public int K {
55      get { return KParameter.Value.Value; }
56      set {
57        if (value <= 0) throw new ArgumentException("K must be larger than zero.", "K");
58        else KParameter.Value.Value = value;
59      }
60    }
61    public DoubleArray Weights {
62      get { return WeightsParameter.Value; }
63      set { WeightsParameter.Value = value; }
64    }
65    #endregion
66
67    [StorableConstructor]
68    private NearestNeighbourClassification(bool deserializing) : base(deserializing) { }
69    private NearestNeighbourClassification(NearestNeighbourClassification original, Cloner cloner)
70      : base(original, cloner) {
71    }
72    public NearestNeighbourClassification()
73      : base() {
74      Parameters.Add(new FixedValueParameter<IntValue>(KParameterName, "The number of nearest neighbours to consider for regression.", new IntValue(3)));
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)"));
76      Problem = new ClassificationProblem();
77    }
78    [StorableHook(HookType.AfterDeserialization)]
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    }
87
88    public override IDeepCloneable Clone(Cloner cloner) {
89      return new NearestNeighbourClassification(this, cloner);
90    }
91
92    #region nearest neighbour
93    protected override void Run() {
94      double[] weights = null;
95      if (Weights != null) weights = Weights.CloneAsArray();
96      var solution = CreateNearestNeighbourClassificationSolution(Problem.ProblemData, K, weights);
97      Results.Add(new Result(NearestNeighbourClassificationModelResultName, "The nearest neighbour classification solution.", solution));
98    }
99
100    public static IClassificationSolution CreateNearestNeighbourClassificationSolution(IClassificationProblemData problemData, int k, double[] weights = null) {
101      var problemDataClone = (IClassificationProblemData)problemData.Clone();
102      return new NearestNeighbourClassificationSolution(Train(problemDataClone, k, weights), problemDataClone);
103    }
104
105    public static INearestNeighbourModel Train(IClassificationProblemData problemData, int k, double[] weights = null) {
106      return new NearestNeighbourModel(problemData.Dataset,
107        problemData.TrainingIndices,
108        k,
109        problemData.TargetVariable,
110        problemData.AllowedInputVariables,
111        weights,
112        problemData.ClassValues.ToArray());
113    }
114    #endregion
115  }
116}
Note: See TracBrowser for help on using the repository browser.