Dtype
What kind of number an array holds, such as whole numbers (int64) or decimals (float32). It sets precision and memory use.
What it means
dtype is short for data type. It says what kind of value an array holds, such as whole numbers (int64) or decimals (float32). A NumPy array has one dtype for all its values, which is what makes it so much faster and smaller than a Python list. The number in the name is how many bits each value uses.
An example with NumPy
import numpy as np print(np.array([1, 2, 3], dtype=np.int64).dtype) print(np.array([1, 2.5, 3]).dtype) # one decimal makes every value a float print(np.array([1, 2, 3], dtype=np.int64).astype(np.float32).dtype) # The dtype sets the memory, and a million numbers shows the difference. for kind in [np.float64, np.float32, np.float16]: print(f"{kind.__name__:<8} {np.zeros(1_000_000, dtype=kind).nbytes / 1e6:.0f} MB") # And the range: 8-bit whole numbers stop at 255, then wrap around. print(np.array([250], dtype=np.uint8) + np.array([10], dtype=np.uint8))
int64 float64 float32 float64 8 MB float32 4 MB float16 2 MB [4]
One decimal turns the whole array into floats. A million float32 values take half the memory of float64, and machine learning uses that trade all the time. The last line is the trap: uint8 only reaches 255, so 250 + 10 silently wraps around to 4, with no error.
In pandas and PyTorch
In pandas, df.dtypes shows the type of each column, and df["col"].astype("float32") changes one. A number column showing object usually means that something in it is text, which is worth checking before you do any math. PyTorch uses float32 by default, and many language models run in smaller types to fit in memory.
Try it now
Units sold (tee, hoodie, mug) on two days, times each product's price. Every cell is combined with the cell in the same position. No loop needed.
Learn it properly
NumPy arrays · Do math on whole columns at once, the foundation of every ML library. About 30 minutes.
Other terms from the same lesson
- array: A grid of numbers (a row, a table, or more) that NumPy can do math on all at once.
- broadcasting: NumPy and PyTorch stretching a smaller array to match a bigger one, so you can add a single number or a row to a whole table at once.
- NumPy: A Python library for fast math on whole arrays of numbers at once.
Never trained an AI?
Teach a small one to write, right in your browser. It takes about 20 seconds and needs no account.