Introduction to NumPy and Matplotlib
Chapter 4: Array reshaping
Reshape an array into 3D
Well done! Here is your third mission.
Mission 3: I love 3D!
Can you reshape the (3 \times 4) np.array
into a (2 \times 3 \times 2) np.array
instead please?
This mission is just an extension to the previous one!
x = np.array([[0, 1, 2, 3],
[4, 5, 6, 7],
[-6, -5, -4, -3]
])
reshaped_x = ????
assert reshaped_x == np.array([[[ 0, 1],
[ 2, 3],
[ 4, 5]],
[[ 6, 7],
[-6, -5],
[-4, -3]]])
Reshape can be used for different dimensions. This is just like the previous mission. All the variants from the previous mission applies!
reshaped_x = x.reshape((2, 3, 2))
# You can also let NumPy infer the remaining dimension automatically
reshaped_x = x.reshape((-1, 3, 2))
reshaped_x = x.reshape((2, -1, 2))
reshaped_x = x.reshape((2, 3, -1))