1  #region License Information


2  /* HeuristicLab


3  * Copyright (C) 20022018 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 


22  using System;


23  using System.Linq;


24  using System.Threading;


25  using HeuristicLab.Common;


26  using HeuristicLab.Core;


27  using HeuristicLab.Data;


28  using HeuristicLab.Optimization;


29  using HeuristicLab.Parameters;


30  using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;


31  using HeuristicLab.Problems.DataAnalysis;


32 


33  namespace HeuristicLab.Algorithms.DataAnalysis {


34  /// <summary>


35  /// Nearest neighbour classification data analysis algorithm.


36  /// </summary>


37  [Item("Nearest Neighbour Classification (kNN)", "Nearest neighbour classification data analysis algorithm (wrapper for ALGLIB).")]


38  [Creatable(CreatableAttribute.Categories.DataAnalysisClassification, Priority = 150)]


39  [StorableClass]


40  public sealed class NearestNeighbourClassification : FixedDataAnalysisAlgorithm<IClassificationProblem> {


41  private const string KParameterName = "K";


42  private const string NearestNeighbourClassificationModelResultName = "Nearest neighbour classification solution";


43  private const string WeightsParameterName = "Weights";


44 


45 


46  #region parameter properties


47  public IFixedValueParameter<IntValue> KParameter {


48  get { return (IFixedValueParameter<IntValue>)Parameters[KParameterName]; }


49  }


50  public IValueParameter<DoubleArray> WeightsParameter {


51  get { return (IValueParameter<DoubleArray>)Parameters[WeightsParameterName]; }


52  }


53  #endregion


54  #region properties


55  public int K {


56  get { return KParameter.Value.Value; }


57  set {


58  if (value <= 0) throw new ArgumentException("K must be larger than zero.", "K");


59  else KParameter.Value.Value = value;


60  }


61  }


62  public DoubleArray Weights {


63  get { return WeightsParameter.Value; }


64  set { WeightsParameter.Value = value; }


65  }


66  #endregion


67 


68  [StorableConstructor]


69  private NearestNeighbourClassification(bool deserializing) : base(deserializing) { }


70  private NearestNeighbourClassification(NearestNeighbourClassification original, Cloner cloner)


71  : base(original, cloner) {


72  }


73  public NearestNeighbourClassification()


74  : base() {


75  Parameters.Add(new FixedValueParameter<IntValue>(KParameterName, "The number of nearest neighbours to consider for regression.", new IntValue(3)));


76  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)"));


77  Problem = new ClassificationProblem();


78  }


79  [StorableHook(HookType.AfterDeserialization)]


80  private void AfterDeserialization() {


81  // BackwardsCompatibility3.3


82  #region Backwards compatible code, remove with 3.4


83  if (!Parameters.ContainsKey(WeightsParameterName)) {


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)"));


85  }


86  #endregion


87  }


88 


89  public override IDeepCloneable Clone(Cloner cloner) {


90  return new NearestNeighbourClassification(this, cloner);


91  }


92 


93  #region nearest neighbour


94  protected override void Run(CancellationToken cancellationToken) {


95  double[] weights = null;


96  if (Weights != null) weights = Weights.CloneAsArray();


97  var solution = CreateNearestNeighbourClassificationSolution(Problem.ProblemData, K, weights);


98  Results.Add(new Result(NearestNeighbourClassificationModelResultName, "The nearest neighbour classification solution.", solution));


99  }


100 


101  public static IClassificationSolution CreateNearestNeighbourClassificationSolution(IClassificationProblemData problemData, int k, double[] weights = null) {


102  var problemDataClone = (IClassificationProblemData)problemData.Clone();


103  return new NearestNeighbourClassificationSolution(Train(problemDataClone, k, weights), problemDataClone);


104  }


105 


106  public static INearestNeighbourModel Train(IClassificationProblemData problemData, int k, double[] weights = null) {


107  return new NearestNeighbourModel(problemData.Dataset,


108  problemData.TrainingIndices,


109  k,


110  problemData.TargetVariable,


111  problemData.AllowedInputVariables,


112  weights,


113  problemData.ClassValues.ToArray());


114  }


115  #endregion


116  }


117  }

