The numpy.result_type() method returns the type that results from applying the NumPy type promotion rules to the arguments. The 1st parameter is the operands of some operation whose result type is needed. Type promotion in NumPy works similarly to the rules in languages like C++, with some slight differences. When both scalars and arrays are used, the array’s type takes precedence and the actual value of the scalar is taken into account.
Steps
At first, import the required library −
import numpy as np
The numpy.result_type() method returns the type that results from applying the NumPy type promotion rules to the arguments −
print("Using the result_type() method in Numpy\n") print("Result...",np.result_type(2, np.arange(4,dtype='i1'))) print("Result...",np.result_type(5, 8)) print("Result...",np.result_type('i4', 'c8')) print("Result...",np.result_type(3.8, 8)) print("Result...",np.result_type(5, 20.7)) print("Result...",np.result_type(-8, 20.7)) print("Result...",np.result_type(10.0, -4))
Example
import numpy as np # The numpy.result_type() method returns the type that results from applying the NumPy type promotion rules to the arguments. # The 1st parameter is the operands of some operation whose result type is needed. print("Using the result_type() method in Numpy\n") print("Result...",np.result_type(2, np.arange(4,dtype='i1'))) print("Result...",np.result_type(5, 8)) print("Result...",np.result_type('i4', 'c8')) print("Result...",np.result_type(3.8, 8)) print("Result...",np.result_type(5, 20.7)) print("Result...",np.result_type(-8, 20.7)) print("Result...",np.result_type(10.0, -4))
Output
Using the result_type() method in Numpy Result... int8 Result... int64 Result... complex128 Result... float64 Result... float64 Result... float64 Result... float64