np.split() for dividing arrays in NumPy - Time & Space Complexity
We want to understand how the time needed to split an array changes as the array gets bigger.
Specifically, how does dividing an array into parts affect the work done?
Analyze the time complexity of the following code snippet.
import numpy as np
arr = np.arange(1000)
splits = np.split(arr, 10)
This code creates an array of 1000 numbers and splits it into 10 equal parts.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Copying parts of the original array into new sub-arrays.
- How many times: The array is split into 10 parts, so this copying happens 10 times.
As the array size grows, the total amount of data copied grows proportionally.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 copies of 1 element each = 10 operations |
| 100 | About 10 copies of 10 elements each = 100 operations |
| 1000 | About 10 copies of 100 elements each = 1000 operations |
Pattern observation: The total work grows roughly in direct proportion to the size of the input array.
Time Complexity: O(n)
This means the time to split the array grows linearly with the size of the array.
[X] Wrong: "Splitting an array into parts is very fast and does not depend on the array size."
[OK] Correct: Actually, splitting involves copying data into new arrays, so the bigger the array, the more copying is needed, which takes more time.
Understanding how array operations scale helps you explain your code choices clearly and shows you know how to handle data efficiently.
"What if we used views instead of copies when splitting? How would the time complexity change?"
Practice
np.split() function do in NumPy?Solution
Step 1: Understand the purpose of np.split()
The function is designed to break a large array into smaller arrays either by specifying split points or by dividing into equal parts.Step 2: Compare with other array functions
Unlike merging, sorting, or reshaping, np.split specifically divides arrays.Final Answer:
It divides an array into multiple smaller arrays at specified indices or into equal parts. -> Option BQuick Check:
np.split() = divides array [OK]
- Confusing split with merge
- Thinking it sorts arrays
- Mixing with reshape
arr into 3 equal parts using np.split()?Solution
Step 1: Recall np.split() syntax
The function signature is np.split(array, indices_or_sections), where indices_or_sections can be an integer for equal parts.Step 2: Identify correct usage
Passing 3 as the second argument splits the array into 3 equal parts. Keyword arguments like 'indices_or_sections' are not used explicitly in common syntax.Final Answer:
np.split(arr, 3) -> Option AQuick Check:
np.split(arr, 3) splits into 3 parts [OK]
- Using list instead of integer for equal parts
- Wrong keyword arguments
- Confusing indices with number of parts
import numpy as np arr = np.array([10, 20, 30, 40, 50, 60]) splits = np.split(arr, [2, 4]) print(splits)
Solution
Step 1: Understand split indices
The list [2, 4] means split at index 2 and then at index 4.Step 2: Divide the array accordingly
arr[:2] = [10, 20], arr[2:4] = [30, 40], arr[4:] = [50, 60]Final Answer:
[array([10, 20]), array([30, 40]), array([50, 60])] -> Option CQuick Check:
Split at 2 and 4 gives 3 parts [OK]
- Misunderstanding split points
- Off-by-one errors in slicing
- Confusing output format
import numpy as np arr = np.array([1, 2, 3, 4, 5]) splits = np.split(arr, 3) print(splits)
Solution
Step 1: Check array length and split parts
The array length is 5, but we want to split into 3 equal parts.Step 2: Understand np.split() behavior
np.split() requires the array length to be divisible by the number of parts when splitting equally, otherwise it raises an error.Final Answer:
The array length is not divisible by 3, so equal splits are impossible. -> Option AQuick Check:
Length 5 not divisible by 3 causes error [OK]
- Using integer splits on incompatible length
- Confusing np.split with np.array_split
- Assuming print error
data with shape (6, 4). You want to split it into 3 equal parts along the first axis (rows) for separate analysis. Which code correctly does this using np.split()?Solution
Step 1: Understand array shape and axis
data has 6 rows and 4 columns. Splitting into 3 equal parts along rows means each part has 2 rows.Step 2: Use np.split() with correct axis and parts
np.split(data, 3, axis=0) splits rows into 3 equal parts. axis=1 would split columns, which is not desired.Final Answer:
np.split(data, 3, axis=0) -> Option DQuick Check:
Split rows into 3 parts with axis=0 [OK]
- Splitting along wrong axis
- Using indices instead of number for equal parts
- Ignoring array shape
Ready to prove it?
Pass the test and this pattern moves unknown
