University of Relizane – Faculty of Sciences and Technology – Department of Computer Science
Master Degree M1 – Semester 01
Machine Learning
Laboratory Session
KNN in Python
Tools: Python. IDE: PyCharm 4. Vote for Class Labels
K-Nearest Neighbor • For classification tasks, have the K neighbors
The K-Nearest Neighbors (KNN) algorithm is a "vote" for their class. The class with the
straightforward and user-friendly supervised learning majority votes is assigned to the test instance.
technique used in machine learning. It is applicable to
5. Return the Class Label
both classification and regression tasks. In this
laboratory session, we will explore the definition of
• Assign the class label of the majority of the K
KNN, implement and test the algorithm.
neighbors to the test instance.
KNN Algorithm Steps Activities
1. Choose the Number of Neighbors (K) Activity 01
• Select the number of neighbors to consider. A Write KNN algorithm using Python without any
common choice is an odd number to avoid ties library.
in binary classification. Activity 02
2. Calculate the Distance Write Python program to read csv file, split data into
training/test sets, and test the KNN algorithm.
• For each instance in the test set, calculate the
distance between the test instance and all Activity 03
instances in the training set. Common distance Write Python function to calculate the accuracy and
metric is Euclidean Distance: precision.
𝑛 Activity 04
𝑑(𝑥, 𝑦) = √∑(𝑥𝑖 − 𝑦𝑖 )2 Using scikit-learn library, write a Python KNN
𝑖=1 program and test the results with yours.
3. Find the Nearest Neighbors
• Sort the calculated distances and identify the K
nearest neighbors to the test instance.
DR. S. A. MEDJAHED 1