A custom implementation of the K-Nearest Neighbors (KNN) algorithm from scratch using Python and NumPy, with comparison to scikit-learn's built-in implementation.
This project demonstrates a complete implementation of the 1-Nearest Neighbor algorithm without using any machine learning libraries for the core logic. The implementation includes custom distance calculation, prediction logic, and performance evaluation on Iris and Ionosphere datasets.
- Custom Implementation Accuracy: 97.37%
- Scikit-learn Accuracy: 97.37%
- Test Error Rate: 2.63%
- Custom Implementation Accuracy: 87.50%
- Scikit-learn Accuracy: 87.50%
- Test Error Rate: 12.50%
- Clone the repository:
git clone https://github.com/sourabhmarne777/python-nearest-neighbours-machine-learning-algorithm.git
cd python-nearest-neighbours-machine-learning-algorithm- Install required packages:
pip install -r requirements.txt- Launch Jupyter Notebook:
jupyter notebook "Nearest Neighbour Algorithm.ipynb"Open the Jupyter notebook and run all cells to see the complete implementation. The notebook will load datasets, run the custom KNN implementation, compare results with scikit-learn, and display accuracy metrics.
The NNpredict function implements 1-Nearest Neighbor using Euclidean distance calculation to find the closest training sample for each test sample.
- Iris Dataset: 4 features, 3 classes, 150 samples
- Ionosphere Dataset: 5 features (from 34), 2 classes, binary classification
This project is licensed under the MIT License - see the LICENSE file for details.
Sourabh Marne
- GitHub: @sourabhmarne777