📖 Scenario: Imagine you are working with a large collection of numbers representing daily temperatures for a city over several years. You want to analyze this data efficiently without using too much computer memory.
🎯 Goal: You will create a NumPy array with temperature data, set a memory limit, filter the data based on this limit, and then display the filtered results. This will help you understand why managing memory is important when working with big data.
📋 What You'll Learn
Create a NumPy array with exact temperature values
Define a memory limit variable
Filter the array to keep only temperatures below the memory limit
Print the filtered array
💡 Why This Matters
🌍 Real World
In real life, data scientists often work with large datasets. Managing memory helps computers run faster and prevents crashes.
💼 Career
Understanding memory management is important for data scientists to write efficient code that handles big data smoothly.
Progress0 / 4 steps
1
Create the temperature data array
Import NumPy as np and create a NumPy array called temperatures with these exact values: [23, 27, 31, 29, 35, 40, 22, 20].
NumPy
Hint
Use np.array() to create the array with the exact numbers given.
2
Set a memory limit for filtering
Create a variable called memory_limit and set it to 30. This will be the threshold to filter temperatures.
NumPy
Hint
Just assign the number 30 to the variable memory_limit.
3
Filter temperatures below the memory limit
Create a new NumPy array called filtered_temps that contains only the values from temperatures which are less than memory_limit using a boolean mask.
NumPy
Hint
Use temperatures < memory_limit inside the brackets to filter.
4
Display the filtered temperatures
Print the filtered_temps array to see the temperatures below the memory limit.
NumPy
Hint
Use print(filtered_temps) to show the result.
Practice
(1/5)
1. Why is memory management important when working with numpy arrays?
easy
A. It increases the size of arrays to store more data.
B. It makes the code look cleaner and easier to read.
C. It helps keep programs fast and stable by using memory efficiently.
D. It automatically fixes errors in the code.
Solution
Step 1: Understand memory use in numpy
Numpy arrays use memory to store data, and managing this memory well keeps programs efficient.
Step 2: Connect memory management to performance
Efficient memory use prevents slowdowns and crashes, making programs fast and stable.
Final Answer:
It helps keep programs fast and stable by using memory efficiently. -> Option C
Quick Check:
Memory management = fast and stable programs [OK]
Hint: Memory management keeps numpy fast and stable [OK]
nbytes returns total bytes used by array data, which is 6.
Final Answer:
6 -> Option B
Quick Check:
3 elements x 2 bytes = 6 [OK]
Hint: Multiply elements by dtype byte size for nbytes [OK]
Common Mistakes:
Confusing number of elements with bytes
Using default int size instead of dtype size
Mixing up bits and bytes
4. Find the error in this code that tries to check memory size:
import numpy as np
arr = np.array([1, 2, 3])
print(arr.bytes)
medium
A. The attribute should be nbytes, not bytes.
B. The array must be created with a dtype to use bytes.
C. The print statement should be inside a function.
D. The array cannot be printed directly.
Solution
Step 1: Identify attribute for memory size
The correct attribute to get memory size is nbytes, not bytes.
Step 2: Understand error cause
Using bytes causes an AttributeError because it does not exist on numpy arrays.
Final Answer:
The attribute should be nbytes, not bytes. -> Option A
Quick Check:
Use arr.nbytes to get memory size [OK]
Hint: Use arr.nbytes, not arr.bytes [OK]
Common Mistakes:
Using arr.bytes instead of arr.nbytes
Thinking dtype is required for nbytes
Misplacing print statement
5. You have a large numpy array of floats using float64 but want to reduce memory use without losing much precision. Which approach is best?
hard
A. Convert the array to int32 using arr.astype(np.int32).
B. Convert the array to object dtype to save memory.
C. Keep the array as float64 but slice it into smaller parts.
D. Convert the array to float32 using arr.astype(np.float32).
Solution
Step 1: Understand dtype memory sizes and precision
float64 uses 8 bytes per element; float32 uses 4 bytes but keeps decimal precision.
Step 2: Evaluate conversion options
Converting to float32 halves memory use with minimal precision loss; int32 loses decimal data; slicing doesn't reduce total memory; object dtype uses more memory.
Final Answer:
Convert the array to float32 using arr.astype(np.float32). -> Option D
Quick Check:
float32 halves memory, keeps decimals [OK]
Hint: Use float32 to save memory with decimals [OK]
Common Mistakes:
Converting to int32 and losing decimals
Thinking slicing reduces total memory
Using object dtype which increases memory
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.