All Questions

Filter by
Sorted by
Tagged with
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: ...
Nathan Fellman's user avatar
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 ...
fideli's user avatar
  • 3,243
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....
pacodelumberg's user avatar
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 ...
Wes Modes's user avatar
  • 2,074
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 ...
J_yang's user avatar
  • 2,702
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 ...
Spider's user avatar
  • 977
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....
garak's user avatar
  • 4,733
21 votes
3 answers
42k views

Distance calculation between rows in Pandas Dataframe using a distance matrix

I have the following Pandas DataFrame: In [31]: import pandas as pd sample = pd.DataFrame({'Sym1': ['a','a','a','d'],'Sym2':['a','c','b','b'],'Sym3':['a','c','b','d'],'Sym4':['b','b','b','a']},index=[...
Clayton's user avatar
  • 1,545
15 votes
3 answers
2k views

Efficient calculation of euclidean distance

I have a MxN array, where M is the number of observations and N is the dimensionality of each vector. From this array of vectors, I need to calculate the mean and minimum euclidean distance between ...
japata's user avatar
  • 329
14 votes
6 answers
23k views

Efficient and precise calculation of the euclidean distance

Following some online research (1, 2, numpy, scipy, scikit, math), I have found several ways for calculating the Euclidean Distance in Python: # 1 numpy.linalg.norm(a-b) # 2 distance.euclidean(...
user avatar
11 votes
2 answers
18k views

Find euclidean distance from a point to rows in pandas dataframe

i have a dataframe id lat long 1 12.654 15.50 2 14.364 25.51 3 17.636 32.53 5 12.334 25.84 9 32.224 15.74 I want to find the euclidean distance of these ...
Shubham R's user avatar
  • 7,562
10 votes
1 answer
13k views

Compute Euclidean distance between rows of two pandas dataframes

I have two pandas dataframes d1 and d2 that look like these: d1 looks like: output value1 value2 value2 1 100 103 87 1 201 97.5 88.9 1 ...
j1897's user avatar
  • 1,547
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: ...
VansFannel's user avatar
  • 45.4k
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]...
Ηλίας's user avatar
  • 2,600
9 votes
2 answers
7k views

Minimize total distance between two sets of points in Python

Given two sets of points in n-dimensional space, how can one map points from one set to the other, such that each point is only used once and the total euclidean distance between the pairs of points ...
Keith Hughitt's user avatar
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 ...
Maggie's user avatar
  • 5,963
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 ...
user1462351's user avatar
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 ...
user_1_1_1's user avatar
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 ...
user avatar
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(...
Quicklearner.gk's user avatar
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 ...
Gabriel's user avatar
  • 41.7k
8 votes
1 answer
5k views

Caffe Iteration loss versus Train Net loss

I'm using caffe to train a CNN with a Euclidean loss layer at the bottom, and my solver.prototxt file configured to display every 100 iterations. I see something like this, Iteration 4400, loss = 0 ...
user3543300's user avatar
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 ...
user2768102's user avatar
7 votes
2 answers
11k views

Pycuda Blocks and Grids to work with big datas

I need help to know the size of my blocks and grids. I'm building a python app to perform metric calculations based on scipy as: Euclidean distance, Manhattan, Pearson, Cosine, joined other. The ...
Vinnicyus Gracindo's user avatar
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), (...
heltonbiker's user avatar
  • 27.2k
7 votes
1 answer
802 views

Euclidean distance, different results between Scipy, pure Python, and Java

I was playing around with different implementations of the Euclidean distance metric and I noticed that I get different results for Scipy, pure Python, and Java. Here's how I compute the distance ...
Silas Berger's user avatar
6 votes
4 answers
4k views

Euclidian Distance Python Implementation

I am playing with the following code from programming collective intelligence, this is a function from the book that calculated eclidian distance between two movie critics. This function sums the ...
Hamza Yerlikaya's user avatar
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 ...
Jérôme's user avatar
  • 152
6 votes
1 answer
6k views

Why cdist from scipy.spatial.distance is so fast?

I wanted to create a distance proximity matrix for 10060 records/ points, where each record/point has 23 attributes using euclidean distance as metric. I wrote code using nested for loops to calculate ...
Sushodhan's user avatar
  • 400
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 ...
user1658296's user avatar
  • 1,418
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 ...
jfran's user avatar
  • 143
5 votes
1 answer
9k views

Calculating Euclidian Norm in Pytorch.. Trouble understanding an implementation

I've seen another StackOverflow thread talking about the various implementations for calculating the Euclidian norm and I'm having trouble seeing why/how a particular implementation works. The code ...
Fosa's user avatar
  • 572
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,...
DummyGuy's user avatar
  • 425
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(...
ToneDaBass's user avatar
5 votes
2 answers
4k views

How to implement callable distance metric in scikit-learn?

I'm using the clustering module in python's scikit learn, and I'd like to use a Normalized Euclidean Distance. There is no built-in distance for this (that i know of) Here's a list. So, I want to ...
makansij's user avatar
  • 9,583
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 ...
Rashmi Singh's user avatar
4 votes
8 answers
4k views

Python: reduce on tuple of tuples

I am trying to compute in Python the length of the path from a point A to a point B going through a list of intermediary points. I know how to do it but I do want to use the reduce Built-in function. ...
lc2817's user avatar
  • 3,734
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) ...
LWZ's user avatar
  • 11.9k
4 votes
2 answers
4k views

calculating average distance of nearest neighbours in pandas dataframe

I have a set of objects and their positions over time. I would like to get the distance between each car and their nearest neighbour, and calculate an average of this for each time point. An example ...
UserR6's user avatar
  • 503
4 votes
2 answers
13k views

Optimize performance for calculation of euclidean distance between two images

I implemented the k-nearest-neighbours algorithm in python to classify some randomly picked images from the mnist database. However I found my distance function to be quite slow: An analisys of 10 ...
wodzu's user avatar
  • 3,112
4 votes
2 answers
1k views

Euclidean Minimal Spanning Tree and Delaunay Triangulation

I want to calculate the minimal spanning tree based on the euclidean distance between a set of points on a 2D-plane. My current code stores all the edges, and then performs Prim's algorithm in order ...
Jingjie Yang's user avatar
4 votes
2 answers
5k views

Calculate the euclidean distance in scipy csr matrix

I need to calculate the Euclidean Distance between all points that is stored in csr sparse matrix and some lists of points. It would be easier for me to convert the csr to a dense one, but I couldn't ...
Rochana Nana's user avatar
4 votes
4 answers
4k views

How to apply euclidean distance function to a groupby object in pandas dataframe?

I have a set of objects and their positions over time. I would like to get the average distance between objects for each time point. An example dataframe is as follows: time = [0, 0, 0, 1, 1, 2, 2] x ...
UserR6's user avatar
  • 503
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)...
tavalendo's user avatar
  • 857
4 votes
1 answer
3k views

Calculating Euclidean Distance With Given Lists

def distance(alist, blist): sum_of = 0 for x in alist: for y in blist: ans = (x - y)**2 sum_of += ans return (sum_of)**(1/2) print(distance([1, 1, 3], [2, 2,...
Laser's user avatar
  • 43
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, ...
jjepsuomi's user avatar
  • 4,313
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....
Gabriel's user avatar
  • 41.7k
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-...
user2229953's user avatar
  • 1,559
4 votes
2 answers
5k views

Can we cluster Multivariate Time Series dataset in Python

I have a dataset with many financial signal values for different stocks at different times.For example StockName Date Signal1 Signal2 ---------------------------------- Stock1 1/1/20 a ...
Zhang Yongheng's user avatar
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 ...
xShirase's user avatar
  • 12.2k

1
2 3 4 5
8