NumPy Array Attributes (shape, size, dtype, ndim Explained)

πŸ“Œ Introduction

Once you know how to create arrays in NumPy, the next step is to understand their structure.

That’s where NumPy array attributes come in.


πŸ” What are NumPy Array Attributes?

Attributes are properties that describe:

  • Structure of the array
  • Type of data
  • Number of elements

They help you analyze and manipulate data efficiently.


πŸ§ͺ Example Array

import numpy as nparr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr)

πŸ‘‰ Output:

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

πŸ“ 1. shape (Structure of Array)

Tells how many rows and columns an array has.

print(arr.shape)

πŸ‘‰ Output:

(2, 3)

βœ” Meaning:

  • 2 rows
  • 3 columns

πŸ”’ 2. size (Total Elements)

Gives the total number of elements in the array.

print(arr.size)

πŸ‘‰ Output:

6

🧬 3. dtype (Data Type)

Shows the type of elements stored.

print(arr.dtype)

πŸ‘‰ Output:

int64

βœ” Common types:

  • int32 / int64
  • float32 / float64
  • bool

πŸ“Š 4. ndim (Number of Dimensions)

Tells how many dimensions the array has.

print(arr.ndim)

πŸ‘‰ Output:

2

βœ” Meaning:

  • 1D β†’ single row
  • 2D β†’ matrix
  • 3D β†’ multi-layer data

πŸ“¦ Summary Table

AttributeMeaning
shapeRows & columns
sizeTotal elements
dtypeData type
ndimNumber of dimensions

🧠 Why These Attributes Matter?

Using these attributes helps you:

  • Debug errors
  • Understand dataset structure
  • Optimize performance

πŸ”— Used with Other Libraries

These attributes are widely used in:

  • Pandas
  • Scikit-learn
  • TensorFlow

🎯 Pro Tips

  • Always check shape before operations
  • Use dtype to control memory usage
  • Use ndim for handling complex datasets

πŸ”š Conclusion

Understanding array attributes in NumPy is essential for working with real-world data efficiently.

Leave a Comment

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *