- ndim ==> returns the dimension of the array.
- shape ==> returns the shape of the array. Always return in tuple form. (10,) : 1-D(10,3): 2-D.
- size ==> To get total number of elements in the array.
- dtype ==> To get data type of elements of the array.
- itemsize ==> Length of each element of array in bytes.
Example
Python
# 0-D array
import numpy as np
a = np.array(10)
print(f'Dimension of the array a ==> {a.ndim}')
print(f'Shape of the array a ==> {a.shape}')
print(f'Size of the array a ==> {a.size}')
print(f'Data type of the elements in the array a ==> {a.dtype}')
print(f'Itemsize oof the array a ==> {a.itemsize}')
Output
PowerShell
Dimension of the array a ==> 0
Shape of the array a ==> ()
Size of the array a ==> 1
Data type of the elements in the array a ==> int32
Itemsize oof the array a ==> 4
Example
Python
# 1-D array
a = np.array([10,20,30])
print(f'Dimension of the array a ==> {a.ndim}')
print(f'Shape of the array a ==> {a.shape}')
print(f'Size of the array a ==> {a.size}')
print(f'Data type of the elements in the array a ==> {a.dtype}')
print(f'Itemsize oof the array a ==> {a.itemsize}')
Output
PowerShell
Dimension of the array a ==> 1
Shape of the array a ==> (3,)
Size of the array a ==> 3
Data type of the elements in the array a ==> int32
Itemsize oof the array a ==> 4
Example
Python
# 2-D array with int32(default)
a = np.array([[10,20,30],[40,50,60],[70,80,90]])
print(f'Dimension of the array a ==> {a.ndim}')
print(f'Shape of the array a ==> {a.shape}')
print(f'Size of the array a ==> {a.size}')
print(f'Data type of the elements in the array a ==> {a.dtype}')
print(f'Itemsize oof the array a ==> {a.itemsize}')
Output
PowerShell
Dimension of the array a ==> 2
Shape of the array a ==> (3, 3)
Size of the array a ==> 9
Data type of the elements in the array a ==> int32
Itemsize oof the array a ==> 2
Example
Python
# 2-D array with float64
a = np.array([[10,20,30],[40,50,60],[70,80,90]],dtype='float')
print(f'Dimension of the array a ==> {a.ndim}')
print(f'Shape of the array a ==> {a.shape}')
print(f'Size of the array a ==> {a.size}')
print(f'Data type of the elements in the array a ==> {a.dtype}')
print(f'Itemsize oof the array a ==> {a.itemsize}')
Output
PowerShell
Dimension of the array a ==> 2
Shape of the array a ==> (3, 3)
Size of the array a ==> 9
Data type of the elements in the array a ==> float64
Itemsize oof the array a ==> 8
Numpy Data Types
- Python data types : int, float, complex, bool and str.
- Numpy data types : Multiple data types present (Python + C).
- i ==> integer(int8,int16,int32,int64).
- b ==> boolean.
- u ==> unsigned integer(uint8,uint16,uint32,uint64).
- f ==> float(float16,float32,float64).
- c ==> complex(complex64,complex128).
- s ==> String.
- U ==> Unicode String.
- M ==> datetime etc.
- int8 ==> i1 ; int16 ==> i2; int32 ==> i4(default).
- float16 ==> f2 ; float32 ==> f4(default) ; float64 ==> f8.
- The value will be represented by 8 bits.
- MSB is reserved for sign.
- The range: -128 to 127.
int16(i2)
- The value will be represented by 16 bits.
- MSB is reserved for sign.
- The range: –32768 to 32767.
int32 (default) (i4)
- The value will be represented by 32 bits.
- MSB is reserved for sign.
- The range: -2147483648 to 2147483647.
int64
- The value will be represented by 64 bits.
- MSB is reserved for sign.
- The range: -9223372036854775808 to 9223372036854775807.
Example
Python
# For efficient usage of memory we will consider the datatypes
import sys
a = np.array([10,20,30,40]) # default datatype : int32
print(f"Size of int32 : {sys.getsizeof(a)}")
a = np.array([10,20,30,40],dtype='int8')
print(f"Size of int8 : {sys.getsizeof(a)}")
Output
PowerShell
Size of int32 : 120
Size of int8 : 108
Changing the data type of an existing array ==> astype()
we can change the data type of an existing array in two ways.
- by using astype().
- by using built-in function of numpy like float64().
Example
Python
# by using 'astype()'
a = np.array([10,20,30,40])
b = a.astype('float64')
print(f"datatype of elements of array a : {a.dtype}")
print(f"datatype of elements of array b : {b.dtype}")
print(a)
print(b)
Output
PowerShell
datatype of elements of array a : int32
datatype of elements of array b : float64
[10 20 30 40]
[10. 20. 30. 40.]
Example
Python
# by using built-in function of numpy like float64()
a = np.array([10,20,30,40])
b = np.float64(a)
print(f"datatype of elements of array a : {a.dtype}")
print(f"datatype of elements of array b : {b.dtype}")
print(a)
print(b)
Output
PowerShell
datatype of elements of array a : int32
datatype of elements of array b : float64
[10 20 30 40]
[10. 20. 30. 40.]
Example
Python
# converting to bool type
a = np.array([10,0,20,0,30])
x = np.bool(a)
Output
PowerShell
<ipython-input-105-93d254e35467>:4: DeprecationWarning: `np.bool` is a deprec
ated alias for the builtin `bool`. To silence this warning, use `bool` by its
elf. Doing this will not modify any behavior and is safe. If you specifically
wanted the numpy scalar type, use `np.bool_` here.
Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/de
vdocs/release/1.20.0-notes.html#deprecations
x = np.bool(a)
---------------------------------------------------------------------------
ValueError
Traceback (most recent call last)
<ipython-input-105-93d254e35467> in <module>
2
3 a = np.array([10,0,20,0,30])
----> 4 x = np.bool(a)
ValueError: The truth value of an array with more than one element is ambiguo
us. Use a.any() or a.all()
Example
Python
x = np.bool_(a)
x
Output
PowerShell
array([ True, False,
True, False,
True])