[Numpy-discussion] Tuple outer product?
Keith Goodman
kwgoodman@gmail....
Fri Sep 25 16:26:41 CDT 2009
On Fri, Sep 25, 2009 at 1:37 PM, Gökhan Sever <gokhansever@gmail.com> wrote:
> Yes this is super fast indeed :)
>
> with 10000x10000
>
> I[12]: %time run test.py
> CPU times: user 6.14 s, sys: 1.83 s, total: 7.97 s
> Wall time: 8.25 s
>
> #test.py
>
> import numpy
>
> a = numpy.arange(10000)
> b = numpy.arange(10000)
>
> (n,m) = (a.shape[0],b.shape[0])
> a = numpy.repeat(a,m).reshape(n,m)
> b = numpy.repeat(b,n).reshape(m,n).transpose()
> ab = numpy.dstack((a,b))
No numpy-discussion thread can have too many timeit replies. Or at
least that is what I'll test here. To cut the time in half I think
we'll need a netflix prize.
def mesh1(a, b):
(n,m) = (a.shape[0],b.shape[0])
a = np.repeat(a,m).reshape(n,m)
b = np.repeat(b,n).reshape(m,n).transpose()
ab = np.dstack((a,b))
return ab.tolist()
def mesh2(a, b):
np.array(np.meshgrid(a,b)).transpose().tolist()
def mesh3(a, b):
ab = np.empty((a.shape[0], b.shape[0], 2), dtype=np.int)
ab.T[0] = a
ab[:,:,1] = b
return ab.tolist()
>> a = np.array([1,2,3])
>> b = np.array([4,5,6])
>>
>>
>> timeit mesh1(a,b)
10000 loops, best of 3: 31.4 µs per loop
>> timeit mesh2(a,b)
10000 loops, best of 3: 24.8 µs per loop
>> timeit mesh3(a,b)
100000 loops, best of 3: 12.3 µs per loop
>>
>>
>> a = np.arange(1000)
>> b = np.arange(1000)
>>
>> timeit mesh1(a,b)
10 loops, best of 3: 655 ms per loop
>> timeit mesh2(a,b)
10 loops, best of 3: 773 ms per loop
>> timeit mesh3(a,b)
10 loops, best of 3: 587 ms per loop
More information about the NumPy-Discussion
mailing list