Answer to Question #156229 in Python for xry

Question #156229

Assume a and b are two (20, 20) numpy arrays. The L2-distance (defined above) between two equal dimension arrays can be calculated in python as follows:


def l2_dist(a, b):

    result = ((a - b) * (a - b)).sum()

    result = result ** 0.5

    return result


Which of the following expressions using this function will give an error?

l2_dist(np.reshape(a, (20 * 20)), np.reshape(b, (20 * 20, 1)))




1
Expert's answer
2021-01-17T08:07:49-0500
l2_dist(np.reshape(a, (20 * 20)), np.reshape(b, (20 * 20, 1)))
# expression is correct, none of the operators will cause an error

# errors are possible if the module is not imported:
import numpy as np 
# or arrays a,b are not defined in the program, but these cases are trivial and can be ignored

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS