|
Algorithm 1. Pseudo-code for the k-NN Algorithm. |
|
Input: |
|
|
: training data |
|
|
: class labels of
|
|
|
: unknown sample |
|
|
: instance set |
|
|
: number of nearest neighbor |
|
Output: predicted class label |
| 1: |
//train phase |
| 2: |
for = 1 to do
|
| 3: |
|
Store training instance , class label into instance set
|
| 4: |
end for |
| 5: |
|
| 6: |
//inference phase |
| 7: |
for = 1 to do
|
| 8: |
|
Calculate distance between the unknown sample and the stored training instance
|
| 9: |
|
Store calculated distance values into
|
| 10: |
end for |
| 11: |
|
| 12: |
Find minimum distance values |
| 13: |
Vote which class is the most among minimum distance values |
| 14: |
|
| 15: |
Return the class that has been voted by the majority |