KNearest Neighbors - Nori12/Machine-Learning-Tutorial GitHub Wiki

Machine Learning Tutorial

KNearest Neighbors

It is the simplest algorithm in machine learning. To make a prediction for a new data point, the algorithm finds the closest data points in the training dataset—its “nearest neighbors.”. Building the model consists only of storing the training dataset.

The code can follow the generic formula below:

from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier

iris_dataset = load_iris()

X_train, X_test, y_train, y_test = train_test_split(
    iris_dataset['data'], iris_dataset['target'], random_state=0)

knn = KNeighborsClassifier(n_neighbors=1)
knn.fit(X_train, y_train)

print("Test set score: {:.2f}".format(knn.score(X_test, y_test)))

# Test set score: 0.97
KNN
KNN Algorithm for Classification when n_neighbors = 1
KNN
KNN Algorithm for Classification when n_neighbors = 3
KNN
KNN Algorithm for Regression when n_neighbors = 3