π 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
| Attribute | Meaning |
|---|---|
| shape | Rows & columns |
| size | Total elements |
| dtype | Data type |
| ndim | Number 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
shapebefore operations - Use
dtypeto control memory usage - Use
ndimfor handling complex datasets
π Conclusion
Understanding array attributes in NumPy is essential for working with real-world data efficiently.