Note that there are some explanatory texts on larger screens.

plurals
  1. POFast weighted euclidean distance between points in arrays
    text
    copied!<p>I need to efficiently calculate the euclidean <em>weighted</em> distances for every <code>x,y</code> point in a given array to every other <code>x,y</code> point in another array. This is the code I have which works as expected:</p> <pre><code>import numpy as np import random def rand_data(integ): ''' Function that generates 'integ' random values between [0.,1.) ''' rand_dat = [random.random() for _ in range(integ)] return rand_dat def weighted_dist(indx, x_coo, y_coo): ''' Function that calculates *weighted* euclidean distances. ''' dist_point_list = [] # Iterate through every point in array_2. for indx2, x_coo2 in enumerate(array_2[0]): y_coo2 = array_2[1][indx2] # Weighted distance in x. x_dist_weight = (x_coo-x_coo2)/w_data[0][indx] # Weighted distance in y. y_dist_weight = (y_coo-y_coo2)/w_data[1][indx] # Weighted distance between point from array_1 passed and this point # from array_2. dist = np.sqrt(x_dist_weight**2 + y_dist_weight**2) # Append weighted distance value to list. dist_point_list.append(round(dist, 8)) return dist_point_list # Generate random x,y data points. array_1 = np.array([rand_data(10), rand_data(10)], dtype=float) # Generate weights for each x,y coord for points in array_1. w_data = np.array([rand_data(10), rand_data(10)], dtype=float) # Generate second larger array. array_2 = np.array([rand_data(100), rand_data(100)], dtype=float) # Obtain *weighted* distances for every point in array_1 to every point in array_2. dist = [] # Iterate through every point in array_1. for indx, x_coo in enumerate(array_1[0]): y_coo = array_1[1][indx] # Call function to get weighted distances for this point to every point in # array_2. dist.append(weighted_dist(indx, x_coo, y_coo)) </code></pre> <p>The final list <code>dist</code> holds as many sub-lists as points are in the first array with as many elements in each as points are in the second one (the weighted distances).</p> <p>I'd like to know if there's a way to make this code more efficient, perhaps using the <a href="http://docs.scipy.org/doc/scipy/reference/generated/scipy.spatial.distance.cdist.html#scipy.spatial.distance.cdist" rel="nofollow">cdist</a> function, because this process becomes quite expensive when the arrays have lots of elements (which in my case they have) and when I have to check the distances for lots of arrays (which I also have)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload