KNN Solved Example to predict Sugar of Diabetic Patient given BMI and Age
Apply K nearest neighbor classifier to predict the diabetic patient with the given features BMI, Age. If the training examples are,
BMI | Age | Sugar |
33.6 | 50 | 1 |
26.6 | 30 | O |
23.4 | 40 | O |
43.1 | 67 | O |
35.3 | 23 | 1 |
35.9 | 67 | 1 |
36.7 | 45 | 1 |
25.7 | 46 | O |
23.3 | 29 | O |
31 | 56 | 1 |
Assume K=3,
Test Example BMI=43.6, Age=40, Sugar=?
Solution:
Video Tutorial
The given training dataset has 10 instances with two features BMI (Body Mass Index) and Age. Sugar is the target label. The target label has two possibilities 0 and 1. 0 means the diabetic patient has no sugar and 1 means the diabetic patient has sugar.
Given the dataset and new test instance, we need to find the distance from the new test instance to every training example. Here we use the euclidean distance formula to find the distance.
In the next table, you can see the calculated distance from text example to training instances.
BMI | Age | Sugar | Formula | Distance |
33.6 | 50 | 1 | √((43.6-33.6)^2+(40-50)^2 ) | 14.14 |
26.6 | 30 | O | √((43.6-26.6)^2+(40-30)^2 ) | 19.72 |
23.4 | 40 | O | √((43.6-23.4)^2+(40-40)^2 ) | 20.20 |
43.1 | 67 | O | √((43.6-43.1)^2+(40-67)^2 ) | 27.00 |
35.3 | 23 | 1 | √((43.6-35.3)^2+(40-23)^2 ) | 18.92 |
35.9 | 67 | 1 | √((43.6-35.9)^2+(40-67)^2 ) | 28.08 |
36.7 | 45 | 1 | √((43.6-36.7)^2+(40-45)^2 ) | 8.52 |
25.7 | 46 | O | √((43.6-25.7)^2+(40-46)^2 ) | 18.88 |
23.3 | 29 | O | √((43.6-23.3)^2+(40-29)^2 ) | 23.09 |
31 | 56 | 1 | √((43.6-31)^2+(40-56)^2 ) | 20.37 |
Once you calculate the distance, the next step is to find the nearest neighbors based on the value of k. In this case, the value of k is 3. Hence we need to find 3 nearest neighbors.
BMI | Age | Sugar | Distance | Rank |
33.6 | 50 | 1 | 14.14 | 2 |
26.6 | 30 | O | 19.72 | |
23.4 | 40 | O | 20.20 | |
43.1 | 67 | O | 27.00 | |
35.3 | 23 | 1 | 18.92 | |
35.9 | 67 | 1 | 28.08 | |
36.7 | 45 | 1 | 8.52 | 1 |
25.7 | 46 | O | 18.88 | 3 |
23.3 | 29 | O | 23.09 | |
31 | 56 | 1 | 20.37 |
Now, we need to apply the majority voting technique to decide the resulting label fro the new example. Here the 1st and 2nd nearest neighbors have target label 1 and the 3rd nearest neighbor has target label 0. Target label 1 has the majority. Hence the new example is classified as 1, That is the diabetic patient has Sugar.
Test Example BMI=43.6, Age=40, Sugar=1
Summary
Here you can find the solution to the KNN Example to predict the Sugar of Diabetic Patients given BMI and Age. If you like the material share it with your friends. Like the Facebook page for regular updates and YouTube channel for video tutorials.