0
0
NumPydata~5 mins

np.vstack() and np.hstack() in NumPy - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: np.vstack() and np.hstack()
O(n)
Understanding Time Complexity

We want to understand how the time needed to join arrays grows as the arrays get bigger.

How does stacking arrays vertically or horizontally affect the work done by numpy?

Scenario Under Consideration

Analyze the time complexity of the following code snippet.

import numpy as np

arr1 = np.ones((1000, 10))
arr2 = np.zeros((1000, 10))

vstacked = np.vstack((arr1, arr2))
hstacked = np.hstack((arr1, arr2))

This code stacks two arrays: one on top of the other (vertical) and side by side (horizontal).

Identify Repeating Operations

Identify the loops, recursion, array traversals that repeat.

  • Primary operation: Copying all elements from input arrays into a new larger array.
  • How many times: Once for each element in the input arrays.
How Execution Grows With Input

As the size of the input arrays grows, the time to copy all elements grows proportionally.

Input Size (rows x cols)Approx. Operations
10 x 10~200 (copying elements twice)
100 x 10~2,000
1000 x 10~20,000

Pattern observation: The operations grow roughly in direct proportion to the total number of elements copied.

Final Time Complexity

Time Complexity: O(n)

This means the time to stack arrays grows linearly with the total number of elements involved.

Common Mistake

[X] Wrong: "Stacking arrays is instant or very fast regardless of size because it just links arrays together."

[OK] Correct: Actually, numpy creates a new array and copies all elements, so the time depends on how many elements are copied.

Interview Connect

Knowing how array operations scale helps you write efficient data processing code and explain your choices clearly in interviews.

Self-Check

What if we stacked three or more arrays instead of two? How would the time complexity change?