Understand numpy.vstack() and numpy.hstack() with Examples – NumPy Tutorial

By | December 6, 2021

In this tutorial, we will use some examples to show you how to use numpy.vstack() and numpy.hstack() in numpy.

numpy.vstack()

numpy.vstack() is defined as:

numpy.vstack(tup)

Stack arrays in sequence vertically (row wise).

This is equivalent to concatenation along the first axis (axis = 0) after 1-D arrays of shape (N,) have been reshaped to (1,N)

It will return a at least 2-D ndarray.

For example:

import numpy as np
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])

c1 = np.concatenate([np.reshape(a,[1, -1]), np.reshape(b,[1, -1])], axis = 0)

c2= np.vstack((a, b))

print(c1)
print(c2)

In this example, the shape of a and b is (3,), we will reshape them to (1, 3), then concatenate them on first axis axis = 0.

Run this code, we can find c1 and c2 are the same.

[[1 2 3]
 [4 5 6]]
[[1 2 3]
 [4 5 6]]

Example 2:

import numpy as np
a = np.array([[1], [2], [3]])

b = np.array([[4], [5], [6]])

c1 = np.concatenate((a, b), axis = 0)

c2= np.vstack((a, b))

print(c1)
print(c2)

In this example, the shape of a and b is (3,1), we will concatenate them directly on firest axis, where axis = 0.

Run this code, we will get:

[[1]
 [2]
 [3]
 [4]
 [5]
 [6]]
[[1]
 [2]
 [3]
 [4]
 [5]
 [6]]

c1 and c2 are also the same.

Example 3: how about the shape of a and b is (2, 2, 2)?

import numpy as np
a = np.array([[[1,2],[3,4]],[[5, 6], [7, 8]]])

b = a = np.array([[[11,21],[31,41]],[[51, 61], [71, 81]]])

c1 = np.concatenate((a, b), axis = 0)

c2= np.vstack((a, b))

print(c1)
print(c2)

We also concatenate a and b on axis = 0. c1 and c2 are also the some.

[[[11 21]
  [31 41]]

 [[51 61]
  [71 81]]

 [[11 21]
  [31 41]]

 [[51 61]
  [71 81]]]
[[[11 21]
  [31 41]]

 [[51 61]
  [71 81]]

 [[11 21]
  [31 41]]

 [[51 61]
  [71 81]]]

numpy.hstack()

numpy.hstack() is defined as:

numpy.hstack(tup)

Stack arrays in sequence horizontally (column wise).

This is equivalent to concatenation along the second axis (axis = 1),  as to 1-D arrays, it will concatenate on axis = 0.

For example:

import numpy as np
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])

c1 = np.concatenate([a, b], axis = 0)

c2= np.hstack((a, b))

print(c1)
print(c2)

In this example, the shape of a and b is (1, 3), we will concatenate them on axis = 0.

Run this code, we will find c1 and c2 are the same.

[1 2 3 4 5 6]
[1 2 3 4 5 6]

Example 2:

import numpy as np
a = np.array([[1], [2], [3]])

b = np.array([[4], [5], [6]])

c1 = np.concatenate((a, b), axis = 1)

c2= np.hstack((a, b))

print(c1)
print(c2)

Here the shape of a and b are (2, 1) , they are not 1-D array, we will concatenate them on axis = 1.

Run this code, we also c1 and c2 are the same.

[[1 4]
 [2 5]
 [3 6]]
[[1 4]
 [2 5]
 [3 6]]

Example 3: how about the shape of a and b is (2, 2, 2)?

import numpy as np
a = np.array([[[1,2],[3,4]],[[5, 6], [7, 8]]])

b = a = np.array([[[11,21],[31,41]],[[51, 61], [71, 81]]])

c1 = np.concatenate((a, b), axis = 1)

c2= np.hstack((a, b))

print(c1)
print(c2)

Here the shape of a and b are (2, 2, 2), they are not 1-D array, we will concatenate them on axis = 1.

Run this code, we will find c1 and c2 are also the same.

[[[11 21]
  [31 41]
  [11 21]
  [31 41]]

 [[51 61]
  [71 81]
  [51 61]
  [71 81]]]
[[[11 21]
  [31 41]
  [11 21]
  [31 41]]

 [[51 61]
  [71 81]
  [51 61]
  [71 81]]]