diff --git a/Algorithms/MachineLearning/KNearestNeighbors.cs b/Algorithms/MachineLearning/KNearestNeighbors.cs index fde78326..9138fe78 100644 --- a/Algorithms/MachineLearning/KNearestNeighbors.cs +++ b/Algorithms/MachineLearning/KNearestNeighbors.cs @@ -98,9 +98,9 @@ public TLabel Predict(double[] features) // Majority vote var labelCounts = distances .GroupBy(x => x.Label) - .Select(g => new { Label = g.Key, Count = g.Count() }) + .Select(g => new { Label = g.Key, Count = g.Count(), MinDistance = g.Min(item => item.Distance) }) .OrderByDescending(x => x.Count) - .ThenBy(x => x.Label?.GetHashCode() ?? 0) + .ThenBy(x => x.MinDistance) .ToList(); return labelCounts.First().Label;