0
0
NumPydata~15 mins

np.take() and np.put() for advanced selection in NumPy - Mini Project: Build & Apply

Choose your learning style9 modes available
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
Need a 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
Need a 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
Need a 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
Need a hint?

Use np.put(temps, 3, 26) to update the value, then print(temps) to show the updated array.