From 023fb501b823135c60d02ae49f6066196da2f7f3 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 31 Oct 2025 08:58:24 +0000 Subject: [PATCH 1/2] Initial plan From 5e3b845bc45f39ef45a8e49e161bd07edc5c6452 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 31 Oct 2025 09:04:47 +0000 Subject: [PATCH 2/2] Fix non-deterministic tie-breaking in KNN algorithm Co-authored-by: siriak <29201949+siriak@users.noreply.github.com> --- Algorithms/MachineLearning/KNearestNeighbors.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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;