Skip to content

Add K-Nearest Neighbors classifier - #7563

Merged
alxkm merged 8 commits into
TheAlgorithms:masterfrom
poorva0405:feat-knearestneighbors
Sep 4, 2026
Merged

Add K-Nearest Neighbors classifier#7563
alxkm merged 8 commits into
TheAlgorithms:masterfrom
poorva0405:feat-knearestneighbors

Conversation

@poorva0405

@poorva0405 poorva0405 commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

Description

This PR adds a K-Nearest Neighbors (KNN) classifier implementation to the machinelearning package.

Features

  • Implements KNN classification using Euclidean distance.
  • Supports training through fit().
  • Supports prediction for single and batch samples.
  • Uses deterministic tie-breaking when multiple classes receive the same number of votes.
  • Includes comprehensive input validation.
  • Includes JUnit 5 tests covering normal and edge cases.
  • Includes Javadocs for all public APIs.

Closes #7562

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized it.
  • All filenames are in PascalCase.
  • All functions and variable names follow Java naming conventions.
  • All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
  • All new algorithms include a corresponding test class that validates their functionality.
  • All new code is formatted with clang-format -i --style=file path/to/your/file.java

@codecov-commenter

codecov-commenter commented Aug 6, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 96.92308% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 80.66%. Comparing base (6ec6339) to head (d75cae5).

Files with missing lines Patch % Lines
...ealgorithms/machinelearning/KNearestNeighbors.java 96.92% 0 Missing and 2 partials ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master    #7563      +/-   ##
============================================
+ Coverage     80.61%   80.66%   +0.04%     
- Complexity     7497     7526      +29     
============================================
  Files           816      817       +1     
  Lines         24097    24162      +65     
  Branches       4741     4759      +18     
============================================
+ Hits          19427    19491      +64     
  Misses         3907     3907              
- Partials        763      764       +1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@AKASH02-byte AKASH02-byte left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left an inline comment regarding exception checking in KNearestNeighbors.java. Overall, the implementation and tests look clean!

Comment thread src/main/java/com/thealgorithms/machinelearning/KNearestNeighbors.java Outdated

@alxkm alxkm left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Solid PR - clean implementation, good tests.

One change requested: move the k > trainingFeatures.length check into fit(), per the thread on line 168.

Improvements (non-blocking):

  • fields are declared mid-class, move them to the top next to k
  • fit() stores the caller's arrays directly — a defensive copy would be safer
  • the two "not fitted" null checks in predict() can be collapsed into one
  • Neighbor could be a record

Nice tests coverage.

Comment thread src/main/java/com/thealgorithms/machinelearning/KNearestNeighbors.java Outdated
@poorva0405

Copy link
Copy Markdown
Contributor Author

Hi @alxkm, I’ve addressed the requested changes, including moving the k > trainingFeatures.length validation to fit() and implementing the additional improvements discussed.

All 7 CI checks are now passing. Could you please take another look and re-review the PR when you get a chance?

Thank you for the feedback!

@alxkm alxkm left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Thank you for the contribution.

@alxkm
alxkm merged commit 0a6c756 into TheAlgorithms:master Sep 4, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEATURE REQUEST] <title> Add K-Nearest Neighbors (KNN) Classification Algorithm

4 participants