Concatenating & Stacking Arrays (concatenate, vstack, hstack)
Stacking Arrays: np.vstack(), np.hstack(), & np.concatenate()
Combining multiple datasets, merging feature matrices with target labels, and assembling image tiles are fundamental tasks in numerical data pipelines.
NumPy provides specialized functions to combine arrays along existing or new axes:
np.concatenate(): The generalized foundational routine.np.vstack(): Vertical stacking (row-wise / along axis 0).np.hstack(): Horizontal stacking (column-wise / along axis 1).
1. General Array Concatenation: np.concatenate()
np.concatenate((a1, a2, ...), axis=0) joins a sequence of arrays along an existing axis. All input arrays must have the exact same shape, except in the dimension corresponding to axis:
Output:
2. Vertical Stacking: np.vstack()
np.vstack() stacks arrays vertically (row on top of row). For 2D arrays, this is equivalent to np.concatenate(..., axis=0).
Crucially, for 1D arrays, np.vstack() treats them as rows of shape (1, N) and stacks them into a 2D matrix:
Output:
3. Horizontal Stacking: np.hstack()
np.hstack() stacks arrays horizontally (column next to column).
- For 2D arrays, it stacks along columns (equivalent to
np.concatenate(..., axis=1)). - For 1D arrays, it concatenates them end-to-end into a longer 1D array:
4. Dimensional Alignment Rules and Common Pitfalls
When concatenating or stacking arrays, shape mismatches along non-concatenating axes raise ValueError:
Multiple Choice Questions
1. What is the required argument format for arrays passed to np.concatenate()?
A. Individual positional arguments: np.concatenate(a, b, c) B. A tuple or list sequence: np.concatenate((a, b, c)) C. A dictionary of arrays D. A single string specifying the variable names Answer: B Explanation: np.concatenate expects a sequence (tuple or list) of arrays as its first parameter, such as np.concatenate((a, b), axis=0).
2. What is the resulting shape when executing np.vstack((np.array([1, 2, 3]), np.array([4, 5, 6])))?
A. (6,) B. (2, 3) C. (3, 2) D. (1, 6) Answer: B Explanation: np.vstack promotes 1D arrays of shape (N,) to 2D row arrays of shape (1, N) and concatenates them vertically, resulting in shape (2, 3).
3. What is the resulting shape when executing np.hstack((np.array([1, 2, 3]), np.array([4, 5, 6])))?
A. (6,) B. (2, 3) C. (1, 6) D. (3, 2) Answer: A Explanation: For 1D arrays, np.hstack performs simple end-to-end concatenation along the single existing axis, yielding a 1D array of shape (6,).
4. Given 2D arrays A of shape (3, 4) and B of shape (3, 2), which function will successfully combine them?
A. np.vstack((A, B)) B. np.hstack((A, B)) C. np.concatenate((A, B), axis=0) D. None; their shapes are incompatible Answer: B Explanation: To stack horizontally along axis 1, the row dimensions (axis 0) must match. Both A and B have 3 rows, so np.hstack produces a combined array of shape (3, 6).
5. How does np.concatenate((a, b), axis=None) behave?
A. It raises a ValueError B. It flattens all input arrays before concatenating them into a 1D array C. It stacks them along a newly created dimension D. It performs matrix multiplication Answer: B Explanation: When axis=None, np.concatenate() automatically flattens all input arrays and concatenates them into a single 1D array.
Depth & Column Stacking (dstack, column_stack, stack)
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.