KNN From Scratch
Start Timer
0:00:00
Build a 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 nearest neighbors, rerun the search using neighbors instead
- You may use
pandas
andnumpy
but NOTscikit-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, new_point, data) -> 2
Recommended questions for you
Personalized based on your user activity, skill level, and preferences.
.
.
.
.
.
.
.
.
.
Comments