Thuta Learning
IntermediateData & Databasesintermediate

Array Attributes

Relax. We'll talk through this in plain words — no textbook voice.

To work with an array effectively, you need to know its structure. ndim, shape, size, dtype tell you how many dimensions the array has, how many rows/columns, how many elements, and what data type it holds.

python
import numpy as np

scores = np.array([[80, 75, 90], [88, 92, 85]])

print("Dimensions:", scores.ndim)
print("Shape:", scores.shape)
print("Total items:", scores.size)
print("Data type:", scores.dtype)

This array has 2 rows and 3 columns. scores.ndim tells you it's a 2-D array. scores.shape gives you (2, 3), the row/column counts. size shows there are 6 elements total.

You should see
Dimensions: 2 Shape: (2, 3) Total items: 6 Data type: int64

Info

shape is one of the most important basics to understand in NumPy. Broadcasting, reshaping, and matrix multiplication all tend to throw errors when shapes don't match up.

Easy traps

  • On different operating systems, the dtype output might show as int64 or int32. That's normal.

Exercise

When you're debugging "why isn't this calculation working," printing out shape first can save you a ton of time.

You'll know it worked when: