Contents

# Course Contents

1. Introduction
2. NumPy Arrays
2.1 What’s A NumPy Array
2.2 Creating NumPy Arrays
2.3 Indexing And Modifying 1-D Arrays
2.4 Indexing And Modifying Multidimensional Arrays
2.5 Basic Math
3. Intermediate Array Stuff
3.2 newaxis
3.3 reshape
3.4 boolean indexing
3.5 nan
3.6 infinity
3.7 random
4. Common Operations
4.1 where
4.2 Math Funcs
4.3 all and any
4.4 concatenate
4.5 Stacking
4.6 Sorting
4.7 unique
5. Challenges

This video covers functions for stacking NumPy arrays, such as hstack(), vstack(), and stack().

## Code

``````import numpy as np

foo = np.array(['a', 'b'])
bar = np.array(['c', 'd'])
baz = np.array([['e', 'f']])
bingo = np.array([['g', 'h', 'i']])
bongo = np.array(
[['j', 'k'],
['l', 'm']]
)

# vstack foo and bar
np.vstack((foo, bar))

# vstack foo, bar, and baz
np.vstack((foo, bar, baz))

# vstack baz and bingo
np.vstack((baz, bingo))  # error

# hstack foo and bar
np.hstack((foo, bar))

# hstack baz and bingo
np.hstack((baz, bingo))

# hstack foo and bingo
np.hstack((foo, bingo))  # error

# hstack bingo and bongo
np.hstack((bingo, bongo))  # error

# stack foo and bar along axis 0
np.stack((foo, bar), axis = 0)

# stack foo and bar along axis 1
np.stack((foo, bar), axis = 1)

# stack foo and bar along axis 2
np.stack((foo, bar), axis = 2)  # error

# Use the shortcut axis = -1 to insert the new axis behind the last existing axis
np.stack((foo, bar), axis = -1)
``````

## Transcript

The functions hstack(), vstack(), and stack() are a lot like the concatenate() function with some subtle differences that can come in handy. vstack is equivalent to concatenation along the first axis after 1-D arrays of shape N have been reshaped to 1 by N. hstack is equivalent to concatenation along the second axis, except for 1-D arrays where it concatenates along the first axis. And stack is equivalent to concatenation along a newly specified axis.

Let’s start by taking a look at vstack, or vertical stack. The function itself takes just one argument, a sequence of arrays. In pseudocode, you could write the algorithm for vstack as…

1. for each array in the sequence:
2. if the array is 1d,
3. ``````then promote array to 2d by giving it a new front axis
``````
4. if every array in the sequence has the same shape
5. then concatenate the arrays along axis 0
6. otherwise throw an error

Let’s see some examples using these four arrays.

We can vstack foo and bar. In this case, they’re both 1d arrays with 2 elements, so they get promoted to 1x2 arrays before getting stacked row-wise into a 2x2 array.
We can also vstack foo, bar and baz. This is like the last example, except baz is already a 1x2 array. We can’t vstack baz and bingo because, baz has 2 columns and bingo has 3 columns.

Like vstack, the hstack function takes just one argument, a sequence of arrays.
In pseudocode, you could write the algorithm for hstack as…

1. if every array in the sequence is 1d
2. then concatenate arrays along axis 0
3. else, if every array has the same shape excluding axis 1
4. then concatenate arrays along axis 1
5. otherwise throw an error

So, for hstack, every array in the sequence must have the same dimensionality. You can’t stack a 1d array with a 2d array as you can with vstack.
Let’s see some examples using these five arrays.
We can hstack foo and bar because they’re both 1d arrays. In this case, they’ll be stacked along axis 0 resulting in a new 1d array.
We can also hstack baz and bingo because they’re both 2d arrays with 1 row. h-stacking them results in a new 1 by 5 array.
We can’t hstack foo and bingo because they have different dimensionalities. foo is 1d and bingo is 2d. And we can’t hstack bingo and bongo because bingo has 1 row and bongo has 2 rows.

Lastly, let’s have a look at stack(). stack() takes two arguments, a sequence of arrays to combine, and axis, which tells stack to create a new axis along which to combine the arrays.

In pseudocode, you could write the algorithm for stack as…

1. if every array is the same shape, and axis is less than or equal to the dimensionality of the arrays,
2. then for each array:
3. ``````insert new axis where specified
``````
4. concatenate arrays along the new axis
5. otherwise, throw an error

We’ll use two 1-d arrays, foo and bar, to make this clear with some examples.
We can stack foo and bar along a new axis 0. Remember the algorithm. foo and bar each get a new axis 0, promoting them to 1x2 arrays, and then they’re stacked along that new axis; so, row-wise, resulting in a new 2 by 2 array. We can also stack foo and bar along new axis 1. In this case, they each get a new back axis, promoting them to 2x1 arrays, and then they’re stacked column wise, resulting in a new 2 by 2 array.
If we tried to stack them along a new axis 2, we’d get an out of bounds error. Remember, the new axis must be in range of the arrays’ dimensionality.
You can also use the “axis = -1” shortcut to make the new axis the last axis. For foo and bar, it’d be like using axis = 1.