All Questions
Tagged with euclidean-distance scipy
61
questions
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....
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
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 ...
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 ...
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
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),
(...
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 ...
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
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
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 ...
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
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
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....
3
votes
3
answers
1k
views
Calculate euclidean distance between groups in a data frame
I have weekly data for various stores in the following form:
pd.DataFrame({'Store':['S1', 'S1', 'S1', 'S2','S2','S2','S3','S3','S3'], 'Week':[1, 2, 3,1,2,3,1,2,3],
'Sales' :...
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
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
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
912
views
Finding distance between elements of two different list
The code below finds the Euclidean distance between each element of list a and each element of list b.
from scipy.spatial import distance
a = [[1, 2, 3], [4, 5, 6]]
b = [[10, 20]]
Final_distance = []...
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 ...
2
votes
2
answers
1k
views
Calculating pairwise spatial distances in periodic 2D lattice
I've been searching in vain for a solution to compute spatial distances between discrete, equidistant square sites on a lattice of periodic/wrapped edges. For example, setting adjacent sites to be 2 ...
2
votes
1
answer
2k
views
Calculate average weighted euclidean distance between values in numpy
I searched a bit around and found comparable questions/answers, but none of them returned the correct results for me.
Situation:
I have an array with a number of clumps of values == 1, while the rest ...
2
votes
1
answer
5k
views
Euclidean Distance between Scipy Sparse Matrix and Sparse Vector
I am working on a KNN algorithm for a university assignment and at the moment I'm working on finding the Euclidean distance between each of the training vectors stored as a Scipy lil_matrix (due to ...
2
votes
2
answers
280
views
Efficiently updating distance between points
I have a data set that has n rows (observations) and p columns (features):
import numpy as np
from scipy.spatial.distance import pdist, squareform
p = 3
n = 5
xOld = np.random.rand(n * p).reshape([n, ...
2
votes
3
answers
8k
views
Euclidean distances (python3, sklearn): efficiently compute closest pairs and their corresponding distances
I'm given a 2-D numpy array X consisting of floating values and need to compute the euclidean distances between all pairs of rows, then compute the top k row indices with the smallest distances and ...
2
votes
1
answer
1k
views
Vectorizing euclidean distance computation - NumPy
my question regards the vectorization of my code. I have one array that holds 3D-coordinates and one array that holds the information of edges that connect the coordinates:
In [8]:coords
Out[8]:
...
2
votes
0
answers
357
views
Is there a better and faster way to convert from Scipy condensed distance matrix to a Scipy sparse distance matrix under a threshold
I'm trying to calculate the euclidean distance between n-dimensional points, and then get a sparse distance matrix of all points where the distance is under a set threshold.
I've already got a ...
1
vote
4
answers
3k
views
Numpy distance calculations of different shaped arrays
Not sure I titled this well, but basically I have a reference coordinate, in the format of (x,y,z), and a large list/array of coordinates also in that format. I need to get the euclidean distance ...
1
vote
3
answers
1k
views
Find all shortest Euclidean distances between two groups of point coordinates
I have a Pandas DataFrame, where columns X1, Y1 have point coordinates for the first group of coordinates and columns X2, Y2 have point coordinates for the second group of coordinates. Both groups are ...
1
vote
1
answer
2k
views
numpy.linalg.norm VS scipy cdist for L2 norm
Very in advance apologies for my basic question!
Given:
a = np.random.rand(6, 3)
b = np.random.rand(6, 3)
Using scipy.spatial.distance.cdist and d = cdist(a, b, 'euclidean'), results in:
[[0....
1
vote
2
answers
2k
views
Get indices of results from scipy.pdist(myArray,metric="jaccard") to map back to original array?
I am trying to calculate jaccard similarity
y= 1 - scipy.spatial.distance.pdist(X,metric="jaccard")
X is a m x n matrix and I get a 1-D array of size m choose 2 as a result of this function. How ...
1
vote
1
answer
722
views
Determining the average euclidean distance between neighbouring points in dataset
I would like to compute the average euclidean distance in a 2D dataset (xCoords , yCoords) but only between neighbouring points.
As an example:
xCoords = [[16.8742 10.7265 30.0538 10.4524 12.6483 15....
1
vote
1
answer
273
views
Apply euclidean distance function with null values - Scipy
I've got this function from a separate question: How to apply euclidean distance function to a groupby object in pandas dataframe?. The following function measures the distance between objects grouped ...
1
vote
1
answer
502
views
Get Index of data point in training set with shortest distance to input matrix with Numpy
I would like to build a function npbatch(U,X) which compares data points in an input matrix (U) with data points in a training matrix (X) and gets me the index of X with the shortest euclidean ...
1
vote
1
answer
182
views
Is there any difference of precision in python methods to calculate euclidean distance?
I am calculating euclidean distance array by array in a numpy array. I was using np.linalg.norm(v1-v2) to this. Since I was planning to use other distance measures I changed that to scipy.spatial....
1
vote
0
answers
57
views
Find closest point to another point with NumPy and/or SciPy quickly [duplicate]
I want to know the fastest way in python to calculate the closest point in a list of points to my point. There is already this question, but it does not consider performance and there are the ...
1
vote
0
answers
164
views
Python: Reshaping arrays and lists
I have a numpy ndarray object with the following shape:
(3, 256, 170, 256).
So, basically this represents an array of 3-dimensional vectors. The dimension of the vector is the first element as it ...
0
votes
2
answers
322
views
Cluster Analysis: Problem finding Euclidean distances of centroids in a dataframe from origin
The 7 columns for each row in df_centroids show the coordinates in a 7 dimensional space.
import numpy as np
import pandas as pd
import scipy
df_centroids
0 1 2 ...
0
votes
2
answers
233
views
Eucledian distance matrix between two matrices
I have the following function that calculates the eucledian distance between all combinations of the vectors in Matrix A and Matrix B
def distance_matrix(A,B):
n=A.shape[1]
m=B.shape[1]
C=...
0
votes
3
answers
679
views
Least Squares Linear Programming with inequalities
I am using Numpy np.linalg.lstsq(A, b, rcond=-1) to solve a system of equations with an approximate solution via the Least Squares method.
However I realised the approximate solution contains negative ...
0
votes
1
answer
2k
views
are numpy record/structure arrays restricted to containing numpy voids?
I have a complicated set of data that I have to do distance calculations on. Each record in the data set contains many different data types so a record array or structured array appears to be the way ...
0
votes
1
answer
230
views
How to change how distance is calculated in a scipy distance matrix?
I have the following dataframe (df):
x y
0 50 50
1 18 83
2 6 21
3 30 13
4 44 29
5 81 68
which corresponds to the x and y coordinates of 6 points.
I am using scipy's distance_matrix ...
0
votes
1
answer
583
views
Alternative (python) to calculate distance between all points at two different sets
I have two different bunches of points: set_1 and set_2.
Using python, I want to calculate all distances from points at set_1 to points at set_2.
Poins are 1dimension arrays:
point_1=np.array([x1,y1,...
0
votes
2
answers
395
views
Argmin with the Euclidean distance condition
In Python, I have a vector v of 300 elements and an array arr of 20k 300-dimensional vectors. How do I get quickly the indices of the k closest elements to v from the array arr?
0
votes
1
answer
343
views
Euclidean distance and indicator from a large dataframe
I have a large Dataframe (189090, 8), I need to calculate Euclidean distance and the similarity.
My approach:
from scipy.spatial import KDTree
from scipy.spatial.distance import pdist
scaler = ...
0
votes
2
answers
2k
views
Scipy.spatial.distance does not work when imported as normal
I am trying to calculate the euclidean distance between two points in my Python code. However, when I import scipy.spatial in the following way: from scipy.spatial import distance and when I call: ...
0
votes
1
answer
692
views
scipy.spatial.distance.euclidean and scipy.spatial.- distance_matrix not returning the same result
I was using agglomerative clustering technique to cluster a vehicle dataset. I used two methods to calculate the distance matrix, one was using scipy.spatial.distance.euclidean and other using scipy....
0
votes
1
answer
2k
views
Python - Issue with the dimension of array in cdist function
I am trying to find the right number of cluster for k-means and using the cdist function for this.
I can understand the argument for cdist should be of same dimension. I tried printing the size of ...