📖 Scenario: You are analyzing temperature data collected from different sensors. Each sensor records temperatures with different precision levels. You want to store these temperatures using different float types to understand how precision affects the data.
🎯 Goal: Create NumPy arrays with float16, float32, and float64 types and observe their differences.
📋 What You'll Learn
Create a NumPy array with float16 type
Create a NumPy array with float32 type
Create a NumPy array with float64 type
Print the arrays and their data types
💡 Why This Matters
🌍 Real World
Sensors and devices often record measurements with different precision levels. Choosing the right float type helps balance memory use and accuracy.
💼 Career
Data scientists and engineers must understand data types to optimize performance and storage when working with large datasets.
Progress0 / 4 steps
1
Create a NumPy array with float16 type
Import NumPy as np. Create a NumPy array called temps_float16 with values [36.6, 37.1, 36.8] and set its data type to np.float16.
NumPy
Hint
Use np.array() with the dtype parameter set to np.float16.
2
Create a NumPy array with float32 type
Create a NumPy array called temps_float32 with values [36.6, 37.1, 36.8] and set its data type to np.float32.
NumPy
Hint
Use np.array() with the dtype parameter set to np.float32.
3
Create a NumPy array with float64 type
Create a NumPy array called temps_float64 with values [36.6, 37.1, 36.8] and set its data type to np.float64.
NumPy
Hint
Use np.array() with the dtype parameter set to np.float64.
4
Print the arrays and their data types
Print the arrays temps_float16, temps_float32, and temps_float64. Then print their data types using the .dtype attribute.
NumPy
Hint
Use print() to display each array and then print its .dtype.
Practice
(1/5)
1. Which numpy float type uses the least amount of memory?
Using dtype='float16' is valid and creates a float16 array.
Step 2: Check astype conversion and print
astype('float32') converts array to float32 correctly. The print statement syntax is correct.
Final Answer:
No error; code runs and prints float32 -> Option B
Quick Check:
astype converts float16 to float32 fine [OK]
Hint: astype converts float types smoothly [OK]
Common Mistakes:
Thinking astype can't convert float16 to float32
Believing dtype must be np.float32 object
Assuming print syntax error
5. You have a large dataset of decimal numbers but limited memory. You want to store the data with the smallest float type without losing too much precision. Which float type should you choose?
hard
A. float64 for maximum precision
B. int32 since decimals are not needed
C. float32 as a balance of precision and memory
D. float16 for smallest memory regardless of precision
Solution
Step 1: Understand memory vs precision tradeoff
float16 uses least memory but has low precision, which may lose important decimal details.
Step 2: Choose float type balancing memory and precision
float32 uses moderate memory and keeps good precision, suitable for large datasets with decimals.
Final Answer:
float32 as a balance of precision and memory -> Option C
Quick Check:
Balance memory and precision with float32 [OK]
Hint: Pick float32 for good precision and moderate memory [OK]
Common Mistakes:
Choosing float16 losing too much precision
Always picking float64 ignoring memory
Using int32 when decimals matter
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.