This lesson is being piloted (Beta version)

K-Nearest Neighbour

Overview

Teaching: 20 min
Exercises: 0 min
Questions
  • How to use K-Nearest Neighbour in Machine Learning model

Objectives
  • Learn how to use KNN in ML model

13. K-Nearest Neighbour

image

13.1. Explanation

image

image

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)

image

13.3. Other similar models from sklearn.neighbors:

Key Points

  • KNN