[Numpy-discussion] bug in reshape ?

josef.pktd at gmail.com josef.pktd at gmail.com
Tue Nov 22 19:45:37 EST 2011


might be an old story >>> np.__version__  -> '1.5.1'

It thought for once it's easier to use reshape to add a new axis
instead of ...,None
but my results got weird (normal(0,1) sample of 2.13795875e-314)

>>> x = 1
>>> y = np.arange(3)
>>> z = np.arange(2)[:,None]
>>> np.broadcast(x,y,z)
<numpy.broadcast object at 0x04C0DCA0>
>>> np.broadcast_arrays(x,y,z)
[array([[1, 1, 1],
       [1, 1, 1]]), array([[0, 1, 2],
       [0, 1, 2]]), array([[0, 0, 0],
       [1, 1, 1]])]
>>> x1, y1, z1 = np.broadcast_arrays(x,y,z)
>>> map(np.shape, (x1, y1, z1))
[(2, 3), (2, 3), (2, 3)]

shape looks fine, let's add an extra axis with reshape

>>> x1.reshape(2,3,1)
array([[[          1],
        [          1],
        [ 1099464714]],

       [[-2147481592],
        [        184],
        [          1]]])

what's that ?

>>> (0+x1).reshape(2,3,1)
array([[[1],
        [1],
        [1]],

       [[1],
        [1],
        [1]]])

>>> (y1*1.).reshape(2,3,1)
array([[[ 0.],
        [ 1.],
        [ 2.]],

       [[ 0.],
        [ 1.],
        [ 2.]]])

>>> (y1).reshape(2,3,1)
array([[[          0],
        [          1],
        [          2]],

       [[          0],
        [ 1099447643],
        [-2147483648]]])


>>> x1, y1, z1 = np.broadcast_arrays(x,y,z)
>>> x1[...,None]
array([[[1],
        [1],
        [1]],

       [[1],
        [1],
        [1]]])

>>> x1.shape
(2, 3)
>>> x1.reshape(2,3,1)
array([[[          1],
        [          1],
        [ 1099464730]],

       [[-2147479536],
        [ -445054780],
        [ 1063686842]]])


the background story: playing broadcasting tricks for
http://projects.scipy.org/scipy/ticket/1544

Josef



More information about the NumPy-Discussion mailing list