Rajiv Gandhi University of Knowledge Technologies
Catering to the Educational Needs of Gifted Rural Youth of Andhra Pradesh
(Established by the Govt. of Andhra Pradesh and recognized as per Section 2(f) of UGC Act, 1956)
Rajiv Knowledge Valley Campus
Department of Computer Science and Engineering
Machine Learning
Day-6
Presented by
R Sreenivas
Assistant Professor
RGUKT RK Valley
© Copyright 2025 ® All rights are reserved
Agenda
Instance Based Learning
K-Nearest Neighbors
K-Nearest Neighbors Example
Quote of the Day
Aptitude & Coding
Instance Based Learning
➢Instance based learning is a ML technique that relies on storing and recalling
Instance Based Learning
instances of training data.
➢It involves memorizing training data in order to make predictions about future data.
➢In this approach does not require prior knowledge or assumptions about the data.
➢It also called memory based learning or lazy learning.
➢KNN, Locally weighted regression, Radial basis functions, and case based learning
are methods of instance based learning.
?
K-Nearest Neighbors
➢ KNN is a instance based supervised learning algorithm.
K-Nearest Neighbors
➢ It is used for both classification and regression.
➢ KNN algorithm stores all available data and classifies a new data point on the
closest category.
➢ For finding closest category it uses distance measures like Euclidean ,
Manhattan … etc.
➢ KNN is a non-parametric algorithm which means it does not make any
assumptions on underlying data.
➢ KNN is called lazy learning algorithm because it does not learn from the
training set immediately instead the stores dataset
K-Nearest Neighbors
?
K-Nearest Neighbors
new data
?
K-Nearest Neighbors
➢ Load the data
K-Nearest Neighbors
➢ Initialize K to chosen number of neighbours
➢ For each example in the data, Calculate the distance between the new
example and the current example from the data
➢ Sort the ordered collection of distances and indices from smallest to largest
(in ascending order) by the distances
➢ Pick the first K entries from the sorted collection
➢ Get the labels of the selected K entries
➢ If regression, return the mean of the K labels
➢ If classification, return the mode of the K labels
How to choose K value in KNN
➢ A very low value for K such as K=1 or K=2, can be noisy and lead to the effects
K-Nearest Neighbors
of outliers in the model.
➢ Large values for K are good, but it may find some difficulties.
➢ There is no particular way to determine the best value for "K", so we need to
try some values to find the best out of them. The most preferred value for K is
5.
K-Nearest Neighbors Example
Weight (in kg) Height(in cm) Class
K-Nearest Neighbors Example
51 167 Underweighted
62 182 Normal
69 176 Normal
64 173 Normal
65 172 Normal
58 169 Normal
57 173 Normal
55 170 Normal
56 174 Underweighted
Classify the person if weight = 57kg and height = 170cm
K-Nearest Neighbors Example
BRIGHTNESS SATURATION CLASS
K-Nearest Neighbors Example
40 20 Red
50 50 Blue
60 90 Blue
10 25 Red
70 70 Blue
60 10 Red
25 80 Blue
Classify the colour if Brightness = 20 and Saturation = 35
K-Nearest Neighbors Example
Length Weight Cost
K-Nearest Neighbors Example
10 15 45
11 6 37
12 14 48
7 9 33
9 14 38
8 12 40
6 11 35
15 10 50
14 8 46
7 12 35
Calculate the cost for a rod with a length of 7 and a weight of 8.
K-Nearest Neighbors Pro’s and Con’s
Pro’s
➢ It is very simple algorithm to understand and interpret.
K-Nearest Neighbors
➢ It is very useful for nonlinear data because there is no assumption about data in
this algorithm.
➢ It is a versatile algorithm as we can use it for classification as well as regression.
➢ It has relatively high accuracy but there are much better supervised learning
models than KNN.
Con’s
➢ It is computationally expensive algorithm because it stores all the training data.
➢ High memory storage required as compared to other supervised learning
algorithms.
➢ Prediction is slow
➢ It is very sensitive to the scale of data as well as irrelevant features.
Which of the following is a characteristic of instance-based learning
algorithms?
In class assignments
A) They create a model during the training phase
B) They discard training data after learning
C) They use training data to make predictions directly
D) They are always faster than other algorithms
In class assignments What type of learning is KNN considered as?
A) Unsupervised, Eager Learning
B) Supervised, Lazy Learning
C) Supervised, Eager Learning
D) Reinforcement Learning
What happens if you choose a very large value of K in KNN?
In class assignments
A) Model becomes too general and may underfit
B) Model becomes too specific and overfits
C) KNN will ignore distant points
D) There will be no change in prediction
In class assignments What is the main limitation of KNN on large datasets?
A) It needs preprocessing of data
B) Prediction becomes slow due to computation of distances
C) It performs poorly with labeled data
D) It cannot be used for regression
Which technique can be used to give more importance to closer
neighbors in KNN?
In class assignments
A) Distance-weighted voting
B) Feature scaling
C) Voting
D) Bagging
In class assignments What is the output of KNN when used for regression tasks?
A) A trained model
B) The most frequent class
C) The standard deviation of labels
D) The average of k nearest labels
Quote of the Day
No one notice your hard work
No one notice your efforts
No one notice your pain and tears
But Everyone notice your mistakes
Coding & Aptitude
48. Rotate Image(matrix)
Daily Assignments
Input : matrix = [[1,2,3], [4,5,6], [7,8,9]]
Output: [[7,4,1], [8,5,2], [9,6,3]]
The ratio between the speeds of two trains is 7 : 8. If the second train
runs 400 km in 4 hours, then the speed of the first train is:
Thank
You