K-Nearest Neighbours
Submissions: 585   Accuracy:

16.87%

  Difficulty: Easy   Marks: 2

K-Nearest Neighbours is one of the most basic yet essential classification algorithms in Machine Learning. It belongs to the supervised learning domain and finds intense application in pattern recognition, data mining and intrusion detection.

For Example:
Consider the following table of data points containing two 
features/groups "Red and Green":

Given another set of data points (testing data), allocate 
these points a feature/group by analyzing the training set.
White Point represents an unclassified point


Intuition
If we plot these points on a graph, we may be able to locate 
some clusters, or groups. Now, given an unclassified point, 
we can assign it to a group by observing what group its 
nearest neighbours belong to. This means, a point close to a 
cluster of points classified as ‘Red’ has a higher probability
of getting classified as ‘Red’.Intuitively, we can see that the
first point (2.5, 7) should be classified as ‘Green’ and the 
second point (5.5, 4.5) should be classified as ‘Red’.

Your are required to complete the function classifyAPoint(), which should return that to which feature/group will be the test data belong.

Input:
The function takes four arguments as input, an array of structure (arr[]) whose each element represents the coordinates and the type of the feature/group to which the point belong, an integer N denoting the total number of points, an integer K denoting the number of neighbours to consider and an test data (test) whom which needed to be classified.
There will be T test cases and for each test case the function will be called separately.  

Output:
For each test case function should return the group to which the test data would belong.

Constraints:
1<=T<=100
1<=N<=100
-100.00<=X,Y<=100

Example:
Input:

5
1 2 1 1 3 1 2 3 0 0 1 0 0 0 0
1 1 3
5
1 2 0 1 3 1 2 3 0 0 1 1 0 0 1 
1 1 4
Output:
0
1

Note:The Input/Ouput format and Example given are used for system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from stdin/console. The task is to complete the function specified, and not to write the full code.

** For More Input/Output Examples Use 'Expected Output' option **

Author: harshitsidhwa


If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.



Need help with your code? Please use ide.geeksforgeeks.org, generate link and share the link here.


to report an issue on this page.