[Numpy-discussion] record arrays and vectorizing
aronne.merrelli at gmail.com
Wed May 2 17:54:10 EDT 2012
On Wed, May 2, 2012 at 1:06 PM, Moroney, Catherine M (388D)
<Catherine.M.Moroney at jpl.nasa.gov> wrote:
> Can somebody give me some hints as to how to code up this function
> in pure python, rather than dropping down to Fortran?
> I will want to compare a 7-element vector (called "element") to a large list of similarly-dimensioned
> vectors (called "target", and pick out the vector in "target" that is the closest to "element"
> (determined by minimizing the Euclidean distance).
> For instance, in (slow) brute force form it would look like:
> element = numpy.array([1, 2, 3, 4, 5, 6, 7])
> target = numpy.array(range(0, 49)).reshape(7,7)*0.1
> min_length = 9999.0
> min_index =
> for i in xrange(0, 7):
> distance = (element-target)**2
> distance = numpy.sqrt(distance.sum())
> if (distance < min_length):
> min_length = distance
> min_index = i
If you are just trying to find the index to the vector in "target"
that is closest to element, then I think the default broadcasting
would work fine. Here is an example that should work (the broadcasting
is done for the subtraction element-targets):
In : element = np.arange(1,8)
In : targets = np.random.uniform(0,8,(1000,7))
In : distance_squared = ((element-targets)**2).sum(1)
In : min_index = distance_squared.argmin()
In : element
Out: array([1, 2, 3, 4, 5, 6, 7])
In : targets[min_index,:]
array([ 1.93625981, 2.56137284, 2.23395169, 4.15215253, 3.96478248,
Note - depending on the number of vectors in targets, it might be
better to have everything transposed if you are really worried about
the timing; you'd need to try that for your particular case.
Hope that helps,
More information about the NumPy-Discussion