0
0
NumPydata~10 mins

Float types (float16, float32, float64) in NumPy - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Float types (float16, float32, float64)
Start: Choose float type
Create numpy array with chosen float type
Store numbers in array
Check memory size per element
Use array for calculations
Observe precision and memory trade-off
End
This flow shows how to create numpy arrays with different float types, check their memory size, and understand precision differences.
Execution Sample
NumPy
import numpy as np
arr16 = np.array([1.5, 2.5], dtype=np.float16)
arr32 = np.array([1.5, 2.5], dtype=np.float32)
arr64 = np.array([1.5, 2.5], dtype=np.float64)
print(arr16.itemsize, arr32.itemsize, arr64.itemsize)
Creates three numpy arrays with float16, float32, and float64 types and prints their memory size per element.
Execution Table
StepActionArray CreatedData TypeItem Size (bytes)Output
1Create array with float16[1.5, 2.5]float162None
2Create array with float32[1.5, 2.5]float324None
3Create array with float64[1.5, 2.5]float648None
4Print item sizesN/AN/AN/A2 4 8
5EndN/AN/AN/AExecution complete
💡 All arrays created and item sizes printed, showing memory usage differences.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
arr16undefined[1.5, 2.5] (float16)[1.5, 2.5] (float16)[1.5, 2.5] (float16)[1.5, 2.5] (float16)
arr32undefinedundefined[1.5, 2.5] (float32)[1.5, 2.5] (float32)[1.5, 2.5] (float32)
arr64undefinedundefinedundefined[1.5, 2.5] (float64)[1.5, 2.5] (float64)
Key Moments - 2 Insights
Why does float16 use less memory than float64?
Because float16 uses 2 bytes per number while float64 uses 8 bytes, as shown in execution_table step 4 where item sizes are printed.
Does using float16 affect the precision of numbers?
Yes, float16 has less precision than float64, meaning it can store fewer decimal places accurately. This is implied by the different data types in execution_table steps 1-3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table at step 4, what is the item size in bytes for float32?
A2
B4
C8
D16
💡 Hint
Check the 'Item Size (bytes)' column in execution_table row for step 4.
At which step is the float64 array created?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Look at the 'Action' and 'Data Type' columns in execution_table.
If you want to save memory but keep some decimal precision, which float type would you choose based on the execution?
Afloat64
Bfloat16
Cfloat32
Dint32
💡 Hint
Consider the item sizes and typical precision trade-offs shown in execution_table and key_moments.
Concept Snapshot
Float types in numpy:
- float16 uses 2 bytes, less precision
- float32 uses 4 bytes, balanced precision
- float64 uses 8 bytes, highest precision
Choose based on memory vs precision needs
Use dtype parameter to set type
Full Transcript
This lesson shows how to create numpy arrays with different float types: float16, float32, and float64. Each type uses different memory sizes per number: 2, 4, and 8 bytes respectively. We create arrays with each type and print their item sizes to see the difference. Float16 uses less memory but has less precision, while float64 uses more memory but stores numbers more precisely. This helps decide which float type to use depending on your needs for memory and accuracy.