Array Shapes and Dimensions in ML
What you'll learn: How to understand and work with the shape, number of dimensions, and size of arrays that represent your ML data.
Why Shape Matters
In machine learning, data isn't just numbers—it's structured numbers. A dataset of 100 people with 5 features each has a fundamentally different structure than 5 people with 100 features. NumPy arrays capture this structure through shape, ndim (number of dimensions), and size.
The Three Key Properties
Shape (array.shape)
The shape is a tuple showing the length along each dimension. Think of it as the "dimensions" of a box:
- 1D array of 10 numbers: shape
(10,) - 2D array with 100 samples and 5 features: shape
(100, 5)— 100 rows, 5 columns - 3D array of 32 color images (28×28 pixels, 3 color channels): shape
(32, 28, 28, 3)
Number of Dimensions (array.ndim)
This tells you how many "axes" your data has:
- A single measurement: 0D (scalar)
- A list of measurements: 1D (vector)
- A table of data: 2D (matrix)
- A batch of tables: 3D or higher (tensor)
Size (array.size)
The total count of all elements. For shape (100, 5), size = 500.
ML Convention: Rows = Samples, Columns = Features
In ML, a 2D array with shape (n, m) typically means:
n= number of samples (data points, examples)m= number of features (measurements per sample)
Key Takeaway: Understanding shape, ndim, and size lets you correctly interpret and manipulate ML data structures—essential for feeding data into models and debugging dimension mismatches.