Docsity

KNN(K-Nearest Neighbour) Algorithm, Slides of Artificial Intelligence

Partial preview of the text

Download KNN(K-Nearest Neighbour) Algorithm and more Slides Artificial Intelligence in PDF only on Docsity!

KNN ALGORITHM

Artificial Intelligence

Outline

◦ (^) K Nearest Neighbor

KNN Algorithm

◦ (^) Load the data ◦ (^) Determine parameter K = No. of nearest neighbors ◦ (^) Calculate the distance between the query instance and all training samples ◦ (^) Sort the distances in ascending order ◦ (^) Pick the first K entries from the sorted collection ◦ (^) Get the labels of the selected K entries ◦ (^) Use simple majority of the category of the nearest neighbors as the prediction value of the query instance.

KNN Algorithm

KNN Example

Normalization

◦ (^) One major drawback in calculating distance measures directly from the training set is in the case where variables have different measurement scales ◦ (^) There is a mixture of numerical and categorical variables. ◦ (^) For example, if one variable is based on annual income in dollars, and the other is based on age in years. ◦ (^) In such case income will have a much higher influence on the distance calculated. ◦ (^) One solution is to standardize/normalize the training set.

Class Activity

Advantages

◦ (^) The algorithm is simple and easy to implement. ◦ (^) There’s no need to build a model, tune several parameters, or make additional assumptions. ◦ (^) The algorithm is versatile. It can be used for classification and search.

KNN

◦ (^) Choosing the right K value? ◦ (^) To select the K that’s right for your data, we run the KNN algorithm several times with different values of K and choose the K that reduces the number of errors we encounter while maintaining the algorithm’s ability to accurately make predictions when it’s given data it hasn’t seen before.

Outlier

◦ (^) Low k-value is sensitive to outliers and a higher K-value is more resilient to outliers as it considers more voters to decide prediction.