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


Asked By – Stefan Sullivan

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?

Now we will see solution for issue: When should I use hstack/vstack vs append vs concatenate vs column_stack?


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:

concatenate([atleast_2d(_m) for _m in tup], 0)
i.e. turn all inputs in to 2d (or more) and concatenate on first

concatenate([atleast_1d(_m) for _m in tup], axis=<0 or 1>)

transform arrays with (if needed)
    array(arr, copy=False, subok=True, ndmin=2).T

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.

This question is answered By – hpaulj

This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0