📖 Scenario: You are working with temperature data collected from sensors in a greenhouse. The data is stored as a list of daily temperature readings. You want to use NumPy arrays to handle this data efficiently.
🎯 Goal: Build a simple program that creates a NumPy array from a list of temperatures, sets a threshold temperature, filters the array to find days warmer than the threshold, and prints those warmer temperatures.
📋 What You'll Learn
Create a NumPy array with exact temperature values
Create a threshold variable with a specific value
Use NumPy array filtering to select temperatures above the threshold
Print the filtered warmer temperatures
💡 Why This Matters
🌍 Real World
Scientists and engineers often use NumPy arrays to efficiently handle and analyze sensor data like temperatures.
💼 Career
Data analysts and data scientists use array filtering to quickly find important data points and make decisions based on thresholds.
Progress0 / 4 steps
1
Create a NumPy array of temperatures
Import the numpy library as np. Then create a NumPy array called temps with these exact temperature values: 22.5, 25.0, 19.8, 23.1, 26.7, 21.0.
SciPy
Hint
Use np.array([...]) to create the array with the exact values.
2
Set a temperature threshold
Create a variable called threshold and set it to the value 23.0.
SciPy
Hint
Just assign the number 23.0 to the variable threshold.
3
Filter temperatures above the threshold
Create a new NumPy array called warm_days that contains only the temperatures from temps that are greater than threshold. Use NumPy array filtering.
SciPy
Hint
Use the condition temps > threshold inside square brackets to filter.
4
Print the warmer temperatures
Print the warm_days array to display the temperatures warmer than the threshold.
SciPy
Hint
Use print(warm_days) to show the filtered temperatures.
Practice
(1/5)
1. What is a key advantage of using a NumPy array over a regular Python list for storing numbers?
easy
A. NumPy arrays automatically sort the data
B. NumPy arrays can store different data types in the same array
C. NumPy arrays use less memory and allow faster math operations
D. NumPy arrays are slower but more flexible than lists
Solution
Step 1: Understand memory and speed benefits
NumPy arrays store data in a compact way and use optimized code for math.
Step 2: Compare with Python lists
Python lists store references and are slower for math operations.
Final Answer:
NumPy arrays use less memory and allow faster math operations -> Option C
Quick Check:
NumPy arrays = efficient storage + fast math [OK]
Hint: NumPy arrays are built for fast math and memory efficiency [OK]
Common Mistakes:
Thinking NumPy arrays can store mixed data types like lists
Assuming NumPy arrays sort data automatically
Believing NumPy arrays are slower than lists
2. Which of the following is the correct way to create a 1-dimensional NumPy array with elements 1, 2, and 3?
easy
A. np.array(1, 2, 3)
B. np.array([1, 2, 3])
C. np.array{1, 2, 3}
D. np.array([1; 2; 3])
Solution
Step 1: Recall correct syntax for np.array
NumPy arrays are created by passing a list inside parentheses: np.array([elements]).
Step 2: Check each option
np.array([1, 2, 3]) uses correct syntax with square brackets inside parentheses. Others have syntax errors.
Final Answer:
np.array([1, 2, 3]) -> Option B
Quick Check:
np.array needs a list inside parentheses [OK]
Hint: Use square brackets inside parentheses to create arrays [OK]