Visualising KNN Regression 1D (k = 1)

4.5. Visualising KNN Regression 1D (k = 1)#

We’ll continue using our giraffe height dataset as shown below.

Age (years)

Height (cm)

1.0

2.7

1.7

3.1

3.0

3.2

2.4

3.4

4.7

4.1

5.2

3.7

4.5

3.6

../../_images/knn.png

As discussed previously, the value of k we choose will result in a different model. Let’s first consider the case where k = 1. We saw previously that for age = 2.8, our model predicts the giraffe’s height to be 3.2, and that’s because the most similar giraffe in our dataset had a height of 3.2.

../../_images/knn_flat_1-1.png

As long as the nearest neighbour is that same giraffe (aged 3), our model will alway predict a height of 3.2. This is indicated by the shaded region in the figure below. We can call this the ‘neighbourhood’.

../../_images/knn_flat_4.png

Once we move out of the neighbourhood (shaded region), our closest neighbour changes.

../../_images/knn_flat_5.png

We can visualise the ‘neighbour hood’ of each training sample as shown below.

../../_images/knn_flat_6.png

We can draw these neighbourhoods on our graph.

../../_images/knn_neighbourhoods.png

Let’s draw these predictions on in a red line.

../../_images/knn_neighbourhoods_2.png

To visualise this more clearly we can remove the shaded neighbourhoods. We will also add vertical lines to connect the ‘steps’. This is what our final model looks like!

../../_images/knn_neighbourhoods_3.png