Machine Learning A-Z: Part 3 – Classification (K-Nearest Neighbors)

K-NN Intuition

How do we classify a new data point between category 1 and 2?
K-NN identifies which category the new data point should be in.

STEP 1: Choose the number K of neighbors

STEP 2: Take the K nearest neighbors of the new data point, according to the Euclidean distance

STEP 3: Among these K neighbors, count the number of data points in each category

STEP 4: Assign the new data point to the category where you counted the most neighbors

Euclidean Distance

2 points:
P1(x1, y1)
P2(x2, y2)

Euclidean Distance between P1 and P2 = √((x2 – x1)2 + (y2 – y1)2)

Implementation

Python

R

ページトップへ