[Numpy-discussion] choose() broadcasting

Roman Bertle bertle at smoerz.org
Fri Oct 17 09:58:56 EDT 2008


* Travis E. Oliphant <oliphant at enthought.com> [081003 22:20]:
> Roman Bertle wrote:
> > Hello,
> >
> > I have found something I call a bug in the numpy choose() method and
> > wanted to report it in trac.
> >   
> Thanks for your report.  I'm not sure why you are having trouble with 
> Trac, but I've created a ticket for this problem.

Hello,

trac works for me know. And thank you for fixing the bug, the svn numpy
version works now for me. But there remains an issue I want to report.
choose is much slower in numpy than in numarray, and even more if an
output array is specified, as these tests show:

import timeit

setups = {
    'numarray': """
import numarray as N

n1, n2 = 4, 1000000
a1 = N.arange(n1*n2, type='Float64', shape=(n1,n2))
a2 = -N.arange(n1*n2, type='Float64', shape=(n1,n2))
a3 = -N.arange(n1*n2, type='Float64', shape=(n1,n2))
b1 = N.arange(n2, type='Float64')
b2 = -N.arange(n2, type='Float64')
b3 = -N.arange(n2, type='Float64')
c = N.remainder(N.arange(n2, type='Int32'),2)
""",
    'numpy': """
import numpy as N

n1, n2 = 4, 1000000
a1 = N.arange(n1*n2, dtype='Float64').reshape((n1,n2))
a2 = -N.arange(n1*n2, dtype='Float64').reshape((n1,n2))
a3 = -N.arange(n1*n2, dtype='Float64').reshape((n1,n2))
b1 = N.arange(n2, dtype='Float64')
b2 = -N.arange(n2, dtype='Float64')
b3 = -N.arange(n2, dtype='Float64')
c = N.remainder(N.arange(n2, dtype='Int32'),2)
"""
    }

stmta = "N.choose(c, (a1, a2))"
stmtao = "N.choose(c, (a1, a2), a3)"
stmtb = "N.choose(c, (b1, b2))"
stmtbo = "N.choose(c, (b1, b2), b3)"

timeit.Timer(setup=setups['numarray'], stmt=stmta).repeat(3,100)
[3.3187780380249023, 3.2966721057891846, 3.3234250545501709]

timeit.Timer(setup=setups['numpy'], stmt=stmta).repeat(3,100)
[14.842453002929688, 14.833296060562134, 14.836632966995239]

timeit.Timer(setup=setups['numarray'], stmt=stmtao).repeat(3,100)
[3.1973719596862793, 3.2031948566436768, 3.2093629837036133]

timeit.Timer(setup=setups['numpy'], stmt=stmtao).repeat(3,100)
[17.546916007995605, 17.548220157623291, 17.536314010620117]

timeit.Timer(setup=setups['numarray'], stmt=stmtb).repeat(3,100)
[0.6694338321685791, 0.66939401626586914, 0.67307686805725098]

timeit.Timer(setup=setups['numpy'], stmt=stmtb).repeat(3,100)
[3.7615809440612793, 3.7627589702606201, 3.7547731399536133]

timeit.Timer(setup=setups['numarray'], stmt=stmtbo).repeat(3,100)
[0.67037606239318848, 0.67186903953552246, 0.66994881629943848]

timeit.Timer(setup=setups['numpy'], stmt=stmtbo).repeat(3,100)
[4.4750981330871582, 4.4650890827178955, 4.4679431915283203]


Best Regards,

Roman



More information about the NumPy-Discussion mailing list