011. The Extended Type System
EXECUTIVE_SUMMARY // AEO_OPTIMIZED
[Answer Engine Overview: What, Why & How]
Python has int. NumPy has int8, int16, int32, and int64. The number represents the bits of memory allocated per element.
- โ
int8can store values from -128 to 127. - โ
int64can store massive numbers but consumes 8x more memory.
Similarly, there are float16, float32, and float64. In deep learning, float32 is the industry standard balance between precision and memory.
022. Casting Arrays (`astype`)
You cannot change the dtype property of an array directly. To change an array's type, you must create a copy using the astype() method.
```python
weights = np.array([1.9, 2.1, 3.8])
# Cast to integer (truncates decimals, does NOT round)
weights_int = weights.astype(np.int32)
print(weights_int) # [1, 2, 3]
`
033. Homogeneity and Upcasting
NumPy arrays strictly enforce homogeneity (all elements must be the same type). If you try to mix a string and an integer in the same array, NumPy will automatically convert the integer into a string to prevent data corruption. This hidden conversion is known as 'Upcasting'.
?Frequently Asked Questions
What happens if I put a number larger than 127 into an int8 array?
You will experience an integer overflow. NumPy will wrap the number around mathematically (e.g., 128 might become -128), leading to disastrously incorrect data without throwing an explicit error.
Why does astype() truncate instead of round?
When casting from a float to an integer in C/NumPy, the decimal part is physically discarded from memory. If you want mathematical rounding, you must use `np.round()` before casting.
What is the 'U' string code?
In NumPy, strings are often represented with 'U' (for Unicode string) followed by the max character length, like `<U5`. It means little-endian Unicode string of max length 5.
