Module: M1-R5: Information Technology Tools and Network Basics
Chapter: Ch1 Computer Intro
NumPy arrays have several built-in attributes that provide important information about the array's structure, data type, shape, and memory usage.
| Attribute | Description | Example |
|---|---|---|
ndarray.shape | Returns the dimensions of the array (rows, columns) | arr.shape |
ndarray.size | Total number of elements in the array | arr.size |
ndarray.ndim | Number of array dimensions | arr.ndim |
ndarray.dtype | Data type of elements | arr.dtype |
ndarray.itemsize | Memory size of each element in bytes | arr.itemsize |
ndarray.nbytes | Total memory used by the array in bytes | arr.nbytes |
ndarray.T | Transpose of the array | arr.T |
ndarray.real / ndarray.imag | Real and imaginary part of complex arrays | arr.real, arr.imag |
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])
print("Shape:", arr.shape)
print("Size:", arr.size)
print("Dimensions:", arr.ndim)
print("Data Type:", arr.dtype)
print("Item Size:", arr.itemsize)
print("Total Bytes:", arr.nbytes)
print("Transpose:\n", arr.T)
Output:
Shape: (2, 3)
Size: 6
Dimensions: 2
Data Type: int64
Item Size: 8
Total Bytes: 48
Transpose:
[[1 4]
[2 5]
[3 6]]