Skip to content

Add K-Nearest Neighbors classifier - #7563

Open
poorva0405 wants to merge 1 commit into
TheAlgorithms:masterfrom
poorva0405:feat-knearestneighbors
Open

Add K-Nearest Neighbors classifier#7563
poorva0405 wants to merge 1 commit into
TheAlgorithms:masterfrom
poorva0405:feat-knearestneighbors

Conversation

@poorva0405

@poorva0405 poorva0405 commented Aug 6, 2026

Copy link
Copy Markdown

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

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 93.93939% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 80.47%. Comparing base (7c934ad) to head (3127891).
⚠️ Report is 5 commits behind head on master.

Files with missing lines Patch % Lines
...ealgorithms/machinelearning/KNearestNeighbors.java 93.93% 1 Missing and 3 partials ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master    #7563      +/-   ##
============================================
+ Coverage     80.42%   80.47%   +0.05%     
- Complexity     7457     7488      +31     
============================================
  Files           815      816       +1     
  Lines         24055    24122      +67     
  Branches       4732     4750      +18     
============================================
+ Hits          19346    19412      +66     
  Misses         3945     3945              
- Partials        764      765       +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.

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

2 participants