w3resource

NumPy: Find point by point distances of a random vector with shape (10,2) representing coordinates

NumPy: Random Exercise-12 with Solution

Write a NumPy program to find point by point distances of a random vector with shape (10,2) representing coordinates.

Sample Solution :

Python Code :

# Importing the NumPy library as np
import numpy as np

# Generating a 10x2 array 'a' with random values between 0 and 1 using np.random.random()
a = np.random.random((10, 2))

# Extracting x and y coordinates from the array 'a' using np.atleast_2d() method
# at least 2-dimensional arrays from the columns of 'a'
x, y = np.atleast_2d(a[:, 0], a[:, 1])

# Calculating the Euclidean distance between points in the 'a' array using the formula:
# d = sqrt((x - x_transpose)^2 + (y - y_transpose)^2)
# where x and y are the coordinate matrices created, and T is the transpose of those matrices
d = np.sqrt((x - x.T) ** 2 + (y - y.T) ** 2)

# Displaying the resultant matrix containing the distances between points
print(d) 

Sample Output:

[[ 0.          0.42309206  0.59799949  0.44076341  0.64568848  0.53547789                                                                     
   0.39263926  0.38982854  0.39228612  0.44529689]                     
 [ 0.42309206  0.          1.0208224   0.38421553  0.92200913  0.73182405                                                                     
   0.73513877  0.80796679  0.64687549  0.73308548]                     
 [ 0.59799949  1.0208224   0.          0.94341451  0.66895368  0.71852306                                                                     
   0.48605697  0.22453123  0.64173589  0.55440442]                     
 [ 0.44076341  0.38421553  0.94341451  0.          0.60846866  0.9467662                                                                      
   0.51350109  0.78044594  0.36885259  0.88113793]                     
 [ 0.64568848  0.92200913  0.66895368  0.60846866  0.          1.12199998                                                                     
   0.27042981  0.66189707  0.27709162  0.98097303]                     
 [ 0.53547789  0.73182405  0.71852306  0.9467662   1.12199998  0.      
   0.85159212  0.53429835  0.90830578  0.1651062 ]                     
 [ 0.39263926  0.73513877  0.48605697  0.51350109  0.27042981  0.85159212                                                                     
   0.          0.41507505  0.16091134  0.71215345]                     
 [ 0.38982854  0.80796679  0.22453123  0.78044594  0.66189707  0.53429835                                                                     
   0.41507505  0.          0.54198973  0.370672  ]                     
 [ 0.39228612  0.64687549  0.64173589  0.36885259  0.27709162  0.90830578                                                                     
   0.16091134  0.54198973  0.          0.78707458]                     
 [ 0.44529689  0.73308548  0.55440442  0.88113793  0.98097303  0.1651062                                                                      
   0.71215345  0.370672    0.78707458  0.        ]]

Explanation:

In the above code –

a = np.random.random((10,2)): This line creates a 10x2 array a with random floating-point values between 0 and 1 using the np.random.random() function.

x, y = np.atleast_2d(a[:,0], a[:,1]): This line separates the two columns of a into two 1D arrays and converts them into 2D arrays with shape (10,1) using the np.atleast_2d() function. x contains the first column of a and y contains the second column of a.

d = np.sqrt( (x-x.T)**2 + (y-y.T)**2): This line computes the Euclidean distance between all pairs of points (rows) in a using the following steps:

  • x-x.T and y-y.T compute the difference between all pairs of x and y coordinates, respectively. Since x and y have shape (10,1), subtracting their transposes (with shape (1,10)) results in broadcasting, producing two 10x10 arrays where each element represents the difference in x or y coordinates between a pair of points.
  • (x-x.T)**2 and (y-y.T)**2 square each element of the resulting arrays from the previous step.
  • (x-x.T)**2 + (y-y.T)**2 adds the squared differences element-wise, resulting in a 10x10 array where each element represents the squared Euclidean distance between a pair of points.
  • np.sqrt() computes the square root of each element in the resulting array, resulting in a 10x10 array d where each element represents the Euclidean distance between a pair of points in a.

Python-Numpy Code Editor:

Previous: Write a NumPy program to create random vector of size 15 and replace the maximum value by -1.
Next: Write a NumPy program to find the most frequent value in an array.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/python-exercises/numpy/python-numpy-random-exercise-12.php