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 :
import numpy as np
a= np.random.random((10,2))
x,y = np.atleast_2d(a[:,0], a[:,1])
d = np.sqrt( (x-x.T)**2 + (y-y.T)**2)
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. ]]
Python-Numpy Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
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.
Python: Tips of the Day
Find current directory and file's directory:
To get the full path to the directory a Python file is contained in, write this in that file:
import os dir_path = os.path.dirname(os.path.realpath(__file__))
(Note that the incantation above won't work if you've already used os.chdir() to change your current working directory, since the value of the __file__ constant is relative to the current working directory and is not changed by an os.chdir() call.)
To get the current working directory use
import os cwd = os.getcwd()
Documentation references for the modules, constants and functions used above:
- The os and os.path modules.
- The __file__ constant
- os.path.realpath(path) (returns "the canonical path of the specified filename, eliminating any symbolic links encountered in the path")
- os.path.dirname(path) (returns "the directory name of pathname path")
- os.getcwd() (returns "a string representing the current working directory")
- os.chdir(path) ("change the current working directory to path")
Ref: https://bit.ly/3fy0R6m
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework