1

I want to vectorize the following function. The arguments are numpy arrays.

def euclidean_distance(dl, dr):
    return math.sqrt(((dl - dr) ** 2).sum())

I do the following

v_u = numpy.vectorize(euclidean_distance)

and I am doing the following call

v_u(numpy.array([[10, 20, 30], [4, 5, 6]]), numpy.array([1, 2, 3]))

What I want is that I get back an array which contains the euclidean distance of [1, 2, 3] with [10, 20, 30], [4, 5, 6].

I think I am missing something obvious.

EDIT:
The following is the error I get

AttributeError: 'int' object has no attribute 'sum'

which is obvious that dl and dr are passed as single elements but not as arrays... So I was wondering if somebody could correct it so that it operated on arrays.

Thanks a lot

3
  • What is your problem? What is the error or unexpected output you get? Commented Jun 1, 2012 at 10:21
  • [1,2,3], [10,20,30], [4,5,6], they are 3D coordinates? So you want the distance between [1,2,3] and every point in the other array? Commented Jun 1, 2012 at 10:21
  • @marshal.ward, yup you are right! Commented Jun 1, 2012 at 10:25

1 Answer 1

5

Why do you need vectorize for that?

You can use shape broadcasting and do something like:

dist = numpy.sqrt(numpy.sum((d1-dr)**2, axis = 1))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.