Type Conversion in NumPy: ======================== two types: 1) Internal Type Conversion/Implicit Type Conversion ===================================================== import numpy a = numpy.array([1,2,3,4,5,6],dtype = 'c8') print("The Type of array = ",a.dtype) 2) External Type Conversion/Explicit Type Conversion ==================================================== 1) astype() 2) cast() functions 1) astype() =========== ==> a built-in method in numpy which we can use for the external type conversion Syntax: source_array_name.astype(numpy.datatype) import numpy as np # creating the array with unsigned integer 32 bit representation # a = np.array([1,3,5,7,9,11,13,15,17,19,21,23,25],dtype = np.uint32) a = np.array([1,3,5,7,9,11,13,15,17,19,21,23,25],dtype = 'u4') print("The Type of the given array definition is = ",a.dtype) # Type conversion from Unsigned integer to signed integer 64-bits a_s_int = a.astype(np.int64) print("The Type of the above definition is = ",a_s_int.dtype) # Unsigned Integer to float 32-bit a_float = a.astype('f4') print("The Type of the above definition is = ",a_float.dtype) # Signed Integer ==> complex 128-bit a_complex = a_s_int.astype('c16') print("The Type of the above definition is = ",a_complex.dtype) # Floating ===> Boolean a_bool = a_float.astype('b1') print("The Type of the above definition is = ",a_bool.dtype) =================================================================== Cast functions: =============== Syntax: numpy.cast(s-array) int8() ==> any array to signed 8-bit integer array int16() ==> any array to signed 16-bit integer array int32() ==> any array to signed 32-bit integer array int64() ==> any array to signed 64-bit integer array uint8() ==> any array to unsigned 8-bit integer array uint16() ==> to unsigned 16-bit integer array uint32() ==> to unsigned 32-bit integer array uint64() ==> to unsigned 64-bit integer array float32() float64() complex64() complex128() import numpy as np a = np.array([1,3,5,7,9,11,13,15]) print("The Type of defined array = ",a.dtype) # integer 32-bit array ==> unsigned integer 64-bit a_u = np.uint64(a) print("Type = ",a_u.dtype) # signed integer ==> complex64 a_c = np.complex64(a) print("Type = ",a_c.dtype) # unsigned integer ==> float a_u_f = np.float16(a_u) print("Type = ",a_u_f.dtype) a_bool = np.bool_(a) print("Type = ",a_bool.dtype)