0
0
NumPydata~20 mins

np.split() for dividing arrays in NumPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
np.split Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of np.split() with equal splits
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, 3)
print(splits)
NumPy
import numpy as np
arr = np.array([10, 20, 30, 40, 50, 60])
splits = np.split(arr, 3)
print(splits)
A[array([10, 20, 30, 40]), array([50, 60])]
B[array([10, 20, 30]), array([40, 50, 60])]
C[array([10]), array([20, 30, 40]), array([50, 60])]
D[array([10, 20]), array([30, 40]), array([50, 60])]
Attempts:
2 left
💡 Hint
np.split divides the array into equal parts if possible.
Predict Output
intermediate
2:00remaining
Output of np.split() with indices_or_sections as list
What is the output of this code?
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6, 7])
splits = np.split(arr, [2, 5])
print(splits)
NumPy
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6, 7])
splits = np.split(arr, [2, 5])
print(splits)
A[array([1, 2]), array([3, 4, 5]), array([6, 7])]
B[array([1, 2, 3]), array([4, 5]), array([6, 7])]
C[array([1, 2]), array([3, 4]), array([5, 6, 7])]
D[array([1]), array([2, 3, 4]), array([5, 6, 7])]
Attempts:
2 left
💡 Hint
The list [2, 5] means split before index 2 and before index 5.
data_output
advanced
1:30remaining
Number of sub-arrays after splitting
How many sub-arrays are created by this code?
import numpy as np
arr = np.arange(12)
splits = np.split(arr, [3, 7, 10])
print(len(splits))
NumPy
import numpy as np
arr = np.arange(12)
splits = np.split(arr, [3, 7, 10])
print(len(splits))
A4
B3
C5
D6
Attempts:
2 left
💡 Hint
Number of sub-arrays is one more than the number of split indices.
🔧 Debug
advanced
1:30remaining
Error raised by np.split with invalid split
What error does this code raise?
import numpy as np
arr = np.array([1, 2, 3, 4])
splits = np.split(arr, 3)
NumPy
import numpy as np
arr = np.array([1, 2, 3, 4])
splits = np.split(arr, 3)
AIndexError: index out of bounds
BValueError: array split does not result in an equal division
CTypeError: indices_or_sections must be an integer or 1-D array
DNo error, splits into 3 parts
Attempts:
2 left
💡 Hint
The array length is not divisible by 3.
🚀 Application
expert
2:30remaining
Using np.split to divide a 2D array by columns
Given a 2D array with shape (4, 6), which option correctly splits it into three arrays each with 2 columns?
import numpy as np
arr = np.arange(24).reshape(4, 6)
splits = np.split(arr, ?, axis=1)
print([s.shape for s in splits])
NumPy
import numpy as np
arr = np.arange(24).reshape(4, 6)
splits = np.split(arr, ?, axis=1)
print([s.shape for s in splits])
A[0, 2, 4]
B3
C[2, 4]
D[1, 3, 5]
Attempts:
2 left
💡 Hint
Split indices mark column positions where to split.