[SciPy-User] It is quite confusing to use scipy.spatial.distance

820 views
Skip to first unread message

江大伟

unread,
May 29, 2011, 12:25:39 PM5/29/11
to scipy...@scipy.org

I want to computes euclidean distance between a vector and 2 vector. For example:

A=numpy.array([0,0])

B= numpy.array([[1,0],[0,1]])

I want to computes euclidean distance between vector A and each vector in matrix B.

My expected result is the vector [1,1]

 

So I use scipy.spatial.distance.cdist(A, B ,'euclidean')

But the error said A must be a 2-dimensional array.

So I turned to use scipy.spatial.distance.euclidean(A,B), it worked, but the result was a value 1.4142.

It was quite confusing!!

 

So I suggest adopting an uniform function to Computes the distance between any-dimensional array. Scipy.spatial.distance.cdist() is a very good function, but it can be extended to Computes the distance between a vector and a vector as well as between a vector and n vectors. That would be perfect !!.

Pauli Virtanen

unread,
May 29, 2011, 12:58:20 PM5/29/11
to scipy...@scipy.org
On Mon, 30 May 2011 00:25:39 +0800, 江大伟 wrote:
> I want to computes euclidean distance between a vector and 2 vector. For
> example:
>
> A=numpy.array([0,0])
>
> B= numpy.array([[1,0],[0,1]])
>
> I want to computes euclidean distance between vector A and each vector
> in matrix B.
>
> My expected result is the vector [1,1]

In [9]: scipy.spatial.distance.cdist(A[numpy.newaxis,:], B, 'euclidean')

Out[9]: array([[ 1., 1.]])

It works similarly as all other functions that support broadcasting.

_______________________________________________
SciPy-User mailing list
SciPy...@scipy.org
http://mail.scipy.org/mailman/listinfo/scipy-user

江大伟

unread,
May 30, 2011, 8:26:13 AM5/30/11
to SciPy Users List
Thanks. This is an googd solusion.
But it is not the best solusion.
Using an uniform function to Computes the distance between

any-dimensional array. Scipy.spatial.distance.cdist() is a very good
function, and it can be extended to Computes the distance between a vector

and a vector as well as between a vector and n vectors. That would be
perfect !!.


2011/5/30 Pauli Virtanen <p...@iki.fi>

Pauli Virtanen

unread,
May 30, 2011, 10:19:42 AM5/30/11
to scipy...@scipy.org
Mon, 30 May 2011 20:26:13 +0800, 江大伟 wrote:
> Thanks. This is an googd solusion.
> But it is not the best solusion.
>
> Using an uniform function to Computes the distance between
> any-dimensional array. Scipy.spatial.distance.cdist() is a very good
> function, and it can be extended to Computes the distance between a
> vector and a vector as well as between a vector and n vectors. That
> would be perfect !!.

I do not understand what you exactly mean. The example I gave does
exactly what you describe.

--
Pauli Virtanen

江大伟

unread,
May 30, 2011, 1:12:16 PM5/30/11
to SciPy Users List
I am sorry that I did not say clearly.I mean:

Extending Scipy.spatial.distance.cdist() to be an uniform function to Computes the distance between any-dimensional array.

 

For example:

A=numpy.array([0,0])

B=numpy.array([1,0])

scipy.spatial.distance.cdist(A, B ,'euclidean')

can return a value 1

 

A=numpy.array([0,0])

B=numpy.array([[1,0],[0,1]])

scipy.spatial.distance.cdist(A, B ,'euclidean')

can return a vector [1,1]

 

A=numpy.array([[0,0],[0,1]])

B=numpy.array([[1,0],[0,1]])

scipy.spatial.distance.cdist(A, B ,'euclidean')

can return a matrix[[1,1],[1.414,0]]




2011/5/30 Pauli Virtanen <p...@iki.fi>

江大伟

unread,
May 30, 2011, 1:32:14 PM5/30/11
to SciPy Users List

 

For a veteran, it may be very easy.

But if the user is a beginner and he does not know much detail of SciPy, he just wants to computes the distance between two arrays quickly. An uniform function and an uniform expression would be a good choice.

The SciPy is powerful now. So how to let the user use its powerful functions more easily and quickly becomes our concern.





2011/5/30 Pauli Virtanen <p...@iki.fi>

江大伟

unread,
May 29, 2011, 12:19:30 PM5/29/11
to scipy...@scipy.org

I want to computes euclidean distance between a vector and 2 vector. For example:

A=numpy.array([0,0])

B= numpy.array([[1,0],[0,1]])

I want to computes euclidean distance between vector A and each vector in matrix B.

My expected result is the vector [1,1]

 

So I use scipy.spatial.distance.cdist(A, B ,'euclidean')

But the error said A must be a 2-dimensional array.

So I turned to use scipy.spatial.distance.euclidean(A,B), it worked, but the result was a value 1.4142.

It was quite confusing!!

 

So I suggest adopting an uniform function to Computes the distance between any-dimensional array. Scipy.spatial.distance.cdist() is a very good function, but it can be extended to Computes the distance between a vector and a vector as well as between a vector and n vectors. That would be perfect !!.

Reply all
Reply to author
Forward
0 new messages