Before you compute, print the layout. Shape mistakes are the usual cause of later ValueErrors.
Goal
Read shape, size, ndim, dtype, memory size, and a few reductions on a units grid.
Attributes
# Rows: Nairobi, Mombasa, Kisumu, Nakuru, Eldoret · cols: A, B, C
units = np.array(
[
[12, 7, 2],
[9, 6, 0],
[3, 11, 1],
[10, 8, 0],
[6, 4, 0],
],
dtype=float,
)
print("shape:", units.shape)
print("ndim:", units.ndim)
print("size:", units.size)
print("dtype:", units.dtype)
print("itemsize:", units.itemsize)
print("nbytes:", units.nbytes)size is the product of the shape: 5 × 3 = 15.
Min, max, mean
units = np.array(
[
[12, 7, 2],
[9, 6, 0],
[3, 11, 1],
[10, 8, 0],
[6, 4, 0],
],
dtype=float,
)
print("min:", np.min(units), "max:", np.max(units), "mean:", np.mean(units))
print("row 0 (Nairobi):", units[0])
print("col 1 (product B):", units[:, 1])There is no pandas info() or describe() here. Print the pieces you care about.
Peek at the corners
units = np.array(
[
[12, 7, 2],
[9, 6, 0],
[3, 11, 1],
[10, 8, 0],
[6, 4, 0],
],
dtype=float,
)
print("first two rows:")
print(units[:2])
print()
print("last row (Eldoret):", units[-1])Tip
When a later chapter fails, print .shape of every array in the expression. Broadcasting errors name the shapes that could not stretch.