Explain the difference between np.vstack(), np.hstack(), and np.concatenate() when combining multiple arrays.

Quick Overview

Asks how np.vstack, np.hstack and np.concatenate differ when joining NumPy arrays. Tests whether you know which axis each one uses, how 1-D and 2-D inputs change the result shape, and when explicit axis control is the better choice.

Explain the difference between np.vstack(), np.hstack(), and np.concatenate() when combining multiple arrays.

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Explain the difference between `np.vstack()`, `np.hstack()`, and `np.concatenate()` when combining multiple arrays. ```hint Compare 1-D and 2-D inputs Run each function on two 1-D arrays of length 3 and on two 2-D arrays of shape (2, 3), and compare the shapes you get back. ``` ### What a Strong Answer Covers - That `np.concatenate` joins along a chosen existing axis, never adds a dimension, and needs the inputs to match on every other axis. - Which axis `vstack` and `hstack` join along, and how each one treats 1-D inputs differently from 2-D inputs. - Concrete result shapes for at least one 1-D case and one 2-D case. - When the wrappers are the clearer choice and when an explicit `axis` argument to `concatenate` is better. ### Follow-up Questions - How would you place two 1-D arrays side by side as the columns of a 2-D array? - What does `np.concatenate` do when you pass `axis=None`? - How does `np.stack` differ from all three of these functions? - Which axis do `vstack` and `hstack` use on 3-D arrays, and which function joins along the third axis?

Overview: Asks how np.vstack, np.hstack and np.concatenate differ when joining NumPy arrays. Tests whether you know which axis each one uses, how 1-D and 2-D inputs change the result shape, and when explicit axis control is the better choice.

PracHub logo
General
Nov 25, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

Explain the difference between np.vstack(), np.hstack(), and np.concatenate() when combining multiple arrays.

What a Strong Answer Covers Guidance

  • That np.concatenate joins along a chosen existing axis, never adds a dimension, and needs the inputs to match on every other axis.
  • Which axis vstack and hstack join along, and how each one treats 1-D inputs differently from 2-D inputs.
  • Concrete result shapes for at least one 1-D case and one 2-D case.
  • When the wrappers are the clearer choice and when an explicit axis argument to concatenate is better.

Follow-up Questions Guidance

  • How would you place two 1-D arrays side by side as the columns of a 2-D array?
  • What does np.concatenate do when you pass axis=None ?
  • How does np.stack differ from all three of these functions?
  • Which axis do vstack and hstack use on 3-D arrays, and which function joins along the third axis?