# How does knn regression .predict() work?

For a typical regression algorithm like linear regression, the model is

y=2x+1

for instance. We can make predictions

y = 3

when

x=1

Picture above is an example from github.The green point is the 'test data' as the author shows. My question is how can we know the exact position of the 'test_data', since we do not know the y_label of 'test data'. If y_lable is not certain, there might be different neighbors of this 'test data'. Thus, how knn regression predict works?

Not very sure if I had upload the picture successfully, the picture is in the

1.3.4 k-neighbors regression variant model

The mechanics of the prediction are quite straight forward. Find the $$k$$-nearest data points in the training set (where the distance is measured with respect to the feature, not the y_label) to the prediction point, and then take an arithmetic mean of the y_labels for the $$k$$-nearest data points. Return the arithmetic mean as the prediction.
• Distance is usually the euclidean distance. In 1D (as in your example) the distance metric is $\vert x^* - x\vert$. Here, $x^*$ is the prediction feature. This is naturally extended to higher dimensions. You can read up about euclidean distance here: en.wikipedia.org/wiki/Euclidean_distance#Definition. Does that answer your question? – Demetri Pananos Sep 18 '19 at 2:31