Skip to content
DeepForgeby svx
All problems
HardNLPnlp-379

Pairwise Cosine Similarity Matrix

Problem

For a list of vectors, build the matrix of pairwise cosine similarities using 0.0 whenever either vector has zero norm.

Given the vectors, return the n x n matrix.

Hint

The diagonal is one for nonzero vectors.

Test cases

case 1
in: [[[1,0],[0,1]]]
expected: [[1,0],[0,1]]
case 2
in: [[[1,1]]]
expected: [[0.9999999999999998]]
case 3
in: [[[0,0],[1,0]]]
expected: [[0,0],[0,1]]
Starter codePython
def cosine_similarity_matrix(vectors):
    # Your code here
    pass

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