Array Inspection and Attributes
Learn how to inspect NumPy arrays, understand their structure, examine data types, and retrieve important information without manually checking every element.
1. Learning Objectives
By the end of this lesson, you will be able to:
- Inspect the shape and dimensions of an array.
- Find the total number of elements in an array.
- Identify the data type and memory usage of array elements.
- Use attributes such as
ndim,shape,size, anddtype. - Use
np.info()and basic inspection techniques when needed. - Debug common array structure problems efficiently.
2. What Is Array Inspection?
Array inspection means examining the properties and structure of a NumPy array. Before performing calculations, it is often useful to know how many dimensions an array has, how its elements are arranged, and what type of data it stores.
NumPy provides attributes that return this information directly. These attributes help developers understand arrays without writing loops to count or examine individual elements.
Example: A Student Marks Array
The array contains marks for two students across three subjects.
The array has two rows, three columns, and six total elements.
3. Important NumPy Array Attributes
NumPy arrays provide several built-in attributes. These are accessed without parentheses because they are attributes rather than methods.
| Attribute | Purpose | Example Result |
|---|---|---|
ndim |
Returns the number of dimensions. | 2 |
shape |
Returns the length of the array along each dimension. | (2, 3) |
size |
Returns the total number of elements. | 6 |
dtype |
Returns the data type of the elements. | int64 or another integer type |
itemsize |
Returns the size in bytes of one array element. | 8 for an 8-byte element |
nbytes |
Returns the total bytes used by the array elements. | 48 for six 8-byte elements |
dtype
instead of assuming that every integer array uses the same type.
4. Inspecting Dimensions with ndim
The ndim attribute tells you how many dimensions an
array has. It is useful when working with data that may be one-
dimensional, two-dimensional, or higher-dimensional.
| Array | Dimensions | Explanation |
|---|---|---|
[10, 20, 30] |
1 | A single sequence of elements. |
[[10, 20], [30, 40]] |
2 | Rows and columns. |
[[[1, 2]]] |
3 | A structure with three axes. |
5. Understanding Array Shape
The shape attribute returns a tuple describing the
number of elements along each dimension. For a two-dimensional
array, the tuple is expressed as (rows, columns).
For a two-dimensional array, shape is reported as (rows, columns).
shape
with size. The shape (2, 3) describes the
array's structure, while the size 6 counts its elements.
6. Counting Elements with size
The size attribute returns the total number of elements
in an array, regardless of the number of dimensions.
(2, 3) gives 2 × 3 = 6 elements.
7. Checking Data Types with dtype
The dtype attribute identifies the type used to
represent elements in the array. NumPy uses this information to
perform calculations and manage storage.
The exact displayed type may vary by platform and NumPy version. Integer arrays commonly display an integer type, decimal arrays commonly display a floating-point type, and text arrays use a suitable string type.
Why dtype Matters
- It helps determine which operations are appropriate.
- It affects the memory required for each element.
- It helps identify unexpected conversions in calculations.
- It is useful when importing data from external files.
8. Understanding itemsize and nbytes
NumPy also provides attributes for inspecting memory usage.
itemsize reports the bytes used by one element, while
nbytes reports the total bytes occupied by the array's
element data.
In this example, each int32 element uses four bytes.
Four elements therefore use sixteen bytes for their element data.
nbytes describes the array
element buffer, not all Python object overhead or every allocation
associated with the array.
9. Using Multiple Attributes Together
In real applications, developers often inspect several attributes together to understand a dataset before processing it.
Typical output for this explicitly typed array is:
10. Practical Activity
Activity: Inspect Student Marks
Samadhan, Pradnya, and Samiksha are reviewing a marks dataset. Create a NumPy array containing three students and four subject marks for each student.
Tasks:
- Create a two-dimensional integer array with three rows and four columns.
- Display the array.
- Print its number of dimensions using
ndim. - Print its shape and total number of elements.
- Display its data type and total element bytes.
Sample Solution
Expected key results:
- Dimensions:
2 - Shape:
(3, 4) - Total elements:
12 - Data type:
int32 - Total element bytes:
48
11. Common Mistakes to Avoid
| Mistake | Correct Approach |
|---|---|
Writing array.shape() |
Use array.shape because shape is an attribute. |
| Assuming shape and size mean the same thing | Use shape for dimensions and size for element count. |
| Assuming all integer arrays use the same dtype | Inspect array.dtype or specify a dtype explicitly. |
| Assuming nbytes includes all Python memory overhead | Remember that nbytes measures the element data buffer. |
| Inspecting only the printed values | Check shape, ndim, dtype, and size when structure matters. |
Summary
Array inspection is an essential step in working with NumPy. Attributes provide quick information about an array's dimensions, structure, element count, data type, and element memory usage.
ndimreturns the number of dimensions.shapereturns the size along each dimension.sizereturns the total number of elements.dtypeidentifies the element data type.itemsizereturns bytes per element.nbytesreturns total bytes used by element data.