Params: -- matrix1: N1 x D -- matrix2: N2 x D Returns: -- dist: N1 x N2 dist[i, j] == distance(matrix1[i], matrix2[j])
(matrix1, matrix2)
| 10 | |
| 11 | |
| 12 | def euclidean_distance_matrix(matrix1, matrix2): |
| 13 | """ |
| 14 | Params: |
| 15 | -- matrix1: N1 x D |
| 16 | -- matrix2: N2 x D |
| 17 | Returns: |
| 18 | -- dist: N1 x N2 |
| 19 | dist[i, j] == distance(matrix1[i], matrix2[j]) |
| 20 | """ |
| 21 | assert matrix1.shape[1] == matrix2.shape[1] |
| 22 | d1 = -2 * np.dot(matrix1, matrix2.T) |
| 23 | d2 = np.sum(np.square(matrix1), axis=1, keepdims=True) |
| 24 | d3 = np.sum(np.square(matrix2), axis=1) |
| 25 | dists = np.sqrt(d1 + d2 + d3) |
| 26 | return dists |
| 27 | |
| 28 | |
| 29 | def calculate_top_k(mat, top_k): |
no outgoing calls
no test coverage detected