A Python implementation of KNN machine learning 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.
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)
To show the package working, I created a jupyter notebook with iris dataset. Take a look into here.