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]]))

Machine Learning — Knowledge Check

Check your understanding of this section. Results are not saved—refresh the page to start over.

1. Supervised learning uses:
2. Train/test split helps you:
3. Linear regression predicts:
4. A confusion matrix shows:
5. K-means is an example of:
6. Cross-validation is used to: