0
0
NumPydata~10 mins

np.vstack() and np.hstack() in NumPy - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - np.vstack() and np.hstack()
Start with arrays
Choose stacking method
np.vstack()
Output combined array
We start with arrays, choose vertical or horizontal stacking, then combine arrays accordingly to get the output.
Execution Sample
NumPy
import numpy as np
arr1 = np.array([1, 2])
arr2 = np.array([3, 4])
v_stack = np.vstack((arr1, arr2))
h_stack = np.hstack((arr1, arr2))
This code stacks two 1D arrays vertically and horizontally using np.vstack() and np.hstack().
Execution Table
StepActionInput ArraysStacking MethodOutput Array
1Define arr1[1, 2]N/AN/A
2Define arr2[3, 4]N/AN/A
3Apply np.vstack[[1, 2], [3, 4]]Vertical stack[[1 2] [3 4]]
4Apply np.hstack[1, 2], [3, 4]Horizontal stack[1 2 3 4]
5EndN/AN/AN/A
💡 All arrays stacked; process complete.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
arr1undefined[1 2][1 2][1 2][1 2][1 2]
arr2undefinedundefined[3 4][3 4][3 4][3 4]
v_stackundefinedundefinedundefined[[1 2] [3 4]][[1 2] [3 4]][[1 2] [3 4]]
h_stackundefinedundefinedundefinedundefined[1 2 3 4][1 2 3 4]
Key Moments - 2 Insights
Why does np.vstack() output a 2D array even if input arrays are 1D?
np.vstack() stacks arrays vertically by adding a new row dimension, so 1D arrays become rows in a 2D array, as shown in step 3 of the execution_table.
Why does np.hstack() output a 1D array when stacking two 1D arrays?
np.hstack() concatenates arrays along the horizontal axis, so two 1D arrays join end-to-end into a longer 1D array, as seen in step 4 of the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3. What is the shape of the output array from np.vstack()?
A(2, 2)
B(4,)
C(1, 4)
D(2,)
💡 Hint
Check the output array shape in the Output Array column at step 3.
At which step does np.hstack() produce the array [1 2 3 4]?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the Output Array column for the horizontal stack result.
If arr1 was a 2D array [[1, 2]], how would np.vstack((arr1, arr2)) change?
AIt would raise an error due to shape mismatch
BIt would stack arr2 as a new row below arr1
CIt would stack arr2 as a new column beside arr1
DIt would flatten both arrays before stacking
💡 Hint
Consider how np.vstack stacks arrays vertically and the shape compatibility.
Concept Snapshot
np.vstack() stacks arrays vertically (adds rows).
np.hstack() stacks arrays horizontally (adds columns).
Input arrays must have compatible shapes.
1D arrays become 2D in vstack, stay 1D in hstack.
Use for combining arrays along different axes.
Full Transcript
We start with two arrays arr1 and arr2. np.vstack stacks them vertically, making a 2D array with arr1 and arr2 as rows. np.hstack stacks them horizontally, joining them end-to-end in one 1D array. The execution table shows each step: defining arrays, applying vstack and hstack, and the resulting arrays. Variable tracker shows how variables change after each step. Key moments clarify why vstack outputs 2D arrays and hstack outputs 1D arrays for 1D inputs. The visual quiz tests understanding of output shapes and stacking behavior. The concept snapshot summarizes usage and behavior of np.vstack and np.hstack.