0
0
NumPydata~20 mins

Type promotion in operations in NumPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Type Promotion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of adding int32 and float64 arrays
What is the data type of the resulting array after adding these two NumPy arrays?
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.dtype)
Afloat64
Bint32
Cfloat32
Dobject
Attempts:
2 left
💡 Hint
When adding integers and floats, NumPy promotes to the more general type.
Predict Output
intermediate
2:00remaining
Result type of multiplying uint8 and int16 arrays
What is the data type of the resulting array after multiplying these two NumPy arrays?
NumPy
import numpy as np
arr1 = np.array([10, 20, 30], dtype=np.uint8)
arr2 = np.array([1000, 2000, 3000], dtype=np.int16)
result = arr1 * arr2
print(result.dtype)
Aint16
Bint32
Cuint8
Dfloat64
Attempts:
2 left
💡 Hint
NumPy promotes the narrower dtype to the wider one.
data_output
advanced
2:30remaining
Output array and dtype after adding complex64 and float32 arrays
What is the output array and its data type after adding these two arrays?
NumPy
import numpy as np
arr1 = np.array([1+2j, 3+4j], dtype=np.complex64)
arr2 = np.array([1.5, 2.5], dtype=np.float32)
result = arr1 + arr2
print(result)
print(result.dtype)
A[2.5+2.j 5.5+4.j] with dtype float64
B[2.5+2.j 5.5+4.j] with dtype complex128
C[2.5 5.5] with dtype float32
D[2.5+2.j 5.5+4.j] with dtype complex64
Attempts:
2 left
💡 Hint
Adding float32 to complex64 promotes float32 to complex64.
Predict Output
advanced
2:00remaining
Data type after subtracting int64 and uint64 arrays
What is the data type of the resulting array after subtracting these two arrays?
NumPy
import numpy as np
arr1 = np.array([10, 20, 30], dtype=np.int64)
arr2 = np.array([1, 2, 3], dtype=np.uint64)
result = arr1 - arr2
print(result.dtype)
Afloat64
Bint64
Cuint64
Dobject
Attempts:
2 left
💡 Hint
Subtracting signed and unsigned 64-bit integers promotes to float64 to avoid overflow.
visualization
expert
3:00remaining
Visualizing type promotion in mixed dtype operations
Which option shows the correct plot of the data types resulting from adding arrays of these dtypes: int8, uint16, float32, complex128?
NumPy
import numpy as np
import matplotlib.pyplot as plt

dtypes = [np.int8, np.uint16, np.float32, np.complex128]
results = []
for i, dt1 in enumerate(dtypes):
    for j, dt2 in enumerate(dtypes):
        a = np.array([1], dtype=dt1)
        b = np.array([1], dtype=dt2)
        c = a + b
        results.append((dt1.__name__, dt2.__name__, c.dtype.name))

# Prepare data for heatmap
import pandas as pd
pivot_data = pd.DataFrame(results, columns=['Type1', 'Type2', 'Result'])
pivot_table = pivot_data.pivot('Type1', 'Type2', 'Result')

plt.figure(figsize=(8,6))
plt.title('Resulting dtype from adding two dtypes')
plt.imshow(pivot_table.applymap(lambda x: {'int8':0,'uint16':1,'float32':2,'complex128':3}[x]), cmap='viridis')
plt.xticks(range(len(dtypes)), [dt.__name__ for dt in dtypes])
plt.yticks(range(len(dtypes)), [dt.__name__ for dt in dtypes])
plt.colorbar(ticks=[0,1,2,3], label='dtype code')
plt.show()
AA heatmap where adding int8 and uint16 results in int8, and adding float32 and complex128 results in float32
BA heatmap where adding int8 and uint16 results in complex128, and adding float32 and complex128 results in uint16
CA heatmap where adding int8 and uint16 results in uint16, and adding float32 and complex128 results in complex128
DA heatmap where all additions result in float64 regardless of input types
Attempts:
2 left
💡 Hint
NumPy promotes to the smallest dtype that can hold both inputs without loss.