[Numpy-discussion] np.vstack vs. np.stack

Allan Haldane allanhaldane at gmail.com
Thu Nov 9 12:58:01 EST 2017


On 11/09/2017 04:30 AM, Joe wrote:
> Hello,
> 
> I have a question and hope that you can help me.
> 
> The doc for vstack mentions that "this function continues to be
> supported for backward compatibility, but you should prefer
> np.concatenate or np.stack."
> 
> Using vstack was convenient because "the arrays must have the same shape
> along all but the first axis."
> 
> So it was possible to stack an array (3,) and (2, 3) to a (3, 3) array
> without using e.g. atleast_2d on the (3,) array.
> 
> Is there a possibility to mimic that behavior with np.concatenate or
> np.stack?
> 
> Joe

I might write this as either

    np.concatenate([a[newaxis,:], b])

(which switches a newaxis for an atleast_2d, and is also more explicit
about where the axis is added), or, as

    np.block([[a],[b]])

Both act like vstack.

Allan


More information about the NumPy-Discussion mailing list