What would you like to Propose?
I propose adding a K-Nearest Neighbors (KNN) implementation to the machinelearning package.
The machinelearning package was recently introduced and currently contains only a few supervised learning algorithms. K-Nearest Neighbors is one of the most fundamental machine learning algorithms and is commonly taught alongside Linear Regression and Naive Bayes in introductory machine learning courses. Adding it would naturally expand the package with another core classification algorithm.
KNN is a good fit for this repository because:
- It is one of the most widely used supervised learning algorithms for classification.
- It can be implemented entirely using standard Java (java.util) without any external dependencies, matching the educational and dependency-free style of this repository.
- The algorithm is straightforward to understand and demonstrates important concepts such as distance metrics, nearest-neighbor search, and majority voting.
- It complements the existing LinearRegression and MultinomialNaiveBayesClassifier implementations already present in the machinelearning package.
Issue details
Algorithm / problem statement: Given a set of labeled training samples and a value of k, classify an unseen sample by computing its Euclidean distance to every training sample, selecting the k nearest neighbors, and assigning the class that receives the majority vote.
-
src/main/java/com/thealgorithms/machinelearning/KNearestNeighbors.java
-
Store training data through a fit() method.
-
Predict the class of a single sample using Euclidean distance.
-
Predict class labels for multiple samples.
-
Majority-vote classification with deterministic tie-breaking (smaller class label wins).
-
Input validation for:
- k <= 0
- null or empty datasets
- inconsistent feature dimensions
- mismatched feature/label counts
- prediction before fitting
-
src/test/java/com/thealgorithms/machinelearning/KNearestNeighborsTest.java
-
Unit tests on well-separated datasets.
-
Batch prediction tests.
-
Tests for invalid inputs and exception handling.
-
Tie-breaking behavior.
-
Prediction before fitting.
-
Dimension mismatch tests.
-
The implementation will include Javadocs describing the algorithm, its time complexity (O(n · d + n log n) in this implementation because neighbors are sorted), and a reference to the Wikipedia article.
Additional Information
I searched the repository and existing pull requests and could not find a K-Nearest Neighbors implementation in the Java repository. A previous pull request (#2484) titled "KNN Algorithm" was closed as invalid because it did not contain an actual KNN implementation.
If this proposal is accepted, I'd be happy to implement the algorithm along with comprehensive JUnit 5 tests and documentation following the repository's coding standards.
What would you like to Propose?
I propose adding a K-Nearest Neighbors (KNN) implementation to the machinelearning package.
The machinelearning package was recently introduced and currently contains only a few supervised learning algorithms. K-Nearest Neighbors is one of the most fundamental machine learning algorithms and is commonly taught alongside Linear Regression and Naive Bayes in introductory machine learning courses. Adding it would naturally expand the package with another core classification algorithm.
KNN is a good fit for this repository because:
Issue details
Algorithm / problem statement: Given a set of labeled training samples and a value of k, classify an unseen sample by computing its Euclidean distance to every training sample, selecting the k nearest neighbors, and assigning the class that receives the majority vote.
src/main/java/com/thealgorithms/machinelearning/KNearestNeighbors.java
Store training data through a fit() method.
Predict the class of a single sample using Euclidean distance.
Predict class labels for multiple samples.
Majority-vote classification with deterministic tie-breaking (smaller class label wins).
Input validation for:
src/test/java/com/thealgorithms/machinelearning/KNearestNeighborsTest.java
Unit tests on well-separated datasets.
Batch prediction tests.
Tests for invalid inputs and exception handling.
Tie-breaking behavior.
Prediction before fitting.
Dimension mismatch tests.
The implementation will include Javadocs describing the algorithm, its time complexity (O(n · d + n log n) in this implementation because neighbors are sorted), and a reference to the Wikipedia article.
Additional Information
I searched the repository and existing pull requests and could not find a K-Nearest Neighbors implementation in the Java repository. A previous pull request (#2484) titled "KNN Algorithm" was closed as invalid because it did not contain an actual KNN implementation.
If this proposal is accepted, I'd be happy to implement the algorithm along with comprehensive JUnit 5 tests and documentation following the repository's coding standards.