Header Ads Widget

⚡ Premium Tools Hub • EXE Apps + Full Python Source Code
Lite • Pro • Bundle Packs • Instant Download

NumPy Array Attributes – Shape, Size, Type, Dimension Explained with Examples

🐍 NumPy – Array Attributes

NumPy arrays come with powerful built-in attributes that help you understand the structure, type, and memory usage of your data.

These attributes are essential for:

  • Data analysis
  • Machine learning
  • Debugging arrays
  • Optimizing performance
  • Understanding dataset structure

Before performing operations like indexing, slicing, or reshaping, you should always inspect array attributes.


What are NumPy Array Attributes?

Array attributes are properties of a NumPy array that provide information about its structure and content.

They do not modify the array; they only describe it.


🔵 1. shape Attribute

The shape attribute returns the dimensions of the array.


Example

import numpy as np

arr = np.array([
    [1, 2, 3],
    [4, 5, 6]
])

print(arr.shape)

Output:

(2, 3)

✔ 2 rows and 3 columns


🟢 2. ndim Attribute

The ndim attribute returns the number of dimensions.


Example

print(arr.ndim)

Output:

2

✔ This is a 2D array


🟡 3. size Attribute

The size attribute returns the total number of elements.


Example

print(arr.size)

Output:

6

✔ 2 × 3 = 6 elements


🔴 4. dtype Attribute

The dtype attribute shows the data type of elements.


Example

print(arr.dtype)

Output:

int64

✔ All elements are integers


🟣 5. itemsize Attribute

The itemsize attribute returns the memory size (in bytes) of each element.


Example

print(arr.itemsize)

Output:

8

✔ Each integer uses 8 bytes


🟤 6. nbytes Attribute

The nbytes attribute returns the total memory consumed by the array.


Example

print(arr.nbytes)

Output:

48

✔ 6 elements × 8 bytes = 48 bytes


🔵 7. T Attribute (Transpose)

The T attribute returns the transpose of the array.


Example

print(arr.T)

Output:

[[1 4]
 [2 5]
 [3 6]]

✔ Rows become columns


🟢 8. flat Attribute

The flat attribute returns a 1D iterator over array elements.


Example

for item in arr.flat:
    print(item)

Output:

1
2
3
4
5
6

✔ Useful for looping through all elements


🧠 Understanding Array Attributes Together

shape  → structure (rows, columns)
ndim   → number of dimensions
size   → total elements
dtype  → data type
itemsize → memory per element
nbytes → total memory usage

⚡ Real-World Example

Dataset inspection

data = np.array([
    [10, 20, 30],
    [40, 50, 60],
    [70, 80, 90]
])

print("Shape:", data.shape)
print("Dimensions:", data.ndim)
print("Size:", data.size)
print("Data type:", data.dtype)

Output:

Shape: (3, 3)
Dimensions: 2
Size: 9
Data type: int64

📊 Why Array Attributes Matter

  • Helps understand dataset structure
  • Useful for debugging errors
  • Required before reshaping or broadcasting
  • Helps optimize memory usage
  • Important in ML preprocessing

🚀 Performance Insight

  • NumPy stores arrays in contiguous memory
  • Attributes like nbytes help track memory efficiency
  • Understanding dtype improves performance tuning

⚠️ Common Mistakes

1. Confusing shape and size

AttributeMeaning
shape         structure
size         total elements

2. Ignoring dtype

✔ Wrong dtype can cause performance issues


🧾 Summary

NumPy array attributes provide essential information about arrays:

  • shape → structure
  • ndim → dimensions
  • size → total elements
  • dtype → data type
  • itemsize → memory per element
  • nbytes → total memory usage
  • T → transpose
  • flat → iterator

🏁 Conclusion

Array attributes are the foundation for understanding NumPy arrays. They help you inspect, debug, and optimize your data structures efficiently.

Mastering these attributes will improve your skills in:

  • Data science
  • Machine learning
  • Scientific computing
  • Data preprocessing




Post a Comment

0 Comments