All Questions
Tagged with euclidean-distance numpy
130
questions
790
votes
26
answers
1.4m
views
How can the Euclidean distance be calculated with NumPy?
I have two points in 3D space:
a = (ax, ay, az)
b = (bx, by, bz)
I want to calculate the distance between them:
dist = sqrt((ax-bx)^2 + (ay-by)^2 + (az-bz)^2)
How do I do this with NumPy? I have:
...
46
votes
9
answers
62k
views
Minimum Euclidean distance between points in two different Numpy arrays, not within
I have two arrays of x-y coordinates, and I would like to find the minimum Euclidean distance between each point in one array with all the points in the other array. The arrays are not necessarily the ...
35
votes
5
answers
109k
views
python numpy euclidean distance calculation between matrices of row vectors
I am new to Numpy and I would like to ask you how to calculate euclidean distance between points stored in a vector.
Let's assume that we have a numpy.array each row is a vector and a single numpy....
32
votes
5
answers
61k
views
Efficiently Calculating a Euclidean Distance Matrix Using Numpy
I have a set of points in 2-dimensional space and need to calculate the distance from each point to each other point.
I have a relatively small number of points, maybe at most 100. But since I need ...
27
votes
2
answers
38k
views
Is "norm" equivalent to "Euclidean distance"?
I am not sure whether "norm" and "Euclidean distance" mean the same thing. Please could you help me with this distinction.
I have an n by m array a, where m > 3. I want to calculate the Eculidean ...
22
votes
6
answers
38k
views
Find the shortest distance between a point and line segments (not line)
I have set of line segments (not lines), (A1, B1), (A2, B2), (A3, B3), where A,B are ending points of the line segment. Each A and B has (x,y) coordinates.
QUESTION:
I need to know the shortest ...
21
votes
7
answers
36k
views
Multidimensional Euclidean Distance in Python
I want to calculate the Euclidean distance in multiple dimensions (24 dimensions) between 2 arrays. I'm using numpy-Scipy.
Here is my code:
import numpy,scipy;
A=numpy.array([116.629, 7192.6, 4535....
10
votes
5
answers
11k
views
Compute L2 distance with numpy using matrix multiplication
I'm trying to do it by myself the assignments from Stanford CS231n 2017 CNN course.
I'm trying to compute L2 distance using only matrix multiplication and sum broadcasting with Numpy. L2 distance is:
...
9
votes
7
answers
10k
views
Identifying points with the smallest Euclidean distance
I have a collection of n dimensional points and I want to find which 2 are the closest. The best I could come up for 2 dimensions is:
from numpy import *
myArr = array( [[1, 2],
[3, 4]...
9
votes
4
answers
10k
views
Euclidean distance with weights
I am currently using SciPy to calculate the euclidean distance
dis = scipy.spatial.distance.euclidean(A,B)
where; A, B are 5-dimension bit vectors. It works fine now, but if I add weights for each ...
8
votes
3
answers
14k
views
Memory Efficient L2 norm using Python broadcasting
I am trying to implement a way to cluster points in a test dataset based on their similarity to a sample dataset, using Euclidean distance. The test dataset has 500 points, each point is a N ...
8
votes
3
answers
23k
views
Python alternative for calculating pairwise distance between two sets of 2d points [duplicate]
In Matlab there exists the pdist2 command. Given the matrix mx2 and the matrix nx2, each row of matrices represents a 2d point. Now I want to create a mxn matrix such that (i,j) element represents the ...
8
votes
1
answer
4k
views
einsum and distance calculations
I have searched for a solution to determine distances using einsum for numpy arrays that are not equal in their number of rows, but equal in columns. I have tried various combinations but the only ...
8
votes
2
answers
16k
views
Calculating pairwise Euclidean distance between all the rows of a dataframe
How can I calculate the Euclidean distance between all the rows of a dataframe? I am trying this code, but it is not working:
zero_data = data
distance = lambda column1, column2: pd.np.linalg.norm(...
8
votes
2
answers
3k
views
Find minimum distances between groups of points in 2D (fast and not too memory consuming)
I have two sets of points in 2D A and B and I need to find the minimum distance for each point in A, to a point in B. So far I've been using SciPy's cdist with the code below
import numpy as np
from ...
8
votes
1
answer
2k
views
Clustering in python(scipy) with space and time variables
The format of my dataset:
[x-coordinate, y-coordinate, hour] with hour an integer value from 0 to 23.
My question now is how can I cluster this data when I need an euclidean distance metric for the ...
7
votes
1
answer
6k
views
Calculating euclidean distance between consecutive points of an array with numpy
I have an array which describes a polyline (ordered list of connected straight segments) as follows:
points = ((0,0),
(1,2),
(3,4),
(6,5),
(10,3),
(...
6
votes
1
answer
4k
views
Calculate the euclidian distance between an array of points to a line segment in Python without for loop
I'm looking for a function to compute the euclidian distance between a numpy array of points with two coordinates (x, y) and a line segment. My goal is to have a result in under 0.01 sec for a line ...
6
votes
3
answers
5k
views
Numpy: find the euclidean distance between two 3-D arrays
Given, two 3-D arrays of dimensions (2,2,2):
A = [[[ 0, 0],
[92, 92]],
[[ 0, 92],
[ 0, 92]]]
B = [[[ 0, 0],
[92, 0]],
[[ 0, 92],
[92, 92]]]
How do you find the Euclidean ...
6
votes
4
answers
2k
views
Sum of distances from a point to all other points
I have two lists
available_points = [[2,3], [4,5], [1,2], [6,8], [5,9], [51,35]]
and
solution = [[3,5], [2,1]]
I'm trying to pop a point in available_points and append it to solution for which ...
5
votes
2
answers
4k
views
Calculate Euclidean Distance within points in numpy array
I have 3D array as
A = [[x1 y1 z1]
[x2 y2 z2]
[x3 y3 z3]]
I have to find euclidean distance between each points so that I'll get output with only 3 distance between (row0,row1),(row1,...
5
votes
4
answers
589
views
Vectorize mask of squared euclidean distance in Python
I'm running code to generate a mask of locations in B closer than some distance D to locations in A.
N = [[0 for j in range(length_B)] for i in range(length_A)]
dSquared = D*D
for i in range(...
5
votes
2
answers
6k
views
Fastest way to Calculate the Euclidian distance between 2 sets of vectors using numpy or scipy
OK I have recently discovered that the the scipy.spatial.distance.cdist command is very quick for solving a COMPLETE distance matrix between two vector arrays for source and destination.
see: How can ...
4
votes
3
answers
17k
views
How to calculate euclidean distance between pair of rows of a numpy array
I have a numpy array like:
import numpy as np
a = np.array([[1,0,1,0],
[1,1,0,0],
[1,0,1,0],
[0,0,1,1]])
I would like to calculate euclidian distance between ...
4
votes
3
answers
16k
views
In Numpy, find Euclidean distance between each pair from two arrays
I have two arrays of 2D coordinate points (x,y)
a = [ (x1,y1), (x2,y2), ... (xN,yN) ]
b = [ (X1,Y1), (X2,Y2), ... (XN,YN) ]
How can I find the Euclidean distances between each aligned pairs (xi,yi) ...
4
votes
1
answer
5k
views
Vectorized implementation for Euclidean distance [duplicate]
I am trying to compute a vectorized implementation of Euclidean distance(between each element in X and Y using inner product). The data as follows:
X = np.random.uniform(low=0, high=1, size=(10000, 5)...
4
votes
1
answer
3k
views
How to find the closest corresponding vectors between two coordinate matrices?
I have the following problem in Python I need to solve:
Given two coordinate matrices (NumPy ndarrays) A and B, find for all coordinate vectors a in A the corresponding coordinate vectors b in B, ...
4
votes
1
answer
3k
views
Find distance to nearest neighbor in 2d array
I have a 2D array and I want to find for each (x, y) point the distance to its nearest neighbor as fast as possible.
I can do this using scipy.spatial.distance.cdist:
import numpy as np
from scipy....
4
votes
2
answers
4k
views
Error - Calculating Euclidean distance for PCA in python
I am trying to implement face recognition by Principal Component Analysis (PCA) using python. I am following the steps in this tutorial: http://onionesquereality.wordpress.com/2009/02/11/face-...
4
votes
2
answers
2k
views
Pass coordinates of 2D Numpy pixel array to distance function
I'm working on an image processing program with OpenCV and numpy. For most pixel operations, I'm able to avoid nested for loops by using np.vectorize(), but one of the functions I need to implement ...
4
votes
3
answers
4k
views
Compute numpy array pairwise Euclidean distance except with self
edit: this question is not specifically about calculating distances, rather the most efficient way to loop through a numpy array, specifying that for index i all comparisons should be made with the ...
3
votes
2
answers
5k
views
Finding squared distances beteen n points to m points in numpy
I have 2 numpy arrays (say X and Y) which each row represents a point vector.
I would like to find the squared euclidean distances (will call this 'dist') between each point in X to each point in Y.
I ...
3
votes
1
answer
1k
views
Minimal edge-to-edge euclidean distance between labeled components in numpy array
I have a number of distinct forms in large numpy arrays and I want to calculate the edge-to-edge euclidean distance between them using numpy and scipy.
Note: I did a search and this is different ...
3
votes
2
answers
7k
views
fastest way to find euclidean distance in python
I have 2 sets of 2D points (A and B), each set have about 540 points. I need to find the points in set B that are farther than a defined distance alpha from all the points in A.
I have a solution, ...
3
votes
1
answer
657
views
Finding euclidean difference between coordinates in numpy
I am trying to calculate the difference between coordinates using numpy using following code :
X = np.random.random((1000, 3))
# using broadcasting to calculate to find pairwise diffrence
diff = X....
3
votes
2
answers
3k
views
Creating a 2-dimensional Numpy array with the euclidean distance from the center
I'm trying to create a 2-dimensional array in Scipy/Numpy where each value represents the euclidean distance from the center. It's supposed to have the same shape as the first two dimensions of a 3-...
3
votes
2
answers
1k
views
Euclidian Distances between points
I have an array of points in numpy:
points = rand(dim, n_points)
And I want to:
Calculate all the l2 norm (euclidian distance) between a certain point and all other points
Calculate all pairwise ...
3
votes
2
answers
228
views
How to get m pair of points among n points that have the largest distance between them
Say I have the following points defined in a one dimensional space:
x = np.array([[0.70710678],
[0.70710678],
[0. ],
[1.41421356]])
I want to get m pair ...
3
votes
1
answer
1k
views
What is the most efficient way to compute the square euclidean distance between N samples and clusters centroids?
I am looking for an efficient way (no for loops) to compute the euclidean distance between a set of samples and a set of clusters centroids.
Example:
import numpy as np
X = np.array([[1,2,3],[1, 1, ...
3
votes
1
answer
196
views
Optimise Euclidean distance matrix algorithm if only interested in closest points
The following Euclidean distance algorithm creates a MxM matrix of distances between the rows of an MxN input matrix (representative of points in some N dimensional space). The speed of this algorithm ...
3
votes
2
answers
2k
views
What is the fastest way to compute the Euclidean distances of a very large matrix with complex numbers?
I have a very large input data set of 50,000 samples with 9 dimensions (i.e. a 50000x9 matrix). This data has been transformed using DFT:
dft_D = data.dot(dft(9).T) / np.sqrt(9)
I want to calculate ...
3
votes
2
answers
2k
views
Fastest way to calculate the shortest (euclidean) distance between points, in pandas dataframe
Consider the following pandas dataframe:
print(df)
Id X Y Type X of Closest Y of Closest
0 201 73.91 34.84 A NaN NaN
1 201 74.67 32.64 A ...
3
votes
2
answers
2k
views
fastest way to get closest 10 euclidean neighbors of large feature vector in python
I have a numpy array that has 10,000 vectors with 3,000 elements in each. I want to return the top 10 indices of the closest pairs with the distance between them. So if row 5 and row 7 have the ...
3
votes
1
answer
303
views
Euclidean Distance for Arrays of 3D points in Python
I have two .csv files of 3D points (numeric coordinate data) and associated attribute data (strings + numeric). I need to calculate the Euclidean distance between each point and every other point, and ...
3
votes
2
answers
1k
views
euclidean distance calculation using Python and Dask
I'm attempting to identify elements in the euclidean distance matrix that fall under a certain threshold. I then take the positional arguments for this search and use them to compare elements in a ...
3
votes
1
answer
1k
views
How to calculate the euclidean distance in Python without fixed-dimension?
I intend to calculate the euclidean distance between two sets of big data. I've googled that the module called SciPy will do the work, whose mechanism is via k-d tree.
But I don't have fixed ...
3
votes
1
answer
2k
views
How to compare great circle distance with euclidean distance of two sphere points using python?
I am trying to check the error that is introduced when you compute the distance of two points on earth with the euclidean distance instead of using the great circle distance (gcd). I have two points ...
2
votes
3
answers
4k
views
Calculate euclidean distance from dicts (sklearn)
I have two dictionaries already calculated in my code, which look like this:
X = {'a': 10, 'b': 3, 'c': 5, ...}
Y = {'a': 8, 'c': 3, 'e': 8, ...}
Actually they contain words from wiki texts, but ...
2
votes
6
answers
23k
views
Calculate Euclidean distance between two python arrays
I want to write a function to calculate the Euclidean distance between coordinates in list_a to each of the coordinates in list_b, and produce an array of distances of dimension a rows by b columns (...
2
votes
2
answers
2k
views
Pairwise Euclidean distance with pandas ignoring NaNs
I start with a dictionary, which is the way my data was already formatted:
import pandas as pd
dict2 = {'A': {'a':1.0, 'b':2.0, 'd':4.0}, 'B':{'a':2.0, 'c':2.0, 'd':5.0},
'C':{'b':1.0,'c':2.0, 'd':4....