KNearest Neighbors is one of the most basic yet important classification algorithms in machine learning. It belongs to a supervised learning field and finds wide application in pattern recognition, data mining, and intrusion detection. It is widely available in realworld scenarios because it is nonparametric, meaning it does not make any basic assumptions about the distribution of the data (unlike other algorithms like GMM, which assume a Gaussian distribution of the data).
this article will demonstrate how to implement the Nearest Neighbor Classifier algorithm, using Sklearn library from Python.
Step 1: Import required libraries
import
numpy as np
import
pandas as pd
from
sklearn.model_selection
import train_test_split
from
sklearn.neighbors
import
KNeighborsClassifier
import
matplotlib.pyplot as plt
import
seaborn as sns
Step 2: Read the dataset
Step 3: Train the model


Step 4: Model Evaluation
for
keys, values in
scores.items ():
print
(keys,
`:`
, values)
Now let`s try to find the optimal value for "k", that is, the number of nearest neighbors .
Step 5: Graph learning and test results



From the above scatter plot we can conclude that the optimal k value would be around 5.