Array Attributes in NumPy
NumPy arrays come with a variety of attributes that provide useful information about the array’s structure, dimensions, data type, and more. These attributes make it easier to analyze and manipulate arrays effectively.
Summary of Array Attributes
Attribute | Description |
---|---|
ndim | Number of dimensions (axes) |
shape | Shape of the array (dimensions of each axis) |
size | Total number of elements |
dtype | Data type of the elements |
itemsize | Size (in bytes) of each element |
nbytes | Total number of bytes consumed |
T | Transpose of the array |
flat | Iterator over all elements |
real | Real part of the array (if complex numbers are present) |
imag | Imaginary part of the array (if complex numbers are present) |
base | Indicates if the array is a view or a copy |
Key Array Attributes
1. ndim
: Number of Dimensions
The ndim
attribute returns the number of dimensions (axes) of the array.
import numpy as np
array = np.array([[1, 2, 3], [4, 5, 6]])
print("Number of dimensions:", array.ndim)
Output:
Number of dimensions: 2
2. shape
: Shape of the Array
The shape
attribute provides the dimensions of the array as a tuple (rows, columns, etc.).
print("Shape of the array:", array.shape)
Output:
Shape of the array: (2, 3)
3. size
: Total Number of Elements
The size
attribute returns the total number of elements in the array.
print("Total number of elements:", array.size)
Output:
Total number of elements: 6
4. dtype
: Data Type of Elements
The dtype
attribute shows the data type of the elements in the array.
print("Data type of elements:", array.dtype)
Output:
Data type of elements: int64
5. itemsize
: Size of Each Element in Bytes
The itemsize
attribute returns the size (in bytes) of each element in the array.
print("Size of each element in bytes:", array.itemsize)
Output:
Size of each element in bytes: 8
6. nbytes
: Total Bytes Consumed by the Array
The nbytes
attribute returns the total number of bytes consumed by the array.
print("Total bytes consumed:", array.nbytes)
Output:
Total bytes consumed: 48
7. T
: Transpose of the Array
The T
attribute returns the transpose of the array (rows become columns and vice versa).
print("Transpose of the array:\n", array.T)
Output:
Transpose of the array:
[[1 4]
[2 5]
[3 6]]
8. flat
: Iterator Over Array Elements
The flat
attribute provides an iterator to iterate through all the elements of the array.
for element in array.flat:
print(element, end=" ")
Output:
1 2 3 4 5 6
9. real
and imag
: Real and Imaginary Parts
If the array contains complex numbers, the real
and imag
attributes return the real and imaginary parts, respectively.
complex_array = np.array([1 + 2j, 3 + 4j])
print("Real part:", complex_array.real)
print("Imaginary part:", complex_array.imag)
Output:
Real part: [1. 3.]
Imaginary part: [2. 4.]
10. base
: View or Copy Indicator
The base
attribute indicates if the array is a view or a copy. If the array is a view, base
points to the original array; otherwise, it returns None
.
view_array = array.view()
copy_array = array.copy()
print("View base:", view_array.base)
print("Copy base:", copy_array.base)
Output:
View base: [[1 2 3]
[4 5 6]]
Copy base: None
Try It Yourself
Problem 1: Array Analysis
Create a 2D array of shape (3, 4) with random integers and print its attributes (shape
, size
, dtype
, and nbytes
).
Show Code
import numpy as np
array = np.random.randint(1, 100, (3, 4))
print("Array:\n", array)
print("Shape:", array.shape)
print("Size:", array.size)
print("Data type:", array.dtype)
print("Total bytes:", array.nbytes)
Problem 2: Transpose and Iterate
Create a 2D array of shape (2, 3). Print its transpose and iterate through all its elements.
Show Code
import numpy as np
array = np.array([[10, 20, 30], [40, 50, 60]])
print("Transpose:\n", array.T)
print("Elements:")
for element in array.flat:
print(element, end=" ")