How do I stack vectors of different lengths in NumPy?

numpy, python

Solution

Short answer: you can't. NumPy does not support jagged arrays natively.

Long answer:

>>> a = ones((3,))
>>> b = ones((2,))
>>> c = array([a, b])
>>> c
array([[ 1.  1.  1.], [ 1.  1.]], dtype=object)

gives an array that may or may not behave as you expect. E.g. it doesn't support basic methods like `sum` or `reshape`, and you should treat this much as you'd treat the ordinary Python list `[a, b]` (iterate over it to perform operations instead of using vectorized idioms).

Several possible workarounds exist; the easiest is to coerce `a` and `b` to a common length, perhaps using masked arrays or NaN to signal that some indices are invalid in some rows. E.g. here's `b` as a masked array:

>>> ma.array(np.resize(b, a.shape[0]), mask=[False, False, True])
masked_array(data = [1.0 1.0 --],
             mask = [False False  True],
       fill_value = 1e+20)

This can be stacked with `a` as follows:

>>> ma.vstack([a, ma.array(np.resize(b, a.shape[0]), mask=[False, False, True])])
masked_array(data =
 [[1.0 1.0 1.0]
 [1.0 1.0 --]],
             mask =
 [[False False False]
 [False False  True]],
       fill_value = 1e+20)

(For some purposes, `scipy.sparse` may also be interesting.)

Problem

How do I stack column-wise `n` vectors of shape `(x,)` where x could be any number? For example, ``` from numpy import * a = ones((3,)) b = ones((2,)) c = vstack((a,b)) # <-- gives an error c = vstack((a[:,newaxis],b[:,newaxis])) #<-- also gives an error ``` `hstack` works fine but concatenates along the wrong dimension.

Original source