Python Machine Learning

K-nearest neighbors

KNN algorithm

K-nearest neighbors

KNN is a simple, supervised machine learning algorithm that can be used to solve both classification and regression problems.

KNN Example

from sklearn.neighbors import KNeighborsClassifier

X = [[0], [1], [2], [3]]
y = [0, 0, 1, 1]

neigh = KNeighborsClassifier(n_neighbors=3)
neigh.fit(X, y)

print(neigh.predict([[1.1]]))