How can I implement KNN ? I want to have user input and data from excel sheet compared.

3 views (last 30 days)
ML on 13 May 2022
Answered: KSSV on 13 May 2022
Basically I want to have a code that compares input with excel sheet data if said data is there then I want to return that if not I'd like to find the closet value to it (nearest neighbor). It would also return the difference between the nearest neighbor and input, how could I go about it?
Ex:
Userinput-- my patient weighs 115lbs and is 33 yrs old
data -- patients in hospital x weigh (97lbs and is 15 yrs old, 140lbs and is 30yrs old, 105lbs and is 25 yrs old, 210 and is 67yrs old )
Answer: is similar to patient that weighs 105lbs and is 25 yrs old the difference between them is 8 yrs 10lbs
Any ideas are welcome, thank you! If anymore explanation is needed I would be happy to try and further explain!

KSSV on 13 May 2022
Say it loudly today that your function is knnsearch.
weight = [971; 1401; 105; 210] ;
age = [15; 30 ; 25 ; 67] ;
data = table(weight,age)
data = 4×2 table
weight age ______ ___ 971 15 1401 30 105 25 210 67
% user input
input_weight = 115 ;
input_age = 33 ;
idx = knnsearch([data.weight data.age],[input_weight input_age]) ;
iwant = [data.weight(idx) data.age(idx)]
iwant = 1×2
105 25
thediff = abs(iwant-[input_weight input_age])
thediff = 1×2
10 8
Note that creating table is optional;, you can go ahead with arrays also.