89438

Find the distance of each pair between two vectors

Question:

I have two vectors, let's say x=[2,4,6,7] and y=[2,6,7,8] and I want to find the euclidean distance, or any other implemented distance (from scipy for example), between each corresponding pair. That will be dist=[0, 2, 1, 1].

When I try

dist = scipy.spatial.distance.cdist(x,y, metric='sqeuclidean')

or

dist = [scipy.spatial.distance.cdist(x,y, metric='sqeuclidean') for x,y in zip(x,y)]

I get

ValueError: XA must be a 2-dimensional array.

How am I supposed to calculate dist and why do I have to reshape data for that purpose?

Answer1:

cdist does not compute the list of distances between corresponding pairs, but the matrix of distances between all pairs.

np.linalg.norm((np.asarray(x)-np.asarray(y))[:, None], axis=1)

Is how id typically write this for the Euclidian distance between n-dimensional points; but if you are only dealing with 1 dimensional points, the absolute difference, as suggested by elpres would be simpler.

Recommend

  • Impossible to inject JMS QueueConnectionFactory in JBoss 6.1.0 with resource annotation
  • ORA-24778: cannot open connections
  • escaping \" symbol used in findstr within a FOR statement
  • Multiple rows using XSLT
  • Accessing function attribute created in a decorator outside that decorator
  • Sampling unique column indexes for each row of a numpy array
  • How to read csv file into an Array of arrays in scala
  • Tensorflow: “GraphDef cannot be larger than 2GB.” error when saving model after assigning variables
  • Doctrine DQL returning flat array from join
  • Fold function in Octave
  • A limitation of Sqlite3's full text search doesn't allow ORs with MATCHes. Workaround?
  • numpy array divide column by vector
  • Assignment of Allocatables of Different Shapes in Fortran [duplicate]
  • How to model a mixture of finite components from different parametric families with JAGS?
  • MATLAB - Classification output
  • Using Netbeans with Cygwin and SDL, including SDL.h creates strange error
  • SQL Server re-calculate or not?
  • CoreData basics – to-many relationship array data
  • Eloquent Relationship on Partial Match
  • Receive mouse move even cursor is outside control
  • XMLBeans Duplicate Class and Class Not Found Errors
  • Python 3.2.2, error(scripts to exe)
  • How to create a 2D image by rotating 1D vector of numbers around its center element?
  • Make new pandas columns based on pipe-delimited column with possible repeats
  • How to assign byte[] as a pointer in C#
  • Convert Type Decimal to Hex (string) in .NET 3.5
  • Appending Character to Character Array In C
  • error importing numpy
  • How to add git credentials to the build so it would be able to be used within a shell code?
  • PHP CURL timing out but CLI CURL works
  • Declaring variable dynamically in VB.net
  • CakePHP 2.0.4 - findBy magic methods with conditions
  • Jackson Parser: ignore deserializing for type mismatch
  • Initializer list vs. initialization method
  • C# - Is there a limit to the size of an httpWebRequest stream?
  • Date difference with leap year
  • Rearranging Cells in UITableView Bug & Saving Changes
  • Benchmarking RAM performance - UWP and C#
  • Angular 2 constructor injection vs direct access
  • IndexOutOfRangeException on multidimensional array despite using GetLength check