Bird
Raised Fist0
NumPydata~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does the np.split() function do in NumPy?
easy
A. It merges multiple arrays into one large array.
B. It divides an array into multiple smaller arrays at specified indices or into equal parts.
C. It sorts the elements of an array in ascending order.
D. It reshapes an array into a different shape without changing data.

Solution

  1. 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.
  2. Step 2: Compare with other array functions

    Unlike merging, sorting, or reshaping, np.split specifically divides arrays.
  3. Final Answer:

    It divides an array into multiple smaller arrays at specified indices or into equal parts. -> Option B
  4. Quick Check:

    np.split() = divides array [OK]
Hint: Remember: split means break into parts [OK]
Common Mistakes:
  • Confusing split with merge
  • Thinking it sorts arrays
  • Mixing with reshape
2. Which of the following is the correct syntax to split a 1D NumPy array arr into 3 equal parts using np.split()?
easy
A. np.split(arr, 3)
B. np.split(arr, [3])
C. np.split(arr, indices=3)
D. np.split(arr, sections=3)

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    np.split(arr, 3) -> Option A
  4. Quick Check:

    np.split(arr, 3) splits into 3 parts [OK]
Hint: Use integer directly for equal parts [OK]
Common Mistakes:
  • Using list instead of integer for equal parts
  • Wrong keyword arguments
  • Confusing indices with number of parts
3. What is the output of the following code?
import numpy as np
arr = np.array([10, 20, 30, 40, 50, 60])
splits = np.split(arr, [2, 4])
print(splits)
medium
A. [array([10]), array([20, 30, 40]), array([50, 60])]
B. [array([10, 20, 30]), array([40, 50, 60])]
C. [array([10, 20]), array([30, 40]), array([50, 60])]
D. [array([10, 20]), array([30]), array([40, 50, 60])]

Solution

  1. Step 1: Understand split indices

    The list [2, 4] means split at index 2 and then at index 4.
  2. Step 2: Divide the array accordingly

    arr[:2] = [10, 20], arr[2:4] = [30, 40], arr[4:] = [50, 60]
  3. Final Answer:

    [array([10, 20]), array([30, 40]), array([50, 60])] -> Option C
  4. Quick Check:

    Split at 2 and 4 gives 3 parts [OK]
Hint: Split indices mark where to cut array [OK]
Common Mistakes:
  • Misunderstanding split points
  • Off-by-one errors in slicing
  • Confusing output format
4. The following code throws an error. What is the cause?
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
splits = np.split(arr, 3)
print(splits)
medium
A. The array length is not divisible by 3, so equal splits are impossible.
B. np.split() requires a list of indices, not an integer.
C. The array must be 2D to use np.split().
D. The print statement syntax is incorrect.

Solution

  1. Step 1: Check array length and split parts

    The array length is 5, but we want to split into 3 equal parts.
  2. 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.
  3. Final Answer:

    The array length is not divisible by 3, so equal splits are impossible. -> Option A
  4. Quick Check:

    Length 5 not divisible by 3 causes error [OK]
Hint: Check divisibility of array length by parts [OK]
Common Mistakes:
  • Using integer splits on incompatible length
  • Confusing np.split with np.array_split
  • Assuming print error
5. You have a 2D NumPy array 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()?
hard
A. np.split(data, [1, 3, 5], axis=1)
B. np.split(data, 3, axis=1)
C. np.split(data, [2, 4], axis=1)
D. np.split(data, 3, axis=0)

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    np.split(data, 3, axis=0) -> Option D
  4. Quick Check:

    Split rows into 3 parts with axis=0 [OK]
Hint: Use axis=0 to split rows, axis=1 for columns [OK]
Common Mistakes:
  • Splitting along wrong axis
  • Using indices instead of number for equal parts
  • Ignoring array shape
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard