A fish farm weighs and measures every fish it catches. The biologists labelled a training set of fish by hand; now a program should label new fish by the most similar labelled one.
Write nn_predict(X_train, y_train, X_test) that returns a list with one predicted label per row of X_test: the label y_train[j] of the training row X_train[j] with the smallest Euclidean distance to that test row. If several training rows are equally close, use the one with the smallest index j.
The tests draw their fish from fish_catch(n, seed), which returns (X, y) with rows [weight in grams, length in cm]. It is available in your code, so you can look at a catch with Run.
Examples
Input: X_train = [[0, 0], [4, 0], [0, 3]], y_train = ["a", "b", "c"]
X_test = [[1, 1], [2, 0], [3, 3]]
Output: ["a", "a", "c"]
Explanation: [1, 1] is nearest to [0, 0]. [2, 0] is exactly 2 away from both [0, 0] and [4, 0];
the smaller index, 0, wins. [3, 3] is 3 away from [0, 3] and further from the others.
Input: X_train, y_train = fish_catch(6, 1)
(X_train = [[566, 34], [408, 30], [434, 26], [439, 33], [558, 32], [610, 34]],
y_train = ["perch", "trout", "trout", "trout", "trout", "perch"])
X_test = [[430, 34], [600, 40]]
Output: ["trout", "perch"]
Explanation: the nearest fish are [439, 33] (a trout) and [610, 34] (a perch).
Constraints
1 <= len(X_train) <= 500,0 <= len(X_test) <= 300- all rows have the same length (1 to 5) and hold whole numbers
Goals
- Predict a label by copying the label of the most similar training example
- Predict a whole test set with the same rule
- Break ties between equally near examples with a stated rule