k nearest neighbor regression

LengthWeightCost
101545
11637
121448
7933
91438
81240
61135
151050
14846
71235
10636
13844
9732
5830
51030

Dataset for K-NN Regression

In the above dataset, we have 15 data points. The dataset contains the length and weight of metal rods along with their cost. Now, suppose that we want to calculate the cost for a rod with a length of 7 and a weight of 8. For this, we will use the following steps.

First, we will decide on the value of K. We will take 3 as the number of closest neighbors used to decide the cost of the input data point.

Next, we will calculate the distance of the new data point i.e. (7, 8) to all the existing points in the dataset. Here, we will use the euclidean distance measure. I have tabulated the distances in the below table.

Point

Distance from (7,8)

(10, 15)

7.61

(11, 6)

4.47

(12, 14)

7.81

(7, 9)

1.0

(9, 14)

6.32

(8, 12)

4.12

(6, 11)

3.16

(15, 10)

8.24

(14, 8)

7.0

(7, 12)

4.0

(10, 6)

3.60

(13, 8)

6.0

(9, 7)

2.23

(5, 8)

2.0

(5, 10)

2.82

Now, we have found the distance of all the data points in the dataset from the point (7, 8). Next, we have to find the three closest points in the dataset. For this, we will sort the points according to their distances from (7, 8). The result is tabulated below.

Point

Distance from (7,8)

(7, 9)

1

(5, 8)

2

(9, 7)

2.23

(5, 10)

2.82

(6, 11)

3.16

(10, 6)

3.6

(7, 12)

4

(8, 12)

4.12

(11, 6)

4.47

(13, 8)

6

(9, 14)

6.32

(14, 8)

7

(10, 15)

7.61

(12, 14)

7.81

(15, 10)

8.24

In the above table, you can observe that the three points closest to (7, 8) are (7, 9), (5, 8), and (9, 7). These points have costs of 33, 30, and 32.

To calculate the cost of a rod with length 7 and weight 8, we can take the average of the above costs. Hence, the cost for (7, 8) will be 31.67.

Thus, we have found the cost of the rod using the given dataset and the K-NN regression algorithm in this numerical example.

0
Subscribe to my newsletter

Read articles from Kaustubh Kulkarni directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Kaustubh Kulkarni
Kaustubh Kulkarni