Using np.take() and np.put() for Advanced Selection in NumPy
📖 Scenario: Imagine you have a list of daily temperatures recorded over a week. You want to select specific days to analyze and then update some of those days with corrected temperature values.
🎯 Goal: You will create a NumPy array of temperatures, select specific days using np.take(), update some temperatures using np.put(), and then display the updated temperatures.
📋 What You'll Learn
Create a NumPy array called temps with exact values: [22, 24, 19, 23, 25, 20, 21]
Create an index array called selected_days with exact values: [1, 3, 4]
Use np.take() with temps and selected_days to select temperatures for those days
Use np.put() to update the temperature on day 3 (index 3) to 26 in the original temps array
Print the updated temps array
💡 Why This Matters
🌍 Real World
Selecting and updating specific data points in arrays is common in data cleaning and analysis, such as correcting sensor readings or focusing on key days in time series data.
💼 Career
Data scientists often need to manipulate arrays efficiently. Knowing how to select and update data using NumPy functions like <code>np.take()</code> and <code>np.put()</code> helps in writing clean and fast data processing code.
Progress0 / 4 steps
1
Create the temperatures array
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 values.
2
Create the index array for selected days
Create a NumPy array called selected_days with these exact values: [1, 3, 4]
NumPy
Hint
Use np.array() to create the index array with the exact values.
3
Select temperatures for the chosen days using np.take()
Use np.take() with temps and selected_days to create a new array called selected_temps that contains the temperatures for those days
NumPy
Hint
Use np.take(temps, selected_days) to get the temperatures for the selected days.
4
Update a temperature using np.put() and print the result
Use np.put() to update the temperature at index 3 in temps to 26. Then print the updated temps array.
NumPy
Hint
Use np.put(temps, 3, 26) to update the value, then print(temps) to show the updated array.
Practice
(1/5)
1. What does np.take() do in NumPy?
easy
A. Selects elements from an array using specified indices
B. Changes elements in an array at specified indices
C. Sorts the elements of an array
D. Creates a new array filled with zeros
Solution
Step 1: Understand the function purpose
np.take() is used to pick elements from an array by their index positions.
Step 2: Compare with other options
Only Selects elements from an array using specified indices describes selecting elements by indices, while others describe different functions.
Final Answer:
Selects elements from an array using specified indices -> Option A
Quick Check:
np.take() = select elements [OK]
Hint: np.take() picks by index, np.put() changes by index [OK]
Common Mistakes:
Confusing np.take() with np.put()
Thinking np.take() modifies the array
Mixing np.take() with sorting functions
2. Which of the following is the correct syntax to replace elements at indices [1, 3] in array a with values [10, 20] using np.put()?
easy
A. np.put(a, [1, 3], [10, 20])
B. np.put([1, 3], a, [10, 20])
C. np.put(a, [10, 20], [1, 3])
D. np.put(a, 10, [1, 3, 20])
Solution
Step 1: Recall np.put() syntax
The correct syntax is np.put(array, indices, values) to replace elements at given indices.
Hint: np.put(array, indices, values) replaces elements [OK]
Common Mistakes:
Swapping the order of arguments
Passing values as indices
Using wrong data types for indices or values
3. What is the output of the following code?
import numpy as np
a = np.array([5, 10, 15, 20, 25])
indices = [0, 2, 4]
result = np.take(a, indices)
print(result)
medium
A. [10 20 25]
B. [5 15 25]
C. [5 10 15]
D. [0 2 4]
Solution
Step 1: Understand np.take() with given indices
np.take(a, [0, 2, 4]) selects elements at positions 0, 2, and 4 from array a.
Step 2: Extract elements from array
Elements at these positions are 5 (index 0), 15 (index 2), and 25 (index 4).
Final Answer:
[5 15 25] -> Option B
Quick Check:
np.take() picks elements by index [OK]
Hint: np.take() returns elements at given indices [OK]
Common Mistakes:
Confusing indices with values
Expecting output as original array
Mixing up index positions
4. Identify the error in this code snippet:
import numpy as np
a = np.array([1, 2, 3, 4])
np.put(a, [1, 4], [10, 20])
print(a)
medium
A. Syntax error in np.put() usage
B. Values list length does not match indices length
C. Index 4 is out of bounds for array of size 4
D. np.put() cannot modify arrays in-place
Solution
Step 1: Check array size and indices
Array a has size 4, valid indices are 0 to 3. Index 4 is invalid.
Step 2: Understand np.put() behavior
np.put() tries to assign values at given indices. Using index 4 causes an IndexError.
Final Answer:
Index 4 is out of bounds for array of size 4 -> Option C
Quick Check:
Index error due to invalid index [OK]
Hint: Check indices are within array size before np.put() [OK]
Common Mistakes:
Ignoring array size limits
Assuming np.put() auto-expands array
Confusing index and value positions
5. Given a = np.array([1, 2, 3, 4, 5]), how can you use np.put() and np.take() together to replace elements at indices [0, 2] with [10, 20] and then retrieve these updated elements?
hard
A. np.take(a, [10, 20]); np.put(a, [0, 2])
B. np.take(a, [0, 2], [10, 20]); np.put(a, [0, 2])
C. np.put([0, 2], a, [10, 20]); np.take([0, 2], a)
D. np.put(a, [0, 2], [10, 20]); np.take(a, [0, 2])
Solution
Step 1: Use np.put() to update elements
np.put(a, [0, 2], [10, 20]) replaces elements at indices 0 and 2 with 10 and 20.
Step 2: Use np.take() to retrieve updated elements
np.take(a, [0, 2]) returns the updated elements at those indices.