Distance-Weighted k-NN Regression

medium
Unit 17 · Data Splitting0/3 to milestone

Implement knn_regress(X_train, y_train, X_query, k, weighted=True):

  • find the k nearest training points by Euclidean distance (ties broken by lower index)
  • weighted=False: predict their mean
  • weighted=True: weight each neighbour by 1 / distance. If a query exactly matches one or more training points (distance 0), predict the mean of those points' targets.
#knn#regression2 visible + 1 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Distance-Weighted k-NN Regression · TechGradify