Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List of coordinates to matrix of distances

I've got a list of 8 coordinates stored as list of lists:

coordinates = [[47.2486, -1.54806],
               [43.5656, 1.47417],
               [48.3592, -4.57],
               [48.1439, 17.1097],
               [39.6275, 140.198],
               [30.0458, 31.2625],
               [38.9371, -77.0869],
               [33.9, 35.4823]]

With the following script, I seek to output a matrix of coordinates:

import numpy
from scipy.spatial.distance import pdist

coordinates_array = numpy.array(coordinates)
dist_array = pdist(coordinates_array)

dist_matrix = numpy.reshape(dist_array, newshape=(len(coordinates), len(coordinates)))

However, I get an ValueError: total size of new array must be unchanged. Why is that?

like image 226
MERose Avatar asked Jun 08 '15 14:06

MERose


People also ask

How do you write a distance matrix?

The distance matrix between the shapes, D∈R+N×N, is calculated using the Adjacent Entries Distance between the self functional maps, where N is the number of the shapes in the benchmark (94)Dij=DAE(Ci,Cj)i,j∈{1… N}.

How do you find the distance between two points in a matrix?

This length can be computed with the help of Pythagora's theorem: dist = sqrt((x2-x1)^2 + (y2-y1)^2) . This is known as the Euclidian distance between the points.

What is distance matrix API?

The Distance Matrix API provides travel distance and time for a matrix of origins and destinations, and consists of rows containing duration and distance values for each pair. Distance Matrix is available in several forms: as a standalone API. as part of the client-side Maps JavaScript API.

How do you find the distance between two points in a matrix in python?

You can use the math. dist() function to get the Euclidean distance between two points in Python. For example, let's use it the get the distance between two 3-dimensional points each represented by a tuple.


1 Answers

As explained in the scipy documentation, use scipy.spatial.distance.squareform to convert the condensed distance matrix returned by pdist to a square distance matrix,

from scipy.spatial.distance import squareform 

dist_matrix = squareform(dist_array)
like image 174
rth Avatar answered Sep 23 '22 22:09

rth