NumPy – Data Types:
Nympy provides the below dataypes more than what exactly python holds.
Data type | Description |
bool_ | Boolean (True or False) stored as a byte |
int_ | Default integer type (same as C long; normally either int64 or int32) |
intc | Identical to C int (normally int32 or int64) |
intp | Integer used for indexing (same as C ssize_t; normally either int32 or int64) |
int8 | Byte (-128 to 127) |
int16 | Integer (-32768 to 32767) |
int32 | Integer (-2147483648 to 2147483647) |
int64 | Integer (-9223372036854775808 to 9223372036854775807) |
uint8 | Unsigned integer (0 to 255) |
uint16 | Unsigned integer (0 to 65535) |
uint32 | Unsigned integer (0 to 4294967295) |
uint64 | Unsigned integer (0 to 18446744073709551615) |
float_ | Shorthand for float64. |
float16 | Half precision float: sign bit, 5 bits exponent, 10 bits mantissa |
float32 | Single precision float: sign bit, 8 bits exponent, 23 bits mantissa |
float64 | Double precision float: sign bit, 11 bits exponent, 52 bits mantissa |
complex_ | Shorthand for complex128. |
complex64 | Complex number, represented by two 32-bit floats (real and imaginary components) |
complex128 | Complex number, represented by two 64-bit floats (real and imaginary components) |
Let us see some example below:
How identify the datatype ?
Below is the command. we will use the “dtype” method to identify the datatype
>>> import numpy as np >>> x=np.array([1,2,3,4,5]) >>> x array([1, 2, 3, 4, 5]) >>> x.dtype dtype('int32')
What if you want to assign the integers as float datatype ?
Below is the command. Please observe that we have given the dtype as floating datatype.
>>> x=np.array([1,2,3,4,5], dtype=float) >>> x.dtype dtype('float64') >>> x array([ 1., 2., 3., 4., 5.])
Below example shows the floating dataype values.
>>> y=np.array([.1,.2,.3,.4,.5]) >>> y array([ 0.1, 0.2, 0.3, 0.4, 0.5]) >>> y.dtype dtype('float64')
Now let us see some more data types too.
This example will show the Boolean datatype.
>>> eq=np.array([True, False]) >>> eq array([ True, False], dtype=bool) >>> eq.dtype dtype('bool')
This example will show the String datatype.
>>> str=np.array(["This", "is", "NumPy"]) >>> str array(['This', 'is', 'NumPy'], dtype='<U5') >>> str.dtype dtype('<U5')
This example will show the Complex datatype.
>>> j=2 >>> solve=np.array([2+3j, 5+j, 9+2*3j]) >>> solve array([ 2.+3.j, 7.+0.j, 9.+6.j]) >>> solve.dtype dtype('complex128') >>> solve.real array([ 2., 7., 9.])