-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Add K-Nearest Neighbor (KNN) implementation and Test #542
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
siriak
merged 4 commits into
TheAlgorithms:master
from
ngtduc693:feature/k-nearest-neighbors
Oct 6, 2025
+195
−0
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a486ee2
Add K-Nearest Neighbor (KNN) implementation and Test
ngtduc693 6b77caa
Merge branch 'master' into feature/k-nearest-neighbors
ngtduc693 36fcaee
Update the Unit test for KNN
ngtduc693 76400bd
Merge branch 'master' into feature/k-nearest-neighbors
siriak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
86 changes: 86 additions & 0 deletions
86
Algorithms.Tests/MachineLearning/KNearestNeighborsTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
using NUnit.Framework; | ||
using Algorithms.MachineLearning; | ||
using System; | ||
|
||
namespace Algorithms.Tests.MachineLearning; | ||
|
||
[TestFixture] | ||
public class KNearestNeighborsTests | ||
{ | ||
[Test] | ||
public void Constructor_InvalidK_ThrowsException() | ||
{ | ||
Assert.Throws<ArgumentOutOfRangeException>(() => new KNearestNeighbors<string>(0)); | ||
} | ||
|
||
[Test] | ||
public void AddSample_NullFeatures_ThrowsException() | ||
{ | ||
var knn = new KNearestNeighbors<string>(3); | ||
double[]? features = null; | ||
Assert.Throws<ArgumentNullException>(() => knn.AddSample(features!, "A")); | ||
} | ||
|
||
[Test] | ||
public void Predict_NoTrainingData_ThrowsException() | ||
{ | ||
var knn = new KNearestNeighbors<string>(1); | ||
Assert.Throws<InvalidOperationException>(() => knn.Predict(new[] { 1.0 })); | ||
} | ||
|
||
[Test] | ||
public void Predict_NullFeatures_ThrowsException() | ||
{ | ||
var knn = new KNearestNeighbors<string>(1); | ||
knn.AddSample(new[] { 1.0 }, "A"); | ||
double[]? features = null; | ||
Assert.Throws<ArgumentNullException>(() => knn.Predict(features!)); | ||
} | ||
|
||
[Test] | ||
public void EuclideanDistance_DifferentLengths_ThrowsException() | ||
{ | ||
Assert.Throws<ArgumentException>(() => KNearestNeighbors<string>.EuclideanDistance(new[] { 1.0 }, new[] { 1.0, 2.0 })); | ||
} | ||
|
||
[Test] | ||
public void EuclideanDistance_CorrectResult() | ||
{ | ||
double[] a = { 1.0, 2.0 }; | ||
double[] b = { 4.0, 6.0 }; | ||
double expected = 5.0; | ||
double actual = KNearestNeighbors<string>.EuclideanDistance(a, b); | ||
Assert.That(actual, Is.EqualTo(expected).Within(1e-9)); | ||
} | ||
|
||
[Test] | ||
public void Predict_SingleNeighbor_CorrectLabel() | ||
{ | ||
var knn = new KNearestNeighbors<string>(1); | ||
knn.AddSample(new double[] { 1.0, 2.0 }, "A"); | ||
knn.AddSample(new double[] { 3.0, 4.0 }, "B"); | ||
var label = knn.Predict(new[] { 1.1, 2.1 }); | ||
Assert.That(label, Is.EqualTo("A")); | ||
} | ||
|
||
[Test] | ||
public void Predict_MajorityVote_CorrectLabel() | ||
{ | ||
var knn = new KNearestNeighbors<string>(3); | ||
knn.AddSample(new double[] { 0.0, 0.0 }, "A"); | ||
knn.AddSample(new double[] { 0.1, 0.1 }, "A"); | ||
knn.AddSample(new double[] { 1.0, 1.0 }, "B"); | ||
var label = knn.Predict(new[] { 0.05, 0.05 }); | ||
Assert.That(label, Is.EqualTo("A")); | ||
} | ||
|
||
[Test] | ||
public void Predict_TieBreaker_ReturnsConsistentLabel() | ||
{ | ||
var knn = new KNearestNeighbors<string>(2); | ||
knn.AddSample(new double[] { 0.0, 0.0 }, "A"); | ||
knn.AddSample(new double[] { 1.0, 1.0 }, "B"); | ||
var label = knn.Predict(new[] { 0.5, 0.5 }); | ||
Assert.That(label, Is.EqualTo("A")); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace Algorithms.MachineLearning; | ||
|
||
/// <summary> | ||
/// K-Nearest Neighbors (KNN) classifier implementation. | ||
/// This algorithm classifies data points based on the majority label of their k nearest neighbors. | ||
/// </summary> | ||
/// <typeparam name="TLabel"> | ||
/// The type of the label used for classification. This can be any type that represents the class or category of a sample. | ||
/// </typeparam> | ||
public class KNearestNeighbors<TLabel> | ||
{ | ||
private readonly List<(double[] Features, TLabel Label)> trainingData = new(); | ||
private readonly int k; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="KNearestNeighbors{TLabel}"/> classifier. | ||
/// </summary> | ||
/// <param name="k">Number of neighbors to consider for classification.</param> | ||
/// <exception cref="ArgumentOutOfRangeException">Thrown if k is less than 1.</exception> | ||
public KNearestNeighbors(int k) | ||
{ | ||
if (k < 1) | ||
{ | ||
throw new ArgumentOutOfRangeException(nameof(k), "k must be at least 1."); | ||
} | ||
|
||
this.k = k; | ||
} | ||
|
||
/// <summary> | ||
/// Calculates the Euclidean distance between two feature vectors. | ||
/// </summary> | ||
/// <param name="a">First feature vector.</param> | ||
/// <param name="b">Second feature vector.</param> | ||
/// <returns>Euclidean distance.</returns> | ||
/// <exception cref="ArgumentException">Thrown if vectors are of different lengths.</exception> | ||
public static double EuclideanDistance(double[] a, double[] b) | ||
{ | ||
if (a.Length != b.Length) | ||
{ | ||
throw new ArgumentException("Feature vectors must be of the same length."); | ||
} | ||
|
||
double sum = 0; | ||
for (int i = 0; i < a.Length; i++) | ||
{ | ||
double diff = a[i] - b[i]; | ||
sum += diff * diff; | ||
} | ||
|
||
return Math.Sqrt(sum); | ||
} | ||
|
||
/// <summary> | ||
/// Adds a training sample to the classifier. | ||
/// </summary> | ||
/// <param name="features">Feature vector of the sample.</param> | ||
/// <param name="label">Label of the sample.</param> | ||
public void AddSample(double[] features, TLabel label) | ||
{ | ||
if (features == null) | ||
{ | ||
throw new ArgumentNullException(nameof(features)); | ||
} | ||
|
||
trainingData.Add((features, label)); | ||
} | ||
|
||
/// <summary> | ||
/// Predicts the label for a given feature vector using the KNN algorithm. | ||
/// </summary> | ||
/// <param name="features">Feature vector to classify.</param> | ||
/// <returns>Predicted label.</returns> | ||
/// <exception cref="InvalidOperationException">Thrown if there is no training data.</exception> | ||
public TLabel Predict(double[] features) | ||
{ | ||
if (trainingData.Count == 0) | ||
{ | ||
throw new InvalidOperationException("No training data available."); | ||
} | ||
|
||
if (features == null) | ||
{ | ||
throw new ArgumentNullException(nameof(features)); | ||
} | ||
|
||
// Compute distances to all training samples | ||
var distances = trainingData | ||
.Select(td => (Label: td.Label, Distance: EuclideanDistance(features, td.Features))) | ||
.OrderBy(x => x.Distance) | ||
.Take(k) | ||
.ToList(); | ||
|
||
// Majority vote | ||
var labelCounts = distances | ||
.GroupBy(x => x.Label) | ||
.Select(g => new { Label = g.Key, Count = g.Count() }) | ||
.OrderByDescending(x => x.Count) | ||
.ThenBy(x => x.Label?.GetHashCode() ?? 0) | ||
.ToList(); | ||
|
||
return labelCounts.First().Label; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.