Checking the Datatype of an array: ================================== dtype: ====== ==> a property which we can use to get the datatype of an array. Syntax: array-name.dtype import numpy a = numpy.array([1,2,3,4,5]) b = numpy.array([1-2j,1+2j,3-4j,3+4j]) c = numpy.array([1.2,0.01,2.1,0.001,2.3,0.0001]) d = numpy.array([1,3,5,7,9],dtype = numpy.float16) print("The Datatype of an array a = ",a.dtype) print("The Datatype of an array b = ",b.dtype) print("The Datatype of an array c = ",c.dtype) print("The Datatype of an array d = ",d.dtype) =============================================== Creation of an array with defined datatype: ============================================ Integer Array: ============== Syntax: numpy.array(object, dtype = numpy.int) import numpy # Integer Array a = numpy.array([1,2,3,4,5,6,7,8,9]) b = numpy.array([1,3,5,7,9],dtype = numpy.int8) print("The Array a = ",a) print("The Type of an array a = ",a.dtype) print("The Array b = ",b) print("The type of an array b = ",b.dtype) ================================================== import numpy as np # creating the integer array from float values a = np.array([1.2,2.3,3.4,4.5,5.6]) b = np.array([1.2,2.3,3.4,4.5,5.6],dtype = np.uint16) c = np.array([1+2j,1-2j,2-3j,2+3j]) # d = np.array([1+2j,1-2j,2-3j,2+3j],dtype = np.uint8) d = np.array([True, False, False, True, True]) e = np.array([True, False, False, True, True],dtype = np.uint8) print("The Given array a = ",a) print("The Type of an array a = ",a.dtype) print("The Given array b = ",b) print("The Type of an array b = ",b.dtype) print("The Given array c = ",c) print("The Type of an array c = ",c.dtype) # print("The Given array d = ",d) # print("The Type of an array d = ",d.dtype) print("The Given array d = ",d) print("The Type of an array d = ",d.dtype) print("The Given array e = ",e) print("The Type of an array e = ",e.dtype) ============================================================ Type Conversion =============== ==> Also called as "Type casting" which we can use to convert the type of array from one form to another form ==> There are two types of type conversion: 1) Internal Type Conversion ==> Type conversion can be by the computer ==> Automatic type casting a = 12 b = 12.23 c = 0 print(type(a),type(b),type(c)) c = a + b print(type(a),type(b),type(c)) 2) External Type Conversion the programmer/user can perform the type casting according to the requirement. a = 12 b = 12.23 c = 0 print(type(a),type(b),type(c)) c = a + b print(type(a),type(b),type(c)) a = float(a) # External Typecasting print(type(a),type(b),type(c)) ==> To perform the External type casting: 1) astype() 2) cast() 1) astype() =========== Syntax: source_array-name.astype(numpy.datatype) import numpy a = numpy.array([1,2,3,4,5,6,7,8,9,10]) print("The Given Array = ",a) print("The Type of array a = ",a.dtype) # typecasting using astype() # integer array ==> float array a_float = a.astype(numpy.float32) print("The Given Array = ",a_float) print("The Type of array a = ",a_float.dtype) # integer array to complex array a_complex = a.astype(numpy.complex64) print("The Given Array = ",a_complex) print("The Type of array a = ",a_complex.dtype) ==================================================== cast(): ======= NumPy have given list of built-in functions for external type casting: 1) int8() 2) int16() 3) int32() 4) int64() 5) uint8() 6) uint16() 7) uint32() 8) uint64() 9) float16() 10) float32() 11) float64() 12) complex64() 13) complex128() import numpy a = numpy.array([1,2,3,4,5,6,7,8,9,10]) print("The Array = ",a) print("The type of array = ",a.dtype) a1 = numpy.uint8(a) print("The Array = ",a1) print("The type of array = ",a1.dtype) # Typecasting with cast() # integer array to float a_float = numpy.float16(a) a_float1 = numpy.float64(a) print("The Array = ",a_float) print("The type of array = ",a_float.dtype) print("The Array = ",a_float1) print("The type of array = ",a_float1.dtype) # integer array to complex array a_complex = numpy.complex64(a) print("The Array = ",a_complex) print("The type of array = ",a_complex.dtype)