You can learn about NumPy in Python Programs with Outputs helped you to understand the language better.
Python Programming – Array Attributes
NumPy arrays have a set of attributes that you can access. These attributes include the array’s size, shape, number of dimensions, and data type. Some of the important attributes of a NumPy object are:
- Nadim: Displays the dimension of the array.
- Shape: Returns a tuple of integers indicating the size of the array.
- Size: Returns the total number of elements in the NumPy array.
- Dtype: Returns the type of elements in the array, i.e., int64.
- Itemsize: Returns the length of each element of the array in bytes.
- Reshape: Reshapes the NumPy array.
Ndim
Specifies minimum dimensions of resultant array. (See Figure 13.2).
Example
Demo of ndim attribute.
import numpy as np arr = np.array( [ [ 1 , 2 , 3 , 4] , [ 4 , 5 , 6 , 7 ] , [ 9 , 10 , 11 , 23 ] ] ) print ( arr . ndim )RUN >>> 2 >>> |
- Python Programming – Array From Numerical Ranges
- Python Programming – Array Creation
- Python Programming – Introduction To Numpy
Shape
The shape of an array tells the number of elements along each axis of it. The ‘Shape’ function returns the shape of the array. It is a tuple of integers. These numbers denote the lengths of the corresponding array dimension. You of shape attribute. can say that the “shape” of an array is a tuple with the number of elements per axis (dimension). In our example, the shape is equal to (6, 3), i.e., you have 6 lines and 3 columns.
np.shape = returns a tuples of integers indicating the size of array.
Example
Demo of shape attribute.
import numpy as np x = np . array ( [ [ 47,73,67 ], [66,79,79], [75,87,99], [69,62,61], [63,99,83], [58,82,73]]) print ( np . shape ( x ) ) x . shape = ( 2 , 9 ) print ( x )RUN >>> (6, 3) [[47 73 67 66 79 79 75 87 99] [69 62 61 63 99 83 58 82 73]] >>> |
The shape of an array also tells us something about the order in which the indices are processed, i.e., first rows, then columns, and after that the further dimensions.
Output
[ [47 73 67 66 79 79 75 87 99]
[69 62 61 63 99 83 58 82 73] ]
“shape” can also be used to change the shape of an array for example,
>>> x.shape = ( 2 , 9 )
>>> print ( x )
Size
To get the size of the array, the size function associated with the numpy array is used. (See Figure 13.4).
Example
Demo of size attribute.
import numpy as np a = np.array([[1,2,3,4,5,6,7]]) print(“Array Size:”, a.size)RUN >>> Array Size: 7 >>> |
Let’s Try
import numpy as np a = np.array([[11,12,13,14,15,16,17]]) print(“Array Size:”,a.size) print(“Shape:”,a.shape) |
Dtype
It tells about the type of data stored in the array. By default, ndarrays have the datatype as a float. Every array has one and only one dtype. All items in it should be of that dtype. An integer is a value without a decimal. If you create an array with deci¬mal, then the type will change to float.
Example
Demo of dtype attribute.
import numpy as np a = np.array([1,2,3]) print(a.dtype) RUN >>> int32 >>> |
Let’s Try
import numpy as np x = np.array([1.1,2.0,3.2]) print(x.dtype) |
Itemsize
The itemsize attribute returns the length of each element of array in byte. (See Figure 13.6).
Example
Demo of itemsize attribute.
import numpy as np a = np.array([ [11,12,13]]) printC’Each item contains”,a.itemsize,“bytes”)RUN >>> Each item contains 4 bytes >>> |
Reshape
You can use reshape method to reshape an array. The reshape ( ) function associated with the ndarray object is used to reshape the array. It accepts the two parameters indicating the row and columns of the new shape of the array. The only required condition is the original size of the array remains unchanged. (See Figure 13.7).
Example
Demo of reshape attribute.
import numpy as np a = np.array([[1,2], [3,4], [5,6]]) print(“original array”) print(a) a=a.reshape(2,3) print(“reshaped array”) print(a)RUN >>> original array [[1 2] [3 4] [5 6]] reshaped array [[1 2 3] [456]] >>> |