'How to stack a 2D array on a 3D array in Numpy

import numpy as np
a = np.zeros((3, 4))
b = np.zeros((3, 4))
ab = np.stack((a, b), axis=0)

I want to stack b in the axis=0 direction of ab.

abb = np.stack((ab, b), axis=0)  # ERROR
abb = np.vstack((ab, b))  # also ERROR
abb = np.vstack((ab, b[np.newaxis, ...]))  # This is OK, but it seems redundant

What is the best way to do this?



Solution 1:[1]

As @Vishesh Mangla suggested:

b=b.reshape(1,3,4)
abb = np.vstack((ab, b))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Kilian