euclidean-distance

Is there a way to calculate the following specified matrix by avoiding loops? in R or Matlab

。_饼干妹妹 提交于 2020-01-05 10:31:24
问题 I have an N-by-M matrix X , and I need to calculate an N-by-N matrix Y : Y[i, j] = sum((X[i,] - X[j,]) ^ 2) 0 <= i,j <= N For now, I have to use nested loops to do it with O(n 2 ). I would like to know if there's a better way, like using matrix operations. more generally, sum(....) can be a function, fun(x1,x 2) of which x1 , x2 are M-by-1 vectors. 回答1: you can use expand.grid to get a data.frame of possible pairs: X <- matrix(sample(1:5, 50, replace=TRUE), nrow=10) row.ind <- expand.grid(1

When using RMSE loss in TensorFlow I receive very small loss values smalerl than 1 [closed]

久未见 提交于 2020-01-05 07:42:37
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 2 years ago . Hello I have a network that produces logits / outputs like this: logits = tf.placeholder(tf.float32, [None, 128, 64, 64]) // outputs y = tf.placeholder(tf.float32, [None, 128, 64, 64]) // ground_truth, targets --> y ground truth values are downscaled from [0, 255] to [0, 1] in order to increase

Vectorizing euclidean distance computation - NumPy

妖精的绣舞 提交于 2020-01-05 01:32:13
问题 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]: array([[ 11.22727013, 24.72620964, 2.02986932], [ 11.23895836, 24.67577744, 2.04130101], [ 11.23624039, 24.63677788, 2.04096866], [ 11.22516632, 24.5986824 , 2.04045677], [ 11.21166992, 24.56095695, 2.03898215], [ 11.20334721, 24.5227356 , 2.03556442], [ 11.2064085 , 24.48479462, 2.03098583], [ 11

Euclidian Distance Python Implementation

两盒软妹~` 提交于 2020-01-03 10:56:13
问题 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 difference of the rankings in the dictionary, but euclidean distance in n dimensions also includes the square root of that sum. AFAIK since we use the same function to rank everyone it does not matter we square root or not, but i was wondering is there a particular reason for that? from math import

Euclidean distance with weights

与世无争的帅哥 提交于 2020-01-02 04:44:08
问题 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 dimension then, is it still possible to use scipy? What I have now: sqrt((a1-b1)^2 + (a2-b2)^2 +...+ (a5-b5)^2) What I want: sqrt(w1(a1-b1)^2 + w2(a2-b2)^2 +...+ w5(a5-b5)^2) using scipy or numpy or any other efficient way to do this. Thanks 回答1: The suggestion of writing your own weighted L2 norm

R: Finding the nearest raster cell within a threshold calculated between two rasters

♀尐吖头ヾ 提交于 2020-01-01 23:19:50
问题 I have two perfectly overlapping rasters (same extents and cell size). For every cell in one raster (i.e. for every XY), I would like to determine the Euclidean geographical distance to the closest cell within a given threshold difference between the rasters. Put another way: raster1 and raster2 measure some variable Z. I have a threshold difference for Z values (t) which constitutes a "matching" value (or "close enough") between raster1 and raster2. For each reference cell in raster1, I need

How to calculate euclidean distance between pair of rows of a numpy array

妖精的绣舞 提交于 2019-12-30 22:55:29
问题 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 each pair of rows. from scipy.spatial import distance for i in range(0,a.shape[0]): d = [np.sqrt(np.sum((a[i]-a[j])**2)) for j in range(i+1,a.shape[0])] print(d) [1.4142135623730951, 0.0, 1.4142135623730951] [1.4142135623730951, 2.0] [1.4142135623730951] [] Is there any better pythonic way to do this since i have to run this code on a

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

做~自己de王妃 提交于 2019-12-24 14:03:40
问题 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 = [216, 218, 217, 280, 290, 130, 132] y = [13, 12, 12, 110, 109, 3, 56] car = [1, 2, 3, 1, 3, 4, 5] df = pd.DataFrame({'time': time, 'x': x, 'y': y, 'car': car}) df x y car time 0 216 13 1 0 218 12 2 0 217 12 3 1 280 110 1 1 290 109 3 2 130 3 4 2 132 56 5 The end result I would like to have is:

How to improve processing time for euclidean distance calculation

爱⌒轻易说出口 提交于 2019-12-24 02:23:07
问题 I'm trying to calculate the weighted euclidean distance (squared) between twoo data frames that have the same number of columns (variables) and different number of rows (observations). The calculation follows the formula: DIST[m,i] <- sum(((DATA1[m,] - DATA2[i,]) ^ 2) * lambda[1,]) I specifically need to multiply each parcel of the somatory by a specific weight (lambda). The code provided bellow runs correctly, but if I use it in hundreds of iterations it takes a lot of processing time.

MatLab - Euclidean Distance Plot 3D

99封情书 提交于 2019-12-23 05:12:30
问题 Im new in matlab programming and I have small issue. I want to draw a plot 3d of Euclidean distance function for 2 coordinates, like in this picture below: Could you help me with the source code? How I can draw this plot? My first thoughts was wrong: [A] = meshgrid(-100:.5:100, -100:.5:100); D1 = bwdist(A); figure surf(double(A), double(D1)) 回答1: It is done like this... [x, y] = meshgrid(-100:.5:100, -100:.5:100); The you have to calculated the euclidean distances. I assume you want them with