In NumPy, attributes are properties of NumPy arrays that provide information about the array's shape, size, data type, dimension, and so on.
For example, to get the dimension of an array, we can use the ndim
attribute.
There are numerous attributes available in NumPy, which we'll learn below.
Common NumPy Attributes
Here are some of the commonly used NumPy attributes:
Attributes | Description |
---|---|
ndim |
returns number of dimension of the array |
size |
returns number of elements in the array |
dtype |
returns data type of elements in the array |
shape |
returns the size of the array in each dimension. |
itemsize |
returns the size (in bytes) of each elements in the array |
data |
returns the buffer containing actual elements of the array in memory |
To access the Numpy attributes, we use the .
notation. For example,
array1.ndim
This returns the number of dimensions in array1.
Numpy Array ndim Attribute
The ndim
attribute returns the number of dimensions in the numpy array. For example,
import numpy as np
# create a 2-D array
array1 = np.array([[2, 4, 6],
[1, 3, 5]])
# check the dimension of array1
print(array1.ndim)
# Output: 2
In this example, array1.ndim
returns the number of dimensions present in array1. As array1 is a 2D array, we got 2 as an output.
NumPy Array size Attribute
The size
attribute returns the total number of elements in the given array.
Let's see an example.
import numpy as np
array1 = np.array([[1, 2, 3],
[6, 7, 8]])
# return total number of elements in array1
print(array1.size)
# Output: 6
In this example, array1.size
returns the total number of elements in the array1 array, regardless of the number of dimensions.
Since these are a total of 6 elements in array1, the size
attribute returns 6.
NumPy Array shape Attribute
In NumPy, the shape
attribute returns a tuple of integers that gives the size of the array in each dimension. For example,
import numpy as np
array1 = np.array([[1, 2, 3],
[6, 7, 8]])
# return a tuple that gives size of array in each dimension
print(array1.shape)
# Output: (2,3)
Here, array1 is a 2-D array that has 2 rows and 3 columns. So array1.shape
returns the tuple (2,3)
as an output.
NumPy Array dtype Attribute
We can use the dtype
attribute to check the datatype of a NumPy array. For example,
import numpy as np
# create an array of integers
array1 = np.array([6, 7, 8])
# check the data type of array1
print(array1.dtype)
# Output: int64
In the above example, the dtype
attribute returns the data type of array1.
Since array1 is an array of integers, the data type of array1 is inferred as int64
by default.
Note: To learn more about the dtype
attribute to check the datatype of an array, visit NumPy Data Types.
NumPy Array itemsize Attribute
In NumPy, the itemsize
attribute determines size (in bytes) of each element in the array. For example,
import numpy as np
# create a default 1-D array of integers
array1 = np.array([6, 7, 8, 10, 13])
# create a 1-D array of 32-bit integers
array2 = np.array([6, 7, 8, 10, 13], dtype=np.int32)
# use of itemsize to determine size of each array element of array1 and array2
print(array1.itemsize) # prints 8
print(array2.itemsize) # prints 4
Output
8 4
Here,
- array1 is an array containing 64-bit integers by default, which uses 8 bytes of memory per element. So,
itemsize
returns 8 as the size of each element. - array2 is an array of 32-bit integers, so each element in this array uses only 4 bytes of memory. So,
itemsize
returns 4 as the size of each element.
NumPy Array data Attribute
In NumPy, we can get a buffer containing actual elements of the array in memory using the data
attribute.
In simpler terms, the data
attribute is like a pointer to the memory location where the array's data is stored in the computer's memory.
Let's see an example.
import numpy as np
array1 = np.array([6, 7, 8])
array2 = np.array([[1, 2, 3],
[6, 7, 8]])
# print memory address of array1's and array2's data
print("\nData of array1 is: ",array1.data)
print("Data of array2 is: ",array2.data)
Output
Data of array1 is: <memory at 0x7f746fea4a00> Data of array2 is: <memory at 0x7f746ff6a5a0>
Here, the data
attribute returns the memory addresses of the data for array1 and array2 respectively.