0
0
NumPydata~10 mins

Type promotion in operations in NumPy - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Type promotion in operations
Start with arrays of different types
Perform operation (e.g., addition)
Check types of operands
Apply type promotion rules
Result array has promoted type
Output result with promoted type
When numpy arrays of different types are used in operations, numpy promotes their types to a common type that can hold all values safely before computing the result.
Execution Sample
NumPy
import numpy as np
arr1 = np.array([1, 2, 3], dtype=np.int32)
arr2 = np.array([1.5, 2.5, 3.5], dtype=np.float64)
result = arr1 + arr2
print(result)
print(result.dtype)
Adds an int32 array and a float64 array, showing how numpy promotes types to float64 for the result.
Execution Table
StepActionOperand 1 TypeOperand 2 TypePromoted TypeResult ValueResult Type
1Create arr1int32--[1 2 3]int32
2Create arr2-float64-[1.5 2.5 3.5]float64
3Add arr1 + arr2int32float64float64[2.5 4.5 6.5]float64
4Print result---[2.5 4.5 6.5]float64
5Print result dtype---float64dtype('float64')
💡 Operation completes with result promoted to float64 because float64 can hold int32 and float values safely.
Variable Tracker
VariableStartAfter OperationFinal
arr1[1 2 3] (int32)[1 2 3] (int32)[1 2 3] (int32)
arr2[1.5 2.5 3.5] (float64)[1.5 2.5 3.5] (float64)[1.5 2.5 3.5] (float64)
resultN/A[2.5 4.5 6.5] (float64)[2.5 4.5 6.5] (float64)
Key Moments - 2 Insights
Why is the result array's type float64 instead of int32?
Because numpy promotes types to the more general type that can hold all values safely. Here, float64 can hold both int32 and float64 values, so the result is float64 (see execution_table step 3).
Does the original int32 array change its type after the operation?
No, the original arrays keep their types unchanged. Only the result array has the promoted type (see variable_tracker for arr1 and result).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table at step 3, what is the promoted type for the addition?
Afloat64
Bint32
Cint64
Dobject
💡 Hint
Check the 'Promoted Type' column at step 3 in the execution_table.
At which step does the result array get created with the promoted type?
AStep 2
BStep 3
CStep 1
DStep 4
💡 Hint
Look for the step where the addition happens and the result type is assigned.
If arr2 was int16 instead of float64, what would be the promoted type when adding to arr1 (int32)?
Aint16
Bfloat64
Cint32
Dobject
💡 Hint
Consider numpy's type promotion rules for integer types of different sizes.
Concept Snapshot
Type promotion in numpy operations:
- When arrays of different types are combined, numpy promotes to a common type.
- The promoted type can hold all values without loss.
- Example: int32 + float64 -> float64 result.
- Original arrays keep their types unchanged.
- Result array has the promoted type.
Full Transcript
This visual execution shows how numpy promotes types when performing operations on arrays of different types. Starting with an int32 array and a float64 array, numpy adds them by promoting the int32 to float64 to avoid losing decimal information. The result array is float64 type. The original arrays remain unchanged. This ensures safe and accurate calculations.