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?
The difference between stacking and concatenating tensors can be described in a single sentence, so here goes. Concatenating joins a sequence of tensors along an existing axis, and stacking joins a sequence of tensors along a new axis.
HStack - which arranges its children(i.e. subviews) in a horizontal line, next to each other. VStack - which arranges its children in a vertical line, i.e above and below each other.
append() and np. concatenate(). The append method will add an item to the end of an array and the Concatenation function will allow us to add two arrays together. In concatenate function the input can be any dimension while in the append function all input must be of the same dimension.
VSTACK returns the array formed by appending each of the array arguments in a row-wise fashion. The resulting array will be the following dimensions: Rows: the combined count of all the rows from each of the array arguments. Columns: The maximum of the column count from each of the array arguments.
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
andblock
provide more general stacking and concatenation operations.
np.block
is also new. It, in effect, recursively concatenates along the nested lists.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With