What is Computer

Module: M4-R5: Internet of Things (IoT)

Chapter: Ch1 Computer Intro

🔹 Introduction to NumPy Array Attributes

NumPy arrays have several built-in attributes that provide important information about the array's structure, data type, shape, and memory usage.

🔹 Common Array Attributes
Attribute Description Example
ndarray.shapeReturns the dimensions of the array (rows, columns)arr.shape
ndarray.sizeTotal number of elements in the arrayarr.size
ndarray.ndimNumber of array dimensionsarr.ndim
ndarray.dtypeData type of elementsarr.dtype
ndarray.itemsizeMemory size of each element in bytesarr.itemsize
ndarray.nbytesTotal memory used by the array in bytesarr.nbytes
ndarray.TTranspose of the arrayarr.T
ndarray.real / ndarray.imagReal and imaginary part of complex arraysarr.real, arr.imag
🔹 Example: Accessing Array Attributes
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]]
✅ Summary
  • Array attributes provide essential information about the array structure, data type, and memory usage.
  • Common attributes: shape, size, ndim, dtype, itemsize, nbytes, T.
  • Use attributes to check or debug arrays efficiently.
Quick Links