Interview Query

KNN From Scratch

1
Have you seen this question before?

Build a kk Nearest Neighbors classification model from scratch with the following conditions:

  • Use Euclidian distance (aka, the “2 norm”) as your closeness metric
  • Your function should be able to handle data frames of arbitrary many rows and columns
  • If there is a tie in the class of the kk nearest neighbors, rerun the search using k1k-1 neighbors instead
  • You may use pandas and numpy but NOT scikit-learn

Example:

Input:

k = 5
new_point = [0.5,-2,8]
print(data)
...
        Var1      Var2      Var3  Target
0  -3.279536  3.362223  2.847892       2
1  -0.791565  1.742475  2.151587       2
2  -0.785992 -0.938681 -0.459770       0
3  -1.068190  1.461051  0.127130       3
4  -0.367568 -0.870240 -0.225734       0
..       ...       ...       ...     ...
95 -1.327175  1.971085 -0.690689       2
96 -3.203714  1.847649  0.778901       2
97 -0.587640  0.647458  2.094385       2
98  0.363644 -0.509795  2.514191       1
99 -0.673498  2.955285  2.102122       4

[100 rows x 4 columns]

Output:

def kNN(k,data,new_point) -> 2
Next question: Google Docs Drop
.....
Python 3.9.6
Loading editor
Use Shift + Enter to run code