[Numpy-discussion] Bug in dstack?

Charles R Harris charlesr.harris at gmail.com
Thu Apr 2 20:18:40 EDT 2009


On Thu, Apr 2, 2009 at 5:50 PM, <josef.pktd at gmail.com> wrote:

> On Thu, Apr 2, 2009 at 6:46 PM, Charles R Harris
> <charlesr.harris at gmail.com> wrote:
> > Note:
> >
> > In [133]: l = [[1,0,0],[1,1,0],[1,1,1]]
> >
> > In [134]: dstack(l)
> > Out[134]:
> > array([[[1, 1, 1],
> >         [0, 1, 1],
> >         [0, 0, 1]]])
> >
> > In [135]: dstack(l).shape
> > Out[135]: (1, 3, 3)
> >
> >
> > Shouldn't the shape be (3,3)? Also, for generalized ufuncs and
> broadcasting
> > I think a function that stacked along the first axis instead of the last
> > would be useful. Maybe gstack or astack?
> >
> > Chuck
> >
>
> I think dstack, vstack and hstack work exactly as advertised in the
> docs, at least that's how I interpret and use them
>
> Josef
>
> >>> l = [[1,0,0],[1,1,0],[1,1,1]]
> >>> np.dstack(l).shape
> (1, 3, 3)
> >>> np.vstack(l).shape
> (3, 3)
> >>> np.hstack(l).shape
> (9,)
> >>> np.vstack(l)
> array([[1, 0, 0],
>       [1, 1, 0],
>       [1, 1, 1]])
> >>> np.dstack(l)[:,:,0]
> array([[1, 0, 0]])
> >>> np.dstack(l)[:,:,1]
> array([[1, 1, 0]])
> >>> np.dstack(l)[:,:,2]
> array([[1, 1, 1]])
> ________


But stacking 2D arrays gives the same number of dimensions as stack 1D
vectors:

In [1]: dstack([eye(3)]*3)
Out[1]:
array([[[ 1.,  1.,  1.],
        [ 0.,  0.,  0.],
        [ 0.,  0.,  0.]],

       [[ 0.,  0.,  0.],
        [ 1.,  1.,  1.],
        [ 0.,  0.,  0.]],

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

In [2]: dstack([eye(3)]*3).shape
Out[2]: (3, 3, 3)

So dstack is turning 1D vectors iinto 1xn arrays so that

In [8]: dstack([[1,0]]*3).shape
Out[8]: (1, 2, 3)

In [9]: dstack([[[1,0]]]*3).shape
Out[9]: (1, 2, 3)

Note the different bracket counts. This messes up general applications so
that dstacking 1D vectors has to be treated differently than dstacking
arrays. You can't sum along the last axis of the stack and get a 1D vector
back, whereas for dstacked 2D arrays summing along the last axis gives back
a 2D array.

Chuck
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20090402/dbd5e81c/attachment.html>


More information about the NumPy-Discussion mailing list