K-Nearest Neighbour
Overview
Teaching: 20 min
Exercises: 0 minQuestions
How to use K-Nearest Neighbour in Machine Learning model
Objectives
Learn how to use KNN in ML model
13. K-Nearest Neighbour
- Simplicity but powerful and fast for certain task
- Work for both classification and regression
- Named as Instance Based Learning; Non-parametrics; Lazy learner
- Work well with small number of inputs

13.1. Explanation

- In KNN, the most important parameter is the K group and the distance computed between points.
- Euclide distance:

13.2. Implementation
Here we gonna use the iris dataset again:
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
import numpy as np
import pandas as pd
iris = load_iris()
X = iris.data
y = iris.target
X_train, X_test, y_train, y_test = train_test_split(X,y,train_size=0.6,random_state=123)
Train the model using KNN model with 3 nearest neighbors
from sklearn.neighbors import KNeighborsClassifier
model_KNN = KNeighborsClassifier(n_neighbors=3).fit(X_train,y_train)
model_KNN.score(X_train,y_train)
model_KNN.score(X_test,y_test)

13.3. Other similar models from sklearn.neighbors:
- KNeighborsRegressor: KNN estimators for Regression problem with continuous data
- NearestCentroid
- and more
Key Points
KNN