[17164] | 1 | #region License Information
|
---|
[6577] | 2 | /* HeuristicLab
|
---|
[17181] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Linq;
|
---|
[15061] | 24 | using System.Threading;
|
---|
[6577] | 25 | using HeuristicLab.Common;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
| 27 | using HeuristicLab.Data;
|
---|
| 28 | using HeuristicLab.Optimization;
|
---|
[8465] | 29 | using HeuristicLab.Parameters;
|
---|
[17097] | 30 | using HEAL.Attic;
|
---|
[6577] | 31 | using HeuristicLab.Problems.DataAnalysis;
|
---|
| 32 |
|
---|
| 33 | namespace HeuristicLab.Algorithms.DataAnalysis {
|
---|
| 34 | /// <summary>
|
---|
[6583] | 35 | /// Nearest neighbour classification data analysis algorithm.
|
---|
[6577] | 36 | /// </summary>
|
---|
[13297] | 37 | [Item("Nearest Neighbour Classification (kNN)", "Nearest neighbour classification data analysis algorithm (wrapper for ALGLIB).")]
|
---|
[12708] | 38 | [Creatable(CreatableAttribute.Categories.DataAnalysisClassification, Priority = 150)]
|
---|
[17097] | 39 | [StorableType("98161D6F-D977-45EA-B899-E47EE017865E")]
|
---|
[6583] | 40 | public sealed class NearestNeighbourClassification : FixedDataAnalysisAlgorithm<IClassificationProblem> {
|
---|
| 41 | private const string KParameterName = "K";
|
---|
| 42 | private const string NearestNeighbourClassificationModelResultName = "Nearest neighbour classification solution";
|
---|
[14308] | 43 | private const string WeightsParameterName = "Weights";
|
---|
[17164] | 44 | private const string SelfMatchParameterName = "SelfMatch";
|
---|
[6578] | 45 |
|
---|
| 46 | #region parameter properties
|
---|
[6583] | 47 | public IFixedValueParameter<IntValue> KParameter {
|
---|
| 48 | get { return (IFixedValueParameter<IntValue>)Parameters[KParameterName]; }
|
---|
[6578] | 49 | }
|
---|
[17164] | 50 | public IFixedValueParameter<BoolValue> SelfMatchParameter {
|
---|
| 51 | get { return (IFixedValueParameter<BoolValue>)Parameters[SelfMatchParameterName]; }
|
---|
| 52 | }
|
---|
[14308] | 53 | public IValueParameter<DoubleArray> WeightsParameter {
|
---|
| 54 | get { return (IValueParameter<DoubleArray>)Parameters[WeightsParameterName]; }
|
---|
| 55 | }
|
---|
[6578] | 56 | #endregion
|
---|
| 57 | #region properties
|
---|
[17164] | 58 | public bool SelfMatch {
|
---|
| 59 | get { return SelfMatchParameter.Value.Value; }
|
---|
| 60 | set { SelfMatchParameter.Value.Value = value; }
|
---|
| 61 | }
|
---|
[6583] | 62 | public int K {
|
---|
| 63 | get { return KParameter.Value.Value; }
|
---|
[6578] | 64 | set {
|
---|
[6583] | 65 | if (value <= 0) throw new ArgumentException("K must be larger than zero.", "K");
|
---|
| 66 | else KParameter.Value.Value = value;
|
---|
[6578] | 67 | }
|
---|
| 68 | }
|
---|
[14308] | 69 | public DoubleArray Weights {
|
---|
| 70 | get { return WeightsParameter.Value; }
|
---|
| 71 | set { WeightsParameter.Value = value; }
|
---|
| 72 | }
|
---|
[6578] | 73 | #endregion
|
---|
| 74 |
|
---|
[6577] | 75 | [StorableConstructor]
|
---|
[17097] | 76 | private NearestNeighbourClassification(StorableConstructorFlag _) : base(_) { }
|
---|
[6583] | 77 | private NearestNeighbourClassification(NearestNeighbourClassification original, Cloner cloner)
|
---|
[6577] | 78 | : base(original, cloner) {
|
---|
| 79 | }
|
---|
[6583] | 80 | public NearestNeighbourClassification()
|
---|
[6577] | 81 | : base() {
|
---|
[17164] | 82 | Parameters.Add(new FixedValueParameter<BoolValue>(SelfMatchParameterName, "Should we use equal points for classification?", new BoolValue(false)));
|
---|
[6583] | 83 | Parameters.Add(new FixedValueParameter<IntValue>(KParameterName, "The number of nearest neighbours to consider for regression.", new IntValue(3)));
|
---|
[14308] | 84 | 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)"));
|
---|
[6583] | 85 | Problem = new ClassificationProblem();
|
---|
[6577] | 86 | }
|
---|
| 87 | [StorableHook(HookType.AfterDeserialization)]
|
---|
[14308] | 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 | }
|
---|
[17164] | 94 | if (!Parameters.ContainsKey(SelfMatchParameterName)) {
|
---|
| 95 | Parameters.Add(new FixedValueParameter<BoolValue>(SelfMatchParameterName, "Should we use equal points for classification?", new BoolValue(false)));
|
---|
| 96 | }
|
---|
[14308] | 97 | #endregion
|
---|
| 98 | }
|
---|
[6577] | 99 |
|
---|
| 100 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[6583] | 101 | return new NearestNeighbourClassification(this, cloner);
|
---|
[6577] | 102 | }
|
---|
| 103 |
|
---|
[6583] | 104 | #region nearest neighbour
|
---|
[15061] | 105 | protected override void Run(CancellationToken cancellationToken) {
|
---|
[14308] | 106 | double[] weights = null;
|
---|
| 107 | if (Weights != null) weights = Weights.CloneAsArray();
|
---|
[17164] | 108 | var solution = CreateNearestNeighbourClassificationSolution(Problem.ProblemData, K, SelfMatch, weights);
|
---|
[6583] | 109 | Results.Add(new Result(NearestNeighbourClassificationModelResultName, "The nearest neighbour classification solution.", solution));
|
---|
[6577] | 110 | }
|
---|
| 111 |
|
---|
[17164] | 112 | public static IClassificationSolution CreateNearestNeighbourClassificationSolution(IClassificationProblemData problemData, int k, bool selfMatch = false, double[] weights = null) {
|
---|
[8465] | 113 | var problemDataClone = (IClassificationProblemData)problemData.Clone();
|
---|
[17164] | 114 | return new NearestNeighbourClassificationSolution(Train(problemDataClone, k, selfMatch, weights), problemDataClone);
|
---|
[8465] | 115 | }
|
---|
[6577] | 116 |
|
---|
[17164] | 117 | public static INearestNeighbourModel Train(IClassificationProblemData problemData, int k, bool selfMatch = false, double[] weights = null) {
|
---|
[8465] | 118 | return new NearestNeighbourModel(problemData.Dataset,
|
---|
| 119 | problemData.TrainingIndices,
|
---|
| 120 | k,
|
---|
[17164] | 121 | selfMatch,
|
---|
[8465] | 122 | problemData.TargetVariable,
|
---|
| 123 | problemData.AllowedInputVariables,
|
---|
[14308] | 124 | weights,
|
---|
[8465] | 125 | problemData.ClassValues.ToArray());
|
---|
[6577] | 126 | }
|
---|
| 127 | #endregion
|
---|
| 128 | }
|
---|
| 129 | }
|
---|