K-Nearest neighbour algorithm
Machine learning
Slides Fill with
• What is meant by algorithm
• Algorithm in Machine learning
• Category of NN algorithm
• Nearest neighbour
• How its work?
• Pseudo code
• Code-Explanation
• Example
• Positive and negative
What is meant by algorithm
COMPUTER
INPUT OUTPUT
PROBLEM
ALGORITHM
ALGORITHM
• It is a step-by-step procedure to solve a
problem
• Algorithm provides an overview for
solving and implementing problem
• These algorithm decides the solution of
the problem by implementing in
procedural way
ALGORITHM IN MACHINE LEARNING
ALGORITHM IN MACHINE LEARNING
• IN the machine learning algorithm is
categorized based on types of machine
learning
• Types of machine learning are
Supervised machine learning
Unsupervised machine learning
Reinforcement machine learning.
A computer is learn from past experience to do some task
and measure performance then predict future.
Many machine learning tasks can be solved &
implemented by using some Algorithms.
Category of K-NN algorithm
Nearest neighbour is the kind of
supervised learning
These follow the instance based learning
approach
INSTANCE BASED LEARNING
 The instance based stored presented training data in the
memory ,when new instance is arrived a set of related instance
is retrieved from memory and classified for new instance.
 Instance based is used for more complex and symbolic data.
Nearest neighbour
 Nearest neighbour is conceptually
straightforward approaches to
approximating real-valued or discrete-
valued target functions.
 Instances are represented in this
fashion and the process for identifying
"neighbour ing" instances is elaborated
accordingly
How its work
1. Determine parameter value K
2. Calculate the distance between the
query instance and training data
3. Sort the distance
4. Determine the nearest neighbour based
kth distance.
5. Gather the category of the nearest
instances according to k value
6. Predict the result by using simple
majority instances
Nearest neighbour
The nearest
neighbours of an
instance are
defined in terms of
the standard
Euclidean distance
Pseudo code
Training and
classification
algorithm
Code explanation
• Training algorithm:
f-Data
f(x)-instance or pattern Example data
• Classification algorithm:
xq -query instance
xl . . .xk -k instances which nearest to xq.
The value should return if the given value is correct.
Code explanation
Here we have to compare
each instance with new
instance which is nearest
instance
V-distance between new
instance and training data
argmax-Average max
distance between new and
comparing training data
i=1-initial value
K-k instances
 v belongs to V-each data is
belongs to V
Code explanation
Here ,
If and b are comparing
means it a and b is equals
then pattern matches with
training and new instances
and returns 1.
Otherwise 0 and not
matching
Example
ACID DURABILITY STRENGHT CLASSIFICATION
7 7 Bad
7 4 Good
3 4 Good
1 4 Good
3 7 ?
To find: Guess the classification of the new
instance with x=3
Example
• Given:
Step 1: Find the K=3
Step 2:Calculate the each instance
distance with new instance
(7-3)2+(7-7)2 = 4
(7-3)2+(7-4)2= 5
Example
Example
(3-3)2+(7-4)2= 3
(1-3)2+(7-7)2=
13
(3-3)2+(7-7)2= 0
Example
• Step 3:Sort the calculated distance in
ascending order
The order is
1. 5
2. 4
3. 3
4. 0
5. 13
Example
• Step4:Pick neighbour ones with k=3 so,
take first three instance 5,4 and 3
• Step 5 :find the category of the instances
5-Good
4-Bad
3-Good
• Step6:Choose majority value as a result
• The result of new instance is Good
Positive of nearest neighbour
 Simple to implement
 Easy to understand
 simple Algorithm
 K-NN classification is best for local
information
 The result can be able to classify easily
Negative of nearest neighbour
 These cause over-fitting
 Robust is noisy
 Classify the new instance cost is high
 Training data should be provided for
classification
Thankyou

Nearest neighbour algorithm

  • 1.
  • 2.
    Slides Fill with •What is meant by algorithm • Algorithm in Machine learning • Category of NN algorithm • Nearest neighbour • How its work? • Pseudo code • Code-Explanation • Example • Positive and negative
  • 3.
    What is meantby algorithm COMPUTER INPUT OUTPUT PROBLEM ALGORITHM
  • 4.
    ALGORITHM • It isa step-by-step procedure to solve a problem • Algorithm provides an overview for solving and implementing problem • These algorithm decides the solution of the problem by implementing in procedural way
  • 5.
  • 6.
    ALGORITHM IN MACHINELEARNING • IN the machine learning algorithm is categorized based on types of machine learning • Types of machine learning are Supervised machine learning Unsupervised machine learning Reinforcement machine learning.
  • 7.
    A computer islearn from past experience to do some task and measure performance then predict future. Many machine learning tasks can be solved & implemented by using some Algorithms.
  • 8.
    Category of K-NNalgorithm Nearest neighbour is the kind of supervised learning These follow the instance based learning approach INSTANCE BASED LEARNING  The instance based stored presented training data in the memory ,when new instance is arrived a set of related instance is retrieved from memory and classified for new instance.  Instance based is used for more complex and symbolic data.
  • 9.
    Nearest neighbour  Nearestneighbour is conceptually straightforward approaches to approximating real-valued or discrete- valued target functions.  Instances are represented in this fashion and the process for identifying "neighbour ing" instances is elaborated accordingly
  • 10.
    How its work 1.Determine parameter value K 2. Calculate the distance between the query instance and training data 3. Sort the distance 4. Determine the nearest neighbour based kth distance. 5. Gather the category of the nearest instances according to k value 6. Predict the result by using simple majority instances
  • 11.
    Nearest neighbour The nearest neighboursof an instance are defined in terms of the standard Euclidean distance
  • 12.
  • 13.
    Code explanation • Trainingalgorithm: f-Data f(x)-instance or pattern Example data • Classification algorithm: xq -query instance xl . . .xk -k instances which nearest to xq. The value should return if the given value is correct.
  • 14.
    Code explanation Here wehave to compare each instance with new instance which is nearest instance V-distance between new instance and training data argmax-Average max distance between new and comparing training data i=1-initial value K-k instances  v belongs to V-each data is belongs to V
  • 15.
    Code explanation Here , Ifand b are comparing means it a and b is equals then pattern matches with training and new instances and returns 1. Otherwise 0 and not matching
  • 16.
    Example ACID DURABILITY STRENGHTCLASSIFICATION 7 7 Bad 7 4 Good 3 4 Good 1 4 Good 3 7 ? To find: Guess the classification of the new instance with x=3
  • 17.
    Example • Given: Step 1:Find the K=3 Step 2:Calculate the each instance distance with new instance (7-3)2+(7-7)2 = 4 (7-3)2+(7-4)2= 5
  • 18.
  • 19.
    Example • Step 3:Sortthe calculated distance in ascending order The order is 1. 5 2. 4 3. 3 4. 0 5. 13
  • 20.
    Example • Step4:Pick neighbourones with k=3 so, take first three instance 5,4 and 3 • Step 5 :find the category of the instances 5-Good 4-Bad 3-Good • Step6:Choose majority value as a result • The result of new instance is Good
  • 21.
    Positive of nearestneighbour  Simple to implement  Easy to understand  simple Algorithm  K-NN classification is best for local information  The result can be able to classify easily
  • 22.
    Negative of nearestneighbour  These cause over-fitting  Robust is noisy  Classify the new instance cost is high  Training data should be provided for classification
  • 23.