Skip to content

✔️ A Python implementation of KNN machine learning algorithm.

License

Notifications You must be signed in to change notification settings

senavs/knn-from-scratch

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

KNN from scratch

A Python implementation of KNN machine learning algorithm.

Algorithm

K nearest neighbors is a supervised learning algorithm to classification or regression. Considering n points in the cartesian plane, if a new point is placed, its label will be the label of the k nearest neighbors, in other words, the neighbors with least distance. To calculate the distance euclidean distance algorithm is used.

Euclidean distance

Implementation

Point is a class to represent a point in cartesian plane. You are able to sum, subtract, multiply, divide and calculate distance between two points.

from model.point import Point

p1 = Point([7, 4, 3])
p2 = Point([17, 6, 2])

KNearestNeighbors is the model class. Only the methods are allowed: fit and predict. Look into help(KNearestNeighbors) for more infomraiton.

from model.knn import KNearestNeighbors

knn = KNearestNeighbors(k=3)
knn.fit(x_train, y_train)

predict = knn.predict(x_predict)

Apply KNearestNeighbors from scratch in dataset

To show the package working, I created a jupyter notebook with iris dataset. Take a look into here.