Nearest Neighbor AlgorithmZaffar Ahmed Shaikh
TopicsIntroduction – Memory-based algorithmsK-nearest neighbor (KNN) algorithmHow KNN works?KNN ExampleDifferent types of KNN
IntroductionMemory-based algorithms utilize the entire user-item database to generate a prediction. They find a set of users, known as neighbors, that have a history of agreeing with the target user. Once a neighborhood of users is formed, the preferences of neighbors are combined to produce a prediction or top-K recommendation for the active user.   K-nearest neighbor (KNN)The nearest neighbor algorithm measures the distance dE(Xi,Xj)between query points Xi and a set of training samples Xjto classify a new object based on majority of K-nearest neighbor category of Y attributes of training samples. Query point Xi = x1, x2, x3, ……….., xnTraining Sample Xj= x1, x2, x3, ……….., xn
How KNN works?Determine K (no of nearest neighbors)Calculate distance (Euclidean, Manhattan)Determine K-minimum distance neighborsGather category Y values of nearest neighbors Use simple majority of nearest neighbors to predict value of query instance
KNN ExamplePredict who will win today’s Cricket match between India and Pakistan based on users rating and previous results of matches played between the two teams.
1. Determine KDetermine value of K Suppose K = 32. Calculate distanceCoordinates of query instance are (4,3,3)Coordinates of training instance(1) are (7,2,1)D = SQRT ((7-4)2+(2-3) 2+(1-3) 2) = 3.74165
2. Calculate distance
3. Determine K-minimum distance neighborsK = 3
4. Gather category Y values of nearest neighbors
5. Use simple majority of nearest neighbors to predict value of query instanceHere India has won 2 matches 2 (-) signs and Pakistan has won 1 match 1 (+) signWe conclude that India will win today’s match
Different types of KNN KNN for ClassificationKNN for PredictionKNN for Smoothing
Thank you
Nearest Neighbor Algorithm  Zaffar Ahmed

Nearest Neighbor Algorithm Zaffar Ahmed

  • 1.
  • 2.
    TopicsIntroduction – Memory-basedalgorithmsK-nearest neighbor (KNN) algorithmHow KNN works?KNN ExampleDifferent types of KNN
  • 3.
    IntroductionMemory-based algorithms utilizethe entire user-item database to generate a prediction. They find a set of users, known as neighbors, that have a history of agreeing with the target user. Once a neighborhood of users is formed, the preferences of neighbors are combined to produce a prediction or top-K recommendation for the active user. K-nearest neighbor (KNN)The nearest neighbor algorithm measures the distance dE(Xi,Xj)between query points Xi and a set of training samples Xjto classify a new object based on majority of K-nearest neighbor category of Y attributes of training samples. Query point Xi = x1, x2, x3, ……….., xnTraining Sample Xj= x1, x2, x3, ……….., xn
  • 4.
    How KNN works?DetermineK (no of nearest neighbors)Calculate distance (Euclidean, Manhattan)Determine K-minimum distance neighborsGather category Y values of nearest neighbors Use simple majority of nearest neighbors to predict value of query instance
  • 5.
    KNN ExamplePredict whowill win today’s Cricket match between India and Pakistan based on users rating and previous results of matches played between the two teams.
  • 6.
    1. Determine KDeterminevalue of K Suppose K = 32. Calculate distanceCoordinates of query instance are (4,3,3)Coordinates of training instance(1) are (7,2,1)D = SQRT ((7-4)2+(2-3) 2+(1-3) 2) = 3.74165
  • 7.
  • 8.
    3. Determine K-minimumdistance neighborsK = 3
  • 9.
    4. Gather categoryY values of nearest neighbors
  • 10.
    5. Use simplemajority of nearest neighbors to predict value of query instanceHere India has won 2 matches 2 (-) signs and Pakistan has won 1 match 1 (+) signWe conclude that India will win today’s match
  • 11.
    Different types ofKNN KNN for ClassificationKNN for PredictionKNN for Smoothing
  • 12.