Skip to content
DeepForgeby svx
All problems
EasyDeep Learningdl-150

Nearest Neighbor Explanation

Problem

Return the label of the training example with the highest cosine similarity to the query. If the query or an example is all zeros, its similarity is 0.0.

The signature is nearest_neighbor_explanation(query, examples, labels).

Hint

Example-based explanations justify a prediction with the most similar training point.

Test cases

case 1
in: [[1,0],[[1,0],[0,1]],[10,20]]
expected: 10
case 2
in: [[0,1],[[1,0],[0,1]],[10,20]]
expected: 20
case 3
in: [[0.9,0.1],[[1,0],[0,1],[0.5,0.5]],[1,2,3]]
expected: 1
case 4
in: [[0.1,0.1],[[1,0],[0,1]],[7,8]]
expected: 7
Starter codePython
import math
def nearest_neighbor_explanation(query, examples, labels):
    # Your code here
    pass

4 test cases · run your code against them in the editor