To return a scalar type which is common to the input arrays, use the numpy.common_type() method in Python Numpy. The 1st parameter is the input array(s). The return type will always be an inexact (i.e. floating point) scalar type, even if all the arrays are integer arrays. If one of the inputs is an integer array, the minimum precision type that is returned is a 64-bit floating point dtype.
All input arrays except int64 and uint64 can be safely cast to the returned dtype without loss of information.
Steps
At first, import the required library −
import numpy as np
To return a scalar type which is common to the input arrays, use the numpy.common_type() method −
print("Using the common_type() method in Numpy\n") print("Result...",np.common_type(np.arange(3,dtype=np.float32))) print("Result...",np.common_type(np.arange(3,dtype=np.float32), np.arange(2))) print("Result...",np.common_type(np.arange(3), np.array([22, 2.j]), np.array([32.9]))) print("Result...",np.common_type(np.arange(3), np.array([22, 39]), np.array([32.9]))) print("Result...",np.common_type(np.arange(3,dtyp =np.int32), np.arange(2))) Example
Example
import numpy as np # To return a scalar type which is common to the input arrays, use the numpy.common_type() method in Python Numpy. # The 1st parameter is the input array(s). print("Using the common_type() method in Numpy\n") print("Result...",np.common_type(np.arange(3,dtype=np.float32))) print("Result...",np.common_type(np.arange(3,dtype=np.float32), np.arange(2))) print("Result...",np.common_type(np.arange(3), np.array([22, 2.j]), np.array([32.9]))) print("Result...",np.common_type(np.arange(3), np.array([22, 39]), np.array([32.9]))) print("Result...",np.common_type(np.arange(3,dtype=np.int32), np.arange(2)))
Output
Using the common_type() method in Numpy Result... <class 'numpy.float32'> Result... <class 'numpy.float64'> Result... <class 'numpy.complex128'> Result... <class 'numpy.float64'> Result... <class 'numpy.float64'>