Changeset 16408 for branches/2942_KNNRegressionClassification/HeuristicLab.Algorithms.DataAnalysis/3.4/NearestNeighbour/NearestNeighbourModel.cs
- Timestamp:
- 12/19/18 14:56:54 (6 years ago)
- Location:
- branches/2942_KNNRegressionClassification
- Files:
-
- 1 added
- 1 edited
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
branches/2942_KNNRegressionClassification/HeuristicLab.Algorithms.DataAnalysis/3.4/NearestNeighbour/NearestNeighbourModel.cs
r16243 r16408 1 #region License Information1 #region License Information 2 2 /* HeuristicLab 3 3 * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL) … … 37 37 38 38 private readonly object kdTreeLockObject = new object(); 39 39 40 private alglib.nearestneighbor.kdtree kdTree; 40 41 public alglib.nearestneighbor.kdtree KDTree { … … 60 61 [Storable] 61 62 private int k; 63 [Storable] 64 private bool selfMatch; 62 65 [Storable(DefaultValue = null)] 63 66 private double[] weights; // not set for old versions loaded from disk … … 97 100 kdTree.x = (double[])original.kdTree.x.Clone(); 98 101 kdTree.xy = (double[,])original.kdTree.xy.Clone(); 99 102 selfMatch = original.selfMatch; 100 103 k = original.k; 101 104 isCompatibilityLoaded = original.IsCompatibilityLoaded; … … 110 113 this.classValues = (double[])original.classValues.Clone(); 111 114 } 112 public NearestNeighbourModel(IDataset dataset, IEnumerable<int> rows, int k, string targetVariable, IEnumerable<string> allowedInputVariables, IEnumerable<double> weights = null, double[] classValues = null)115 public NearestNeighbourModel(IDataset dataset, IEnumerable<int> rows, int k, bool selfMatch, string targetVariable, IEnumerable<string> allowedInputVariables, IEnumerable<double> weights = null, double[] classValues = null) 113 116 : base(targetVariable) { 114 117 Name = ItemName; 115 118 Description = ItemDescription; 119 this.selfMatch = selfMatch; 116 120 this.k = k; 117 121 this.allowedInputVariables = allowedInputVariables.ToArray(); … … 132 136 .Select(name => { 133 137 var pop = dataset.GetDoubleValues(name, rows).StandardDeviationPop(); 134 return pop.IsAlmost(0) ? 1.0 : 1.0/pop;138 return pop.IsAlmost(0) ? 1.0 : 1.0 / pop; 135 139 }) 136 140 .Concat(new double[] { 1.0 }) // no scaling for target variable … … 201 205 int numNeighbours; 202 206 lock (kdTreeLockObject) { // gkronber: the following calls change the kdTree data structure 203 numNeighbours = alglib.nearestneighbor.kdtreequeryknn(kdTree, x, k, false);207 numNeighbours = alglib.nearestneighbor.kdtreequeryknn(kdTree, x, k, selfMatch); 204 208 alglib.nearestneighbor.kdtreequeryresultsdistances(kdTree, ref dists); 205 209 alglib.nearestneighbor.kdtreequeryresultsxy(kdTree, ref neighbours); 206 210 } 207 211 if (selfMatch) { 212 double minDist = dists[0] + 1; 213 for (int i = 0; i < numNeighbours; i++) { 214 if ((minDist > dists[i]) && (dists[i] != 0)) { 215 minDist = dists[i]; 216 } 217 } 218 minDist /= 100.0; 219 for (int i = 0; i < numNeighbours; i++) { 220 if (dists[i] == 0) { 221 dists[i] = minDist; 222 } 223 } 224 } 208 225 double distanceWeightedValue = 0.0; 209 226 double distsSum = 0.0; … … 238 255 lock (kdTreeLockObject) { 239 256 // gkronber: the following calls change the kdTree data structure 240 numNeighbours = alglib.nearestneighbor.kdtreequeryknn(kdTree, x, k, false);257 numNeighbours = alglib.nearestneighbor.kdtreequeryknn(kdTree, x, k, selfMatch); 241 258 alglib.nearestneighbor.kdtreequeryresultsdistances(kdTree, ref dists); 242 259 alglib.nearestneighbor.kdtreequeryresultsxy(kdTree, ref neighbours);
Note: See TracChangeset
for help on using the changeset viewer.