Introduction
NumPy provides different attributes to get information about an array. These attributes help us understand an array’s dimensions, shape, size, data type and memory usage.
Let’s explore the most commonly used NumPy array attributes with simple examples.
1. ndim – Number of Dimensions
The ndim attribute returns the number of dimensions in a NumPy array.
Example:
import numpy as np
arr = np.array([[10, 20, 30],
[40, 50, 60]])
print(arr.ndim)
Output:
2
Here, the array has two dimensions.
2. shape – Shape of an Array
The shape attribute returns the number of rows and columns in an array.
Example:
import numpy as np
arr = np.array([[10, 20, 30],
[40, 50, 60]])
print(arr.shape)
Output:
(2, 3)
Here, 2 represents the number of rows and 3 represents the number of columns.
3. size – Total Number of Elements
The size attribute returns the total number of elements present in an array.
Example:
import numpy as np
arr = np.array([[10, 20, 30],
[40, 50, 60]])
print(arr.size)
Output:
6
The array contains six elements.
4. dtype – Data Type of Elements
The dtype attribute tells us the data type of the elements stored in an array.
Example:
import numpy as np
arr = np.array([10, 20, 30, 40])
print(arr.dtype)
Output:
int64
Here, int64 indicates that the array elements are stored as 64-bit integers. The exact integer type may depend on your system and NumPy version.
5. itemsize – Memory Used by Each Element
The itemsize attribute returns the memory used by a single array element in bytes.
Example:
import numpy as np
arr = np.array([10, 20, 30, 40])
print(arr.itemsize)
Output:
8
Here, each element uses 8 bytes of memory if the array’s data type is int64.
6. nbytes – Total Memory Used by an Array
The nbytes attribute returns the total memory occupied by the array elements in bytes.
Example:
import numpy as np
arr = np.array([10, 20, 30, 40])
print(arr.nbytes)
Output:
32
The array contains four elements, and each element uses 8 bytes. Therefore, the total memory is 32 bytes.
7. T – Transpose of an Array
The T attribute returns the transpose of an array. In a 2-D array, it converts rows into columns and columns into rows.
Example:
import numpy as np
arr = np.array([[10, 20, 30],
[40, 50, 60]])
print(arr.T)
Output:
[[10 40]
[20 50]
[30 60]]
Here, the original array has 2 rows and 3 columns. After transposing, it has 3 rows and 2 columns.
Student Task: Explore Array Attributes
Use the following array to complete the tasks:
import numpy as np
marks = np.array([[75, 80, 85],
[90, 65, 70],
[88, 92, 78]])
- Find the number of dimensions using
ndim. - Find the shape using
shape. - Count the total elements using
size. - Check the data type using
dtype. - Find the memory used by each element using
itemsize. - Find the total memory used using
nbytes. - Display the transpose using
T.
Final Student Challenge: Analyze a Sales Array
A company stores sales figures for three products across two months.
import numpy as np
sales = np.array([[1200, 1500, 1800],
[1400, 1600, 2000]])
Write a Python program to display:
- Number of dimensions.
- Shape of the array.
- Total number of elements.
- Data type of elements.
- Memory used by each element.
- Total memory occupied by the array.
- Transpose of the sales array.
Expected Output:
Dimensions: 2
Shape: (2, 3)
Total Elements: 6
Data Type: int64
Item Size: 8
Total Memory: 48
Transpose:
[[1200 1400]
[1500 1600]
[1800 2000]]
Note: Memory values and integer data type may vary depending on the system.
Key Takeaways
ndimreturns the number of dimensions.shapereturns the dimensions of an array.sizecounts the total elements.dtypeidentifies the data type.itemsizereturns memory used by one element.nbytesreturns total memory used by array elements.Treturns the transpose of an array.
Frequently Asked Questions (FAQs)
1. What are NumPy Array Attributes in Python?
NumPy Array Attributes are properties that provide information about an array, such as its shape, size, number of dimensions, and data type.
2. What is a NumPy Array in Python?
A NumPy Array is a data structure provided by the NumPy library that stores elements of the same data type and allows efficient numerical calculations.
3. How do you check the dimensions of a NumPy Array?
You can use the ndim attribute to check the number of dimensions in a NumPy Array.
4. What are the different NumPy Array Dimensions?
NumPy supports 0-D (scalar), 1-D (one-dimensional), 2-D (two-dimensional), and 3-D (three-dimensional) arrays. It also supports higher-dimensional arrays.
5. How do you check the shape of a NumPy Array?
You can use the shape attribute to find the number of elements in each dimension of a NumPy Array.
6. What is the difference between NumPy Array Size and Shape?
The size attribute returns the total number of elements in an array, while the shape attribute returns the number of elements along each dimension.
7. Why is NumPy used in Python?
NumPy is used in Python for fast numerical calculations, array operations, mathematical functions, scientific computing, and data analysis.
8. How do you check the data type of a NumPy Array?
You can use the dtype attribute to identify the data type of elements stored in a NumPy Array, such as integers, floats, or strings.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
