NumPy_Array_Attributes
NumPy_Array_Attributes
1. ndarray.shape
2. ndarray.ndim
3. numpy.itemsize
4. numpy.flags
Example Code 1:
import numpy as np
a = np.array([[4,5,6],[7,8,9]])
print(a.shape)
Output
(2,3)
The code creates a 2x3 array named a, and prints its shape, which is (2,
3), indicating 2 rows and 3 columns.
Example Code 2:
a = np.array([[4,5,6],[7,8,9]])
a.shape = (3,2)
print(a)
Output
[[4 5]
[6 7]
[8 9]]
1|Page
This code snippet creates a 2x3 array a, then resizes it to a 3x2 array
using the shape property. Finally, it prints the resized array.
Example Code:
import numpy as np
a = np.array([[4,5,6],[7,8,9]])
b = a.reshape(3,2)
print(b)
Output
[[4 5]
[6 7]
[8 9]]
The code creates a NumPy array a with shape (2,3), then reshapes it to
a new array b with shape (3,2), and finally prints the reshaped array b.
Example Code:
import numpy as np
Output
1
3
2|Page
3. numpy.itemsize: This array attributes provides the size of each element
in the array, measured in bytes.
Output
4
3|Page
Example Code:
import numpy as np
x = np.array([3,4,5,6,7])
print(x.flags)
Output
C_CONTIGUOUS : True
F_CONTIGUOUS : True
OWNDATA : True
WRITEABLE : True
ALIGNED : True
WRITEBACKIFCOPY : False
The code creates a NumPy array 'x' with elements [3,4,5,6,7] and prints
its flags indicating memory layout and properties.
4|Page