Time for action – stacking arrays
First, let's set up some arrays:
In: a = arange(9).reshape(3,3) In: a Out: array([[0, 1, 2], [3, 4, 5], [6, 7, 8]]) In: b = 2 * a In: b Out: array([[ 0, 2, 4], [ 6, 8, 10], [12, 14, 16]])
Horizontal stacking: Starting with horizontal stacking, we will form a tuple of
ndarrays
and give it to thehstack
function. This is shown as follows:In: hstack((a, b)) Out: array([[ 0, 1, 2, 0, 2, 4], [ 3, 4, 5, 6, 8, 10], [ 6, 7, 8, 12, 14, 16]])
We can achieve the same with the
concatenate
function, which is shown as follows:In: concatenate((a, b), axis=1) Out: array([[ 0, 1, 2, 0, 2, 4], [ 3, 4, 5, 6, 8, 10], [ 6, 7, 8, 12, 14, 16]])
Vertical stacking: With vertical stacking, again, a tuple is formed. This time, it is given to the
vstack
function. This can be seen as follows:In: vstack((a, b)) Out: array([[ 0, 1, 2], [ 3, 4, 5], [ 6, 7, 8], [ 0, 2, 4], ...