Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 11171 was 11171, checked in by ascheibe, 10 years ago

#2115 merged r11170 (copyright update) into trunk

File size: 3.9 KB
RevLine 
[6577]1#region License Information
2/* HeuristicLab
[11171]3 * Copyright (C) 2002-2014 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 System.Linq;
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 classification data analysis algorithm.
[6577]35  /// </summary>
[6583]36  [Item("Nearest Neighbour Classification", "Nearest neighbour classification data analysis algorithm (wrapper for ALGLIB).")]
[6577]37  [Creatable("Data Analysis")]
38  [StorableClass]
[6583]39  public sealed class NearestNeighbourClassification : FixedDataAnalysisAlgorithm<IClassificationProblem> {
40    private const string KParameterName = "K";
41    private const string NearestNeighbourClassificationModelResultName = "Nearest neighbour classification solution";
[6578]42
43    #region parameter properties
[6583]44    public IFixedValueParameter<IntValue> KParameter {
45      get { return (IFixedValueParameter<IntValue>)Parameters[KParameterName]; }
[6578]46    }
47    #endregion
48    #region properties
[6583]49    public int K {
50      get { return KParameter.Value.Value; }
[6578]51      set {
[6583]52        if (value <= 0) throw new ArgumentException("K must be larger than zero.", "K");
53        else KParameter.Value.Value = value;
[6578]54      }
55    }
56    #endregion
57
[6577]58    [StorableConstructor]
[6583]59    private NearestNeighbourClassification(bool deserializing) : base(deserializing) { }
60    private NearestNeighbourClassification(NearestNeighbourClassification original, Cloner cloner)
[6577]61      : base(original, cloner) {
62    }
[6583]63    public NearestNeighbourClassification()
[6577]64      : base() {
[6583]65      Parameters.Add(new FixedValueParameter<IntValue>(KParameterName, "The number of nearest neighbours to consider for regression.", new IntValue(3)));
66      Problem = new ClassificationProblem();
[6577]67    }
68    [StorableHook(HookType.AfterDeserialization)]
69    private void AfterDeserialization() { }
70
71    public override IDeepCloneable Clone(Cloner cloner) {
[6583]72      return new NearestNeighbourClassification(this, cloner);
[6577]73    }
74
[6583]75    #region nearest neighbour
[6577]76    protected override void Run() {
[6583]77      var solution = CreateNearestNeighbourClassificationSolution(Problem.ProblemData, K);
78      Results.Add(new Result(NearestNeighbourClassificationModelResultName, "The nearest neighbour classification solution.", solution));
[6577]79    }
80
[6583]81    public static IClassificationSolution CreateNearestNeighbourClassificationSolution(IClassificationProblemData problemData, int k) {
[8465]82      var problemDataClone = (IClassificationProblemData)problemData.Clone();
83      return new NearestNeighbourClassificationSolution(problemDataClone, Train(problemDataClone, k));
84    }
[6577]85
[8465]86    public static INearestNeighbourModel Train(IClassificationProblemData problemData, int k) {
87      return new NearestNeighbourModel(problemData.Dataset,
88        problemData.TrainingIndices,
89        k,
90        problemData.TargetVariable,
91        problemData.AllowedInputVariables,
92        problemData.ClassValues.ToArray());
[6577]93    }
94    #endregion
95  }
96}
Note: See TracBrowser for help on using the repository browser.