[16491] | 1 | #region License Information
|
---|
[6577] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Linq;
|
---|
[14523] | 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;
|
---|
[16565] | 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>
|
---|
[13238] | 37 | [Item("Nearest Neighbour Classification (kNN)", "Nearest neighbour classification data analysis algorithm (wrapper for ALGLIB).")]
|
---|
[12504] | 38 | [Creatable(CreatableAttribute.Categories.DataAnalysisClassification, Priority = 150)]
|
---|
[16565] | 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";
|
---|
[14235] | 43 | private const string WeightsParameterName = "Weights";
|
---|
[6578] | 44 |
|
---|
| 45 | #region parameter properties
|
---|
[6583] | 46 | public IFixedValueParameter<IntValue> KParameter {
|
---|
| 47 | get { return (IFixedValueParameter<IntValue>)Parameters[KParameterName]; }
|
---|
[6578] | 48 | }
|
---|
[14235] | 49 | public IValueParameter<DoubleArray> WeightsParameter {
|
---|
| 50 | get { return (IValueParameter<DoubleArray>)Parameters[WeightsParameterName]; }
|
---|
| 51 | }
|
---|
[6578] | 52 | #endregion
|
---|
| 53 | #region properties
|
---|
[6583] | 54 | public int K {
|
---|
| 55 | get { return KParameter.Value.Value; }
|
---|
[6578] | 56 | set {
|
---|
[6583] | 57 | if (value <= 0) throw new ArgumentException("K must be larger than zero.", "K");
|
---|
| 58 | else KParameter.Value.Value = value;
|
---|
[6578] | 59 | }
|
---|
| 60 | }
|
---|
[14235] | 61 | public DoubleArray Weights {
|
---|
| 62 | get { return WeightsParameter.Value; }
|
---|
| 63 | set { WeightsParameter.Value = value; }
|
---|
| 64 | }
|
---|
[6578] | 65 | #endregion
|
---|
| 66 |
|
---|
[6577] | 67 | [StorableConstructor]
|
---|
[16565] | 68 | private NearestNeighbourClassification(StorableConstructorFlag _) : base(_) { }
|
---|
[6583] | 69 | private NearestNeighbourClassification(NearestNeighbourClassification original, Cloner cloner)
|
---|
[6577] | 70 | : base(original, cloner) {
|
---|
| 71 | }
|
---|
[6583] | 72 | public NearestNeighbourClassification()
|
---|
[6577] | 73 | : base() {
|
---|
[6583] | 74 | Parameters.Add(new FixedValueParameter<IntValue>(KParameterName, "The number of nearest neighbours to consider for regression.", new IntValue(3)));
|
---|
[14235] | 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)"));
|
---|
[6583] | 76 | Problem = new ClassificationProblem();
|
---|
[6577] | 77 | }
|
---|
| 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 NearestNeighbourClassification(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 = CreateNearestNeighbourClassificationSolution(Problem.ProblemData, K, weights);
|
---|
[6583] | 97 | Results.Add(new Result(NearestNeighbourClassificationModelResultName, "The nearest neighbour classification solution.", solution));
|
---|
[6577] | 98 | }
|
---|
| 99 |
|
---|
[17931] | 100 | public static IClassificationSolution CreateNearestNeighbourClassificationSolution(IClassificationProblemData problemData, int k, double[] weights = null) {
|
---|
[8465] | 101 | var problemDataClone = (IClassificationProblemData)problemData.Clone();
|
---|
[17931] | 102 | return new NearestNeighbourClassificationSolution(Train(problemDataClone, k, weights), problemDataClone);
|
---|
[8465] | 103 | }
|
---|
[6577] | 104 |
|
---|
[17931] | 105 | public static INearestNeighbourModel Train(IClassificationProblemData problemData, int k, double[] weights = null) {
|
---|
[8465] | 106 | return new NearestNeighbourModel(problemData.Dataset,
|
---|
| 107 | problemData.TrainingIndices,
|
---|
| 108 | k,
|
---|
| 109 | problemData.TargetVariable,
|
---|
| 110 | problemData.AllowedInputVariables,
|
---|
[14235] | 111 | weights,
|
---|
[8465] | 112 | problemData.ClassValues.ToArray());
|
---|
[6577] | 113 | }
|
---|
| 114 | #endregion
|
---|
| 115 | }
|
---|
| 116 | }
|
---|