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 !!.
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
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
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]]
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.
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 !!.