📖 Scenario: Imagine you have a list of daily temperatures for a week. You want to split this list into smaller parts to analyze each part separately.
🎯 Goal: You will create a NumPy array of temperatures, set a split index, use np.split() to divide the array, and then print the split parts.
📋 What You'll Learn
Create a NumPy array called temps with exact values: [22, 24, 19, 23, 25, 20, 21]
Create a variable called split_index with the value 3
Use np.split() with temps and [split_index] to split the array into two parts
Store the result in a variable called split_temps
Print the variable split_temps
💡 Why This Matters
🌍 Real World
Splitting data arrays helps analyze different sections separately, like dividing weekly sales data into weekdays and weekends.
💼 Career
Data scientists often split datasets to prepare for analysis, testing, or training machine learning models.
Progress0 / 4 steps
1
Create the NumPy array of temperatures
Import NumPy as np and create a NumPy array called temps with these exact values: [22, 24, 19, 23, 25, 20, 21]
NumPy
Hint
Use np.array() to create the array with the exact list inside.
2
Set the split index
Create a variable called split_index and set it to the integer 3
NumPy
Hint
Just assign the number 3 to the variable split_index.
3
Split the array using np.split()
Use np.split() with the array temps and the list [split_index] to split the array into two parts. Store the result in a variable called split_temps
NumPy
Hint
Call np.split() with the array and a list containing split_index.
4
Print the split arrays
Print the variable split_temps to see the two parts of the array after splitting
NumPy
Hint
Use print(split_temps) to display the result.
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
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 B
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
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.
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
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 A
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
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 D
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 unknownstrongwhile earning +1 Grip toward your certificate.