There are three broad categories under which data types of Numpy are divided-
Numeric types include signed and unsigned integer, floating-point numbers, and complex numbers.
|Numpy Numeric Data Types||Character Codes||Description|
|int8||b, i1 or |i1||It is an 8-bit(1 byte) signed integer and its range is -128 to 127.|
|int16||h, i2 or <i2||It is a 16-bit(2 bytes) signed integer and its range is -32768 to 32767.|
|int32||i, i4 or <i4||It is a 32-bit(4 bytes) signed integer and its range is -231 to 231 - 1.|
|int64||l, i8 or <i8||It is a 64-bit(8 bytes) signed integer and its range is -263 to 263 - 1.|
|uint8||B, u1 or |u1||It is an 8-bit(1 byte) unsigned integer and its range is 0 to 255.|
|uint16||H, u2 or <u2||It is a 16-bit(2 bytes) unsigned integer and its range is 0 to 65535.|
|uint32||I, u4 or <u4||It is a 32-bit(4 bytes) unsigned integer and its range is 0 to 232 - 1.|
|uint64||L, u8 or <u8||It is a 64-bit(8 bytes) unsigned integer and its range is 0 to 264 - 1.|
|Single and Half Precision Floating-Point Number|
|float16||e, f2 or <f2||It is a half precision float with signed bit- 5 bits exponent and 10 bits mantissa.|
|float32||f, f4 or <f4||It is a single precision float with signed bit- 8 bits exponent and 23 bits mantissa.|
|Double Precision Floating-Point Number|
|float64||d, f8 or <f8||It is a double precision float with signed bit- 11 bits exponent and 52 bits mantissa.|
|Quadruple Precision Floating-Point Number|
|float128||g, f16 or <f16||It is a quadruple precision float with signed bit- 15 bits exponent and 112 bits mantissa.|
|complex64||F, c8 or <c8||It is a two 32-bit floating complex number|
|complex128||D, c16 or <c16||It is a two 64-bit floating complex number|
String types include String and Unicode.
|Numpy String Data Types||Character Codes||Description|
|S or string_||Sx or |Sx
x is the number of characters.
|It is a string data type whose size is equal to x bytes.|
|U, unicode_ or str_||Ux or <Ux
x is the number of characters.
|It is a unicode data type whose size is equal to 4*x bytes.|
It represents True or False.
|Numpy Boolean Data Type||Character Codes||Description|
|b||b||It is a boolean data type that takes either True or False.|
Character codes are mainly used in two places-
t = np.dtype([('name', 'S30'), ('age', 'i2')])
arr = np.array([12, 34.65, 98.333, 67], dtype='f4') print(arr)Output of the above program
[12. 34.65 98.333 67. ]
dtype() constructor is used to create homogeneous and heterogeneous data types. To the constructor, you can pass numpy data types, their character codes, and a list of tuples having fields and their data types.
homogeneous = dtype('float32')
The below code will create record data type that contains product name represented by a 50-character string, its quantity represented by 16-bit integer and its price represented by 16-bit floating point number.
hetero = dtype([('productname', S50), ('quantity', int16), ('price', float16)])
There are three attributes of dtype class-
import numpy as np d = np.dtype('float16') print(d.char)It will give
ein the output.
import numpy as np d = np.dtype('int32') print(d.str)It will give
<i4in the output.
import numpy as np d = np.dtype('int64') print(d.itemsize)It will give
8in the output.