Array Shape, Size and Dimensions
Learn how to inspect the structure of NumPy arrays using shape, size, and ndim. Understand rows, columns, dimensions, and the relationship between an array's structure and its total elements.
1. Learning Objectives
By the end of this lesson, you will be able to:
- Understand the structure of a NumPy array.
- Inspect array dimensions using the ndim attribute.
- Use shape to identify the length of each dimension.
- Use size to count the total number of elements.
- Distinguish between shape, size, and dimensions.
- Inspect one-dimensional, two-dimensional, and three-dimensional arrays.
- Calculate the total number of elements from an array's shape.
- Use array attributes to validate datasets.
- Recognize common shape-related errors.
- Apply array inspection to practical data analysis.
shape tells you the dimensions' lengths,
size counts all elements, and
ndim tells you the number of dimensions.
2. Understanding Array Shape
The shape of a NumPy array describes how many elements exist along each dimension. NumPy returns the shape as a tuple.
For a one-dimensional array, the shape contains one number. For a two-dimensional array, it contains the number of rows followed by the number of columns.
Example: Inspecting Array Shape
The one-dimensional array has five elements. The two-dimensional array contains two rows and three columns.
(2, 3) means two rows and three columns.
3. Understanding Array Dimensions with ndim
The ndim attribute returns the number of dimensions
in a NumPy array. It does not return the number of elements or
the length of a particular dimension.
Common Array Dimensions
| Array Type | Example Shape | ndim |
|---|---|---|
| One-dimensional array | (3,) |
1 |
| Two-dimensional array | (2, 3) |
2 |
| Three-dimensional array | (2, 2, 2) |
3 |
ndim when
you need to verify whether an array is one-dimensional,
two-dimensional, or higher-dimensional before processing it.
4. Understanding Array Size
The size attribute returns the total number of
elements in an array, regardless of how many dimensions it has.
The two-dimensional array has two rows and three columns. Therefore, its total number of elements is six.
Calculating Size from Shape
For an array with shape (2, 3), the total
number of elements is calculated by multiplying the
lengths of the dimensions.
For higher-dimensional arrays, multiply the lengths of all dimensions to calculate the total number of elements.
size
with ndim. An array can contain many elements
while having only one dimension.
5. Difference Between Shape, Size, and Dimensions
These three attributes describe different properties of the same array. Understanding the difference helps prevent mistakes while working with datasets.
| Attribute | Meaning | Example Result |
|---|---|---|
shape |
Length of each dimension. | (2, 3) |
size |
Total number of elements. | 6 |
ndim |
Number of dimensions. | 2 |
Output
6. Inspecting Three-Dimensional Arrays
A three-dimensional array contains elements organized across three dimensions. It can be understood as a collection of two-dimensional arrays.
(2, 2, 3)
means there are two groups, each containing two rows and
three columns.
7. Empty Arrays and Single-Element Arrays
NumPy can represent arrays containing no elements, arrays with one element, and arrays with multiple dimensions. Their shapes and sizes help explain their structures.
| Array | Shape | Size | ndim |
|---|---|---|---|
np.array([]) |
(0,) |
0 | 1 |
np.array([25]) |
(1,) |
1 | 1 |
np.array([[25]]) |
(1, 1) |
1 | 2 |
8. Using Shape to Validate Data
Before combining or processing arrays, it is useful to inspect their shapes. This helps identify missing rows, unexpected columns, and incompatible structures.
Example Output
rows, columns = array.shape works for a
two-dimensional array because its shape contains two values.
For arrays with other numbers of dimensions, inspect the shape
before unpacking it.
9. Shape and Size in Practical Calculations
Array attributes are useful for checking whether a dataset contains the expected number of values and for understanding how its elements are arranged.
Example: Monthly Sales Data
Samiksha is analyzing monthly sales for four products across three months. She wants to inspect the dataset before calculating totals.
Expected Output
The shape indicates four products and three monthly values per product. The size confirms that the array contains twelve values in total.
10. Common Shape and Dimension Errors
| Problem | Reason | Solution |
|---|---|---|
| Confusing shape and size | Shape describes each dimension, while size counts elements. | Inspect both attributes separately. |
| Unexpected number of dimensions | The input data may be nested differently than expected. | Check ndim and shape. |
| Incorrect shape unpacking | The array has more or fewer dimensions than expected. | Check the shape before unpacking it. |
| Incorrect number of elements | The dataset may contain missing or additional values. | Inspect size and the input data. |
| Unexpected matrix structure | Rows may contain different numbers of values in the source data. | Validate the input structure before creating the array. |
11. Practical Activity: Student Dataset Inspection
Activity: Inspect Student Marks
Pradnya is checking a marks dataset before performing calculations. Use NumPy attributes to determine the number of students, the number of subjects, and the total number of recorded marks.
Expected Output
This activity demonstrates how shape, size, and dimensions help verify the structure of a dataset before analysis.
12. Practice Exercises
Use the following arrays to complete the exercises:
- Print the shape of the numbers array.
- Print the size of the marks array.
- Print the number of dimensions of the marks array.
- Find the shape and size of the three-dimensional array.
- Calculate the total elements in an array with shape
(3, 4). - Explain the difference between
shapeandndim. - Print the number of rows and columns in the marks array.
- Create an array containing one element and inspect its attributes.
Challenge Exercise
Samadhan is preparing a sales dataset for five products across four months. Create a two-dimensional NumPy array and complete the following tasks:
- Display the array's shape, size, and dimensions.
- Calculate the number of products and monthly values.
- Verify that the array contains twenty elements.
- Calculate the total sales for each month.
- Explain how shape helps verify the dataset structure.
Summary
In this lesson, we learned how NumPy describes array structure
using the shape, size, and
ndim attributes.
We inspected one-dimensional, two-dimensional, and
three-dimensional arrays. We also learned how shape represents
the length of each dimension and how size gives the total
number of elements.
Finally, we used array attributes to validate datasets, inspect student marks, analyze sales records, and identify common shape-related errors. These attributes are essential when preparing, validating, reshaping, and analyzing NumPy arrays.