0
0
NumPydata~10 mins

np.split() for dividing arrays in NumPy - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - np.split() for dividing arrays
Start with array
Choose split indices or sections
Call np.split(array, indices_or_sections)
Array is divided into sub-arrays
Return list of sub-arrays
np.split() takes an array and splits it into multiple smaller arrays at specified indices or into equal sections.
Execution Sample
NumPy
import numpy as np
arr = np.array([10, 20, 30, 40, 50, 60])
splits = np.split(arr, [2, 4])
print(splits)
This code splits the array at indices 2 and 4, creating three sub-arrays.
Execution Table
StepInput ArraySplit IndicesActionOutput Sub-arrays
1[10 20 30 40 50 60][2, 4]Split at index 2 and 4[[10 20], [30 40], [50 60]]
2[10 20 30 40 50 60][3]Split at index 3[[10 20 30], [40 50 60]]
3[10 20 30 40 50 60]3Split into 3 equal parts[[10 20], [30 40], [50 60]]
4[10 20 30 40 50 60][1, 3, 5]Split at indices 1, 3, 5[[10], [20 30], [40 50], [60]]
5[10 20 30 40 50 60][7]Error: index 7 out of boundsRaises ValueError
💡 Execution stops after all splits or error raised for invalid indices.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5
arr[10 20 30 40 50 60][10 20 30 40 50 60][10 20 30 40 50 60][10 20 30 40 50 60][10 20 30 40 50 60][10 20 30 40 50 60]
splitsNone[[10 20], [30 40], [50 60]][[10 20 30], [40 50 60]][[10 20], [30 40], [50 60]][[10], [20 30], [40 50], [60]]Error
Key Moments - 3 Insights
Why does np.split(arr, 3) split the array into 3 equal parts instead of splitting at index 3?
When the second argument is an integer, np.split divides the array into that many equal parts. To split at specific indices, you must provide a list or array of indices, as shown in step 1.
What happens if a split index is out of the array bounds?
As in step 5, np.split raises a ValueError if any split index is outside the valid range of the array indices.
How does np.split handle multiple split indices?
np.split divides the array at each index in the list, creating sub-arrays between these indices, as shown in steps 1 and 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output sub-arrays when splitting at indices [2, 4]?
A[[10 20 30], [40 50 60]]
B[[10 20], [30 40], [50 60]]
C[[10], [20 30], [40 50], [60]]
DError
💡 Hint
Check row 1 in the execution_table under 'Output Sub-arrays'.
At which step does np.split raise an error due to invalid index?
AStep 3
BStep 2
CStep 5
DStep 4
💡 Hint
Look for the row mentioning 'Error' in the execution_table.
If you want to split an array into 3 equal parts, which argument should you pass to np.split?
A3
B[3]
C[1, 2, 3]
D[2, 4]
💡 Hint
See step 3 in the execution_table where the argument is an integer.
Concept Snapshot
np.split(array, indices_or_sections)
- Splits array into sub-arrays.
- indices_or_sections: list of split points or integer for equal parts.
- Returns list of arrays.
- Raises error if indices out of bounds.
- Use list for specific splits, int for equal parts.
Full Transcript
np.split() is a function in numpy that divides an array into smaller arrays. You can split by giving a list of indices where the array should be cut, or by giving an integer to split into equal parts. For example, np.split(arr, [2,4]) splits arr at positions 2 and 4, making three parts. If you give an integer like 3, it splits the array into 3 equal parts. If you give an index outside the array range, it raises an error. The output is always a list of arrays. This helps when you want to break data into chunks for analysis or processing.