Search code examples
pythonnumpy

When should I use hstack/vstack vs append vs concatenate vs column_stack?


Simple question: what is the advantage of each of these methods. It seems that given the right parameters (and ndarray shapes) they all work seemingly equivalently. Do some work in place? Have better performance? Which functions should I use when?


Solution

  • All the functions are written in Python except np.concatenate. With an IPython shell you just use ??.

    If not, here's a summary of their code:

    vstack
    concatenate([atleast_2d(_m) for _m in tup], 0)
    i.e. turn all inputs in to 2d (or more) and concatenate on first
    
    hstack
    concatenate([atleast_1d(_m) for _m in tup], axis=<0 or 1>)
    
    colstack
    transform arrays with (if needed)
        array(arr, copy=False, subok=True, ndmin=2).T
    
    append
    concatenate((asarray(arr), values), axis=axis)
    

    In other words, they all work by tweaking the dimensions of the input arrays, and then concatenating on the right axis. They are just convenience functions.


    And newer np.stack:

    arrays = [asanyarray(arr) for arr in arrays]
    shapes = set(arr.shape for arr in arrays)
    result_ndim = arrays[0].ndim + 1
    axis = normalize_axis_index(axis, result_ndim)
    sl = (slice(None),) * axis + (_nx.newaxis,)
    
    expanded_arrays = [arr[sl] for arr in arrays]
    concatenate(expanded_arrays, axis=axis, out=out)
    

    That is, it expands the dims of all inputs (a bit like np.expand_dims), and then concatenates. With axis=0, the effect is the same as np.array.

    hstack documentation now adds:

    The functions concatenate, stack and block provide more general stacking and concatenation operations.

    np.block is also new. It, in effect, recursively concatenates along the nested lists.