The Numpy package provides attributes for the numpy arrays. These attributes help us to know the shape, dimension, and other properties of a given numpy array(ndarray
).
shape attribute
The shape property of a ndarray is used to get the current shape(row and column values in case of a matrix or 2-d array) of a given ndarray. This works only for regular arrays (arrays having a consistent number of elements throughout).
shape of Integer Array:
We create a ndarray
using array()
function. A list of lists having integer elements is passed as an object parameter to array()
function and the resultant array store it as np_lst
.
Now to check the shape of np_lst
, we print the shape using .shape
attribute of ndarray.
import numpy as np
np_lst = np.array([[11,12,13,14],[15,16,17,18]])
print(np_lst.shape)
#Output:
(2, 4)
The output shows that shape is (2, 4), which means the output array is containing 2 arrays and each array contains 4 elements.
The shape attribute will work similar for boolean, float and string data types.
shape of Nested lists:
Creating a ndarray from list of lists using array()
function.
np_lst = np.array([[1,2],[3,4],[5,[6,7]]])
print(np_lst.shape)
#Output:
(3, 2)
The last input array sequence([5, [6, 7]])contains 1 element and 1 array(containing 2 elements) which is also treated as an element.
shape of Nested-Empty list:
np_lst = np.array([[1,2,3],[3,4,[]]])
print(np_lst.shape)
#Output:
(2, 3)
The last input array sequence([3, 4, []]) contains 2 elements and 1 empty array which is also treated as an element.
Using shape attribute to reshape an Array:
The shape attribute gets the number of rows and columns of the ndarray. We can also use the shape attribute to change the number of rows and columns. The number of elements present in the reshaped array should be same as the original array.
Reshaping using the shape attribute will modify the original array.
Creating a ndarray
from list of lists having integer values using array()
function. Then assign a tuple containing the number of rows and columns to the shape of np_lst
.
np_lst = np.array([[1,2,3],[3,4,5]])
print('Before reshaping: ',np_lst.shape)
print(np_lst)
np_lst.shape = (3,2)
print('After reshaping: ',np_lst.shape)
print(np_lst)
#Output:
Before reshaping: (2, 3)
[[1 2 3]
[3 4 5]]
After reshaping: (3, 2)
[[1 2]
[3 3]
[4 5]]
NumPy reshape()
NumPy also provides a reshape() function to resize the ndarray. The reshape() function returns a new array with a changed shape instead of changing the original array.
Using reshape() to reshape ndarray:
Creating a ndarray from list of lists using array()
function. Then we reshape np_lst
using reshape()
function passing the new shape integer values as input to reshape()
function.
np_lst = np.array([[1, 2, 3, 4], [3, 4, 5, 6]])
print('Original Array: ', np_lst.shape)
print(np_lst)
new_np_lst = np_lst.reshape(4, 2)
print('Reshaped Array: ', new_np_lst.shape)
print(new_np_lst)
#Output:
Original Array: (2, 4)
[[1 2 3 4]
[3 4 5 6]]
Reshaped Array: (4, 2)
[[1 2]
[3 4]
[3 4]
[5 6]]
The output shows that before reshaping, the shape was (2, 4), and now the shape is (4, 2).
itemsize attribute
The itemsize attribute is a ndarray attribute that returns the size of each item in a ndarray in bytes. As an array contains elements of the same data type, the size of each element in the memory block is also the same.
itemsize of an Array :
We create a ndarray
where a list of integer values is passed as an object parameter in array()
function. Now to check the size of elements of np_lst
we print it using .itemsize
attribute of ndarray
.
np_lst = np.array([1,2,4])
print(np_lst.itemsize)
#Output:
4
The output shows that the size of each element in np_lst
is 4 bytes in the case of integer values.
- itemsize of the boolean array: 1 byte
- itemsize of the float array: 8 bytes
- itemsize of the complex array: 16bytes
itemsize of String Array :
np_lst = np.array(['Hy', 'How', 'Are', 'You'])
print(np_lst.itemsize)
#Output:
12
Every single character in a string takes 4 bytes. In our input string, the maximum number of characters is 3. So the itemsize attribute gives 12 (3*4) which is the memory size allocated to each element of string array.
itemsize of array having different data type elements:
np_lst = np.array([1, True, 5.9, 4+5j])
print(np_lst.itemsize)
#Output:
16
ndim attribute
The ndim property of a ndarray is used to get the dimensions of a given ndarray.
ndim of an Array:
Creating a ndarray from a 2-dimensional integer list array()
function.
np_lst = np.array([[1,2,3],[2,4,6]])
print(np_lst.ndim)
#Output:
2
ndim of an Array(3-d):
np_lst = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
print(np_lst.ndim)
#Output:
3
flags attribute
The flags attribute of ndarray is an attribute that gives all the information related to the memory layout of a given ndarray.
- C_CONTIGUOUS (C): If the input data is in a single “C-style” contiguous segment or not.
- F_CONTIGUOUS (F): If the input data is in a single “Fortran-style” contiguous segment or not.
- OWNDATA (O): If the input array owns the memory it uses or borrows from any other object.
- WRITEABLE (W): If the data area can be written or not. Setting this False will lock the data, making it read-only.
- ALIGNED (A): If input data and all its elements are aligned appropriately for the hardware or not.
- WRITEBACKIFCOPY (X): If the input array is a copy of any other array or not.
Following are some example showing the current values of flags for a given ndarray:
flags attribute of non copied ndarray:
Creating a ndarray from integer list using array()
function.
np_lst = np.array([1,2,4])
print(np_lst.flags)
#Output:
C_CONTIGUOUS : True
F_CONTIGUOUS : True
OWNDATA : True
WRITEABLE : True
ALIGNED : True
WRITEBACKIFCOPY : False
UPDATEIFCOPY : False
flags attribute of copied ndarray:
np_lst = np.array([1,2,4])
np_lst_copy = np_lst.reshape(3,1)
print(np_lst_copy.flags)
#Output:
C_CONTIGUOUS : True
F_CONTIGUOUS : True
OWNDATA : False
WRITEABLE : True
ALIGNED : True
WRITEBACKIFCOPY : False
UPDATEIFCOPY : False