Skip to content

Comments

Add Manhattan and Minkowski distances to KNN with doctests#13555

Open
kdt523 wants to merge 4 commits intoTheAlgorithms:masterfrom
kdt523:issue-2-knn-distance
Open

Add Manhattan and Minkowski distances to KNN with doctests#13555
kdt523 wants to merge 4 commits intoTheAlgorithms:masterfrom
kdt523:issue-2-knn-distance

Conversation

@kdt523
Copy link

@kdt523 kdt523 commented Oct 17, 2025

This PR extends the K-Nearest Neighbours (KNN) implementation to support additional distance metrics while keeping Euclidean as the default.
What’s included:
Adds distance_metric parameter to KNN: 'euclidean' (default), 'manhattan', 'minkowski'
Adds p parameter for Minkowski (Lp) distance (validated p >= 1)
Implements _manhattan_distance and _minkowski_distance helpers
Updates classify() to use the selected metric
Stores training data as a list to avoid zip exhaustion on multiple calls
Moves the scikit-learn demo under main so importing the module has no extra dependency
Doctests: distances, classification across metrics, and error handling
Backward compatibility:
Existing code remains unchanged by default
(distance_metric='euclidean').
No documentation changes.
How to verify locally:
From the Python folder:
Doctests:
python -m doctest -v [k_nearest_neighbours.py]
Fixes #13546

@kdt523
Copy link
Author

kdt523 commented Oct 24, 2025

Hi @TheAlgorithms/maintainers 👋
This PR Add Manhattan and Minkowski distances to KNN with doctests #13555
Could someone with write access please review and approve it when you get a chance?
Thanks a lot for your time and help!

@kdt523
Copy link
Author

kdt523 commented Oct 24, 2025

Hi @TheAlgorithms/maintainers
This PR #13555 Add Manhattan and Minkowski distances to KNN with doctests
Could someone with write access please review and approve it when you get a chance?
Thanks a lot for your time and help!

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.

Enhancement : Manhattan Distance and Minkowski Distance for K Nearest Neighbours

1 participant