Skip to content

Add Knn algo in machine learning #643

@lck6055

Description

@lck6055

Algorithm Name

KNN - Algorithm ( machine learning )

Programming Language

Python

Category

Other

Difficulty Level

Medium (Intermediate)

Algorithm Description

-Nearest Neighbors (KNN) - Simple Implementation from Scratch

This script implements a basic version of the KNN algorithm for classification
using only Python and NumPy (no sklearn).

Concept Summary:

  1. KNN is a supervised learning algorithm used for classification & regression.
  2. It finds the K nearest data points to a test point using a distance metric
    (usually Euclidean distance).
  3. For classification → predicts the majority label among neighbors.
  4. For regression → predicts the average value among neighbors.
  5. It is a lazy learner (no explicit training phase, prediction happens at query time).

Steps in this code:

  1. Compute Euclidean distance between the test point and all training points.
  2. Sort training points by their distance to the test point.
  3. Select top 'k' nearest points.
  4. Use majority voting to determine the predicted class.
  5. Return the predicted label.

Author / Contributor:

💻 Contributed by: Lakhinana Chaturvedi Kashyap

References (Optional)

No response

Contribution Intent

  • I would like to implement this algorithm myself
  • I'm requesting this for someone else to implement
  • I need help implementing this algorithm

Code of Conduct

  • I agree to follow this project's Code of Conduct

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions