Array shapes and dimensions in Data Analysis Python - Time & Space Complexity
When working with arrays, knowing how their size and shape affect processing time is important.
We want to understand how the time to handle arrays grows as their dimensions change.
Analyze the time complexity of the following code snippet.
import numpy as np
def sum_all_elements(arr):
total = 0
for i in range(arr.shape[0]):
for j in range(arr.shape[1]):
total += arr[i, j]
return total
# Example usage:
# arr = np.array([[1, 2], [3, 4]])
# sum_all_elements(arr)
This code sums all elements in a 2D array by visiting each element one by one.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Nested loops visiting each element in the 2D array.
- How many times: Outer loop runs for number of rows, inner loop runs for number of columns, so total visits equal rows x columns.
As the array grows in rows and columns, the total operations grow by multiplying these dimensions.
| Input Size (rows x columns) | Approx. Operations |
|---|---|
| 10 x 10 | 100 |
| 100 x 100 | 10,000 |
| 1000 x 1000 | 1,000,000 |
Pattern observation: Doubling rows and columns roughly quadruples the operations because total elements multiply.
Time Complexity: O(n * m)
This means the time grows proportionally to the total number of elements in the array, which is rows times columns.
[X] Wrong: "The time depends only on the number of rows or columns, not both."
[OK] Correct: Because the code visits every element, both rows and columns matter. Ignoring one dimension underestimates the work done.
Understanding how array dimensions affect processing time helps you explain efficiency clearly in interviews and shows you can reason about data size impact.
"What if we changed the array to 3 dimensions? How would the time complexity change?"
Practice
Solution
Step 1: Understand the meaning of shape
The shape of an array is a tuple that shows how many elements are in each dimension.Step 2: Differentiate shape from other properties
Shape is not the total count, data type, or memory size, but the size per dimension.Final Answer:
The number of elements along each dimension -> Option AQuick Check:
Shape = elements per dimension [OK]
- Confusing shape with total number of elements
- Mixing shape with data type
- Thinking shape is memory size
arr?Solution
Step 1: Recall NumPy shape syntax
In NumPy, shape is an attribute, not a method, so no parentheses are used.Step 2: Check each option
arr.shape usesarr.shapecorrectly; others use incorrect syntax or functions.Final Answer:
arr.shape -> Option AQuick Check:
Use attribute, no parentheses [OK]
- Adding parentheses after shape
- Using non-existent functions like get_shape()
- Trying to call shape as a function
import numpy as np arr = np.array([[1, 2, 3], [4, 5, 6]]) print(arr.shape)
What is the output?
Solution
Step 1: Analyze the array structure
The array has 2 rows and 3 columns, so shape is (2, 3).Step 2: Confirm shape output
Printingarr.shapereturns the tuple (2, 3).Final Answer:
(2, 3) -> Option CQuick Check:
Rows=2, Columns=3 -> (2, 3) [OK]
- Swapping rows and columns in shape
- Thinking shape is total elements
- Confusing shape with array size
import numpy as np arr = np.array([1, 2, 3]) print(arr.shape())
Solution
Step 1: Identify the error source
The code callsarr.shape()with parentheses, but shape is an attribute.Step 2: Understand attribute vs method
Attributes do not use parentheses; calling with () causes a TypeError.Final Answer:
shape is an attribute, not a method; parentheses cause error -> Option DQuick Check:
Attributes no parentheses [OK]
- Using parentheses with shape
- Assuming array is empty causes error
- Misreading print syntax
Solution
Step 1: Calculate total elements
Original shape (4, 3, 2) means 4*3*2 = 24 elements total.Step 2: Determine new shape dimensions
New shape (4, 6) means 4*6 = 24 elements, matching total elements.Step 3: Verify other options
arr.reshape(12, 2), arr.reshape(6, 4), and arr.reshape(2, 12) do not produce 4 rows and 6 columns.Final Answer:
arr.reshape(4, 6) -> Option BQuick Check:
4*6=24 matches total elements [OK]
- Mixing up rows and columns in reshape
- Choosing shapes with wrong total elements
- Forgetting reshape must keep total size
Ready to prove it?
Pass the test and this pattern moves unknown
