[Numpy-discussion] fast SSD

Keith Goodman kwgoodman@gmail....
Tue Jun 21 19:25:39 CDT 2011

On Tue, Jun 21, 2011 at 5:09 PM, Alex Flint <alex.flint@gmail.com> wrote:
> Is there a fast way to compute an array of sum-of-squared-differences
> between a (small)  K x K array and all K x K sub-arrays of a larger array?
> (i.e. each element x,y in the output array is the SSD between the small
> array and the sub-array (x:x+K, y:y+K)
> My current implementation loops over each sub-array and computes the SSD
> with something like ((A-B)**2).sum().

I don't know of a clever way. But if ((A-B)**2).sum() is a sizable
fraction of the time, then you could use bottleneck:

	>> a = np.random.rand(5,5)
	>> timeit (a**2).sum()
	100000 loops, best of 3: 4.63 us per loop
	>> import bottleneck as bn
	>> timeit bn.ss(a)
	1000000 loops, best of 3: 1.77 us per loop
	>> func, b = bn.func.ss_selector(a, axis=None)
	>> func
	   <built-in function ss_2d_float64_axisNone>
	>> timeit func(b)
	1000000 loops, best of 3: 830 ns per loop

More information about the NumPy-Discussion mailing list