### K-Nearest Neighbors (KNN) - The lazy learner

## What is KNN Algorithm?

K nearest neighbors or KNN Algorithm is a simple algorithm which uses the entire dataset in its training phase. Whenever a prediction is required for an unseen data instance, it searches through the entire training dataset for k-most similar instances and the data with the most similar instance is finally returned as the prediction.

kNN is often used in search applications where you are looking for similar items, like find items similar to this one.

Algorithm suggests that if you’re similar to your neighbours, then you are one of them. For example, if apple looks more similar to peach, pear, and cherry (fruits) than monkey, cat or a rat (animals), then most likely apple is a fruit.

## How does a KNN Algorithm work?

The k-nearest neighbors algorithm uses a very simple approach to perform classification. When tested with a new example, it looks through the training data and finds the k training examples that are closest to the new example. It then assigns the most common class label (among those k-training examples) to the test example.

## What does ‘k’ in kNN Algorithm represent?

k in kNN algorithm represents the number of nearest neighbor points which are voting for the new test data’s class.

If k=1, then test examples are given the same label as the closest example in the training set.

If k=3, the labels of the three closest classes are checked and the most common (i.e., occurring at least twice) label is assigned, and so on for larger ks.

## Why is Nearest Neighbor a Lazy Algorithm?

Although, Nearest neighbor algorithms, for instance, the K-Nearest Neighbors (K-NN) for classification, are very “simple” algorithms, that’s not why they are called lazy ;). K-NN is a lazy learner because it doesn’t learn a discriminative function from the training data but “memorizes” the training dataset instead.

For example, the logistic regression algorithm learns its model weights (parameters) during training time. In contrast, there is no training time in K-NN. Although this may sound very convenient, this property doesn’t come without a cost: The “prediction” step in K-NN is relatively expensive! Each time we want to make a prediction, K-NN is searching for the nearest neighbor(s) in the entire training set! (Note that there are certain tricks such as BallTrees and KDtrees to speed this up a bit.)

To summarize: An eager learner has a model fitting or training step. A lazy learner does not have a training phase.

## CODE

`import`

`csv`

`with `

`open`

`(r`

`'.\iris.data.csv'`

`) as csvfile:`

` `

`lines `

`=`

`csv.reader(csvfile)`

` `

`for`

`row `

`in`

`lines:`

` `

`print`

`(`

`', '`

`.join(row))`

`import`

`csv`

`import`

`random`

`def`

`handleDataset(filename, split, trainingSet`

`=`

`[] , testSet`

`=`

`[]):`

` `

`with `

`open`

`(filename, `

`'r'`

`) as csvfile:`

` `

`lines `

`=`

`csv.reader(csvfile)`

` `

`dataset `

`=`

`list`

`(lines)`

` `

`for`

`x `

`in`

`range`

`(`

`len`

`(dataset)`

`-`

`1`

`):`

` `

`for`

`y `

`in`

`range`

`(`

`4`

`):`

` `

`dataset[x][y] `

`=`

`float`

`(dataset[x][y])`

` `

`if`

`random.random() < split:`

` `

`trainingSet.append(dataset[x])`

` `

`else`

`:`

` `

`testSet.append(dataset[x])`

`trainingSet`

`=`

`[]`

`testSet`

`=`

`[]`

`handleDataset(r`

`'iris.data.csv'`

`, `

`0.66`

`, trainingSet, testSet)`

`print`

`(`

`'Train: '`

`+`

`repr`

`(`

`len`

`(trainingSet)))`

`print`

`(`

`'Test: '`

`+`

`repr`

`(`

`len`

`(testSet)))`

`import`

`math`

`def`

`euclideanDistance(instance1, instance2, length):`

` `

`distance `

`=`

`0`

` `

`for`

`x `

`in`

`range`

`(length):`

` `

`distance `

`+`

`=`

`pow`

`((instance1[x] `

`-`

`instance2[x]), `

`2`

`)`

` `

`return`

`math.sqrt(distance)`

`data1 `

`=`

`[`

`2`

`, `

`2`

`, `

`2`

`, `

`'a'`

`]`

`data2 `

`=`

`[`

`4`

`, `

`4`

`, `

`4`

`, `

`'b'`

`]`

`distance `

`=`

`euclideanDistance(data1, data2, `

`3`

`)`

`print`

`(`

`'Distance: '`

`+`

`repr`

`(distance))`

`import`

`operator `

`def`

`getKNeighbors(trainingSet, testInstance, k):`

` `

`distances `

`=`

`[]`

` `

`length `

`=`

`len`

`(testInstance)`

`-`

`1`

` `

`for`

`x `

`in`

`range`

`(`

`len`

`(trainingSet)):`

` `

`dist `

`=`

`euclideanDistance(testInstance, trainingSet[x], length)`

` `

`distances.append((trainingSet[x], dist))`

` `

`distances.sort(key`

`=`

`operator.itemgetter(`

`1`

`))`

` `

`neighbors `

`=`

`[]`

` `

`for`

`x `

`in`

`range`

`(k):`

` `

`neighbors.append(distances[x][`

`0`

`])`

` `

`return`

`neighbors`

`trainSet `

`=`

`[[`

`2`

`, `

`2`

`, `

`2`

`, `

`'a'`

`], [`

`4`

`, `

`4`

`, `

`4`

`, `

`'b'`

`]]`

`testInstance `

`=`

`[`

`5`

`, `

`5`

`, `

`5`

`]`

`k `

`=`

`1`

`neighbors `

`=`

`getNeighbors(trainSet, testInstance, `

`1`

`)`

`print`

`(neighbors)`

`import`

`operator`

`def`

`getResponse(neighbors):`

` `

`classVotes `

`=`

`{}`

` `

`for`

`x `

`in`

`range`

`(`

`len`

`(neighbors)):`

` `

`response `

`=`

`neighbors[x][`

`-`

`1`

`]`

` `

`if`

`response `

`in`

`classVotes:`

` `

`classVotes[response] `

`+`

`=`

`1`

` `

`else`

`:`

` `

`classVotes[response] `

`=`

`1`

` `

`sortedVotes `

`=`

`sorted`

`(classVotes.items(), key`

`=`

`operator.itemgetter(`

`1`

`), reverse`

`=`

`True`

`)`

` `

`return`

`sortedVotes[`

`0`

`][`

`0`

`]`

`neighbors `

`=`

`[[`

`1`

`,`

`1`

`,`

`1`

`,`

`'a'`

`], [`

`2`

`,`

`2`

`,`

`2`

`,`

`'a'`

`], [`

`3`

`,`

`3`

`,`

`3`

`,`

`'b'`

`]]`

`print`

`(getResponse(neighbors))`

`def`

`getAccuracy(testSet, predictions):`

` `

`correct `

`=`

`0`

` `

`for`

`x `

`in`

`range`

`(`

`len`

`(testSet)):`

` `

`if`

`testSet[x][`

`-`

`1`

`] `

`is`

`predictions[x]:`

` `

`correct `

`+`

`=`

`1`

` `

`return`

`(correct`

`/`

`float`

`(`

`len`

`(testSet))) `

`*`

`100.0`

`testSet `

`=`

`[[`

`1`

`,`

`1`

`,`

`1`

`,`

`'a'`

`], [`

`2`

`,`

`2`

`,`

`2`

`,`

`'a'`

`], [`

`3`

`,`

`3`

`,`

`3`

`,`

`'b'`

`]]`

`predictions `

`=`

`[`

`'a'`

`, `

`'a'`

`, `

`'a'`

`]`

`accuracy `

`=`

`getAccuracy(testSet, predictions)`

`print`

`(accuracy)`

You can also find me on linkedin. I’d love to hear from you if I can help you or your team with machine learning.

This information is impressive; I am inspired how continuously you describe this topic. After reading your post, thanks for taking the time to discuss this, I feel happy about it and I love learning more about this topic snowflake data warehouse

ReplyDelete