Rewrite the function to use a single for Loop. The one-loop implementation and the two-loop implementation shall return the same 2darray. Sample: if X = np.array([[2, 3, 5], [4, 5, 6]]); Y = np.array([[4, 5, 6], [2, 3, 5], [3, 4, 5]]), then euclid_dist(X, Y) returns array([[3., 0, 1.414], [0, 3., 1.732]]). in python program it will return array([[3., 0, 1.414], [0, 3., 1.732]]).
import numpy as np
def sample(x,y):
return np.sqrt(sum([(a - b) ** 2 for a, b in zip(x, y)]))
X = np.array([[2, 3, 5], [4, 5, 6]])
Y = np.array([[4, 5, 6], [2, 3, 5], [3, 4, 5]])
out1= map(sample,[X[0],X[0],X[0]],Y)
out2= map(sample,[X[1],X[1],X[1]],Y)
out=[list(out1),list(out2)]
print(out)
Get Answers For Free
Most questions answered within 1 hours.