Python NumPy Data Types
In this tutorial, we will cover datatypes in the NumPy library of Python.
In Numpy, all the items of an array are data type objects that are also known as NumPy dtypes. The data type object is used to implement the fixed size of memory corresponding to an array.
It mainly provides us information about the following:
It gives us information about the type of data (that is
It gives us information about the size of data
It tells us about the Byte order (little-endian or big-endian)
In the case of structured type, it tells us about the names of fields, the data type of each field, and part of the memory block taken by each field.
In the case, if the data type is a subarray it tells us about its shape and data type
In this, the byte order is decided just by prefixing '<' or '>' to the data type. where the symbol '<' means that encoding is little-endian (that is least significant is stored in the smallest address) and the symbol '>' means that encoding is big-endian (a most significant byte is stored in the smallest address).
Given below is a required syntax used to create the
numpy.dtype(object, align, copy)
Following is a description of arguments of the above mentioned constructor:
This argument is used to represent the object which is to be converted to the data type.
It is an optional argument as is used to add padding to the fields to match what a C compiler would output for a similar C-struct. This argument can be set to any boolean value
This argument is used to create a copy of
dtypeobject and it is also an optional argument.
The NumPy library mainly provides a higher range of numeric data types than that provided by Python. The list of numeric data types is given in the following table:
||This is used to represents the boolean value indicating true or false. It is stored as a byte.|
||This is the default type of an integer. It is identical to long type in C that mainly contains 64 bit or 32-bit integer.|
||This is similar to the C integer (c int) as it represents 32 or 64-bit int.|
||This is used to represent the integers that are used for indexing.|
||This is the 8-bit integer identical to a byte. The range of the value is -128 to 127.|
||This is the 2-byte (16-bit) integer and the range is -32768 to 32767.|
||This is the 4-byte (32-bit) integer. The range is -2147483648 to 2147483647.|
||This is the 8-byte (64-bit) integer and The range is -9223372036854775808 to 9223372036854775807.|
||This is 1-byte (8-bit) unsigned integer.|
||This is 2-byte (16-bit) unsigned integer.|
||This is 4-byte (32-bit) unsigned integer.|
||This is 8 bytes (64-bit) unsigned integer.|
||This is identical to float64.|
||This is used to represent the half-precision float. 5 bits are reserved for the exponent. 10 bits are reserved for the mantissa, and 1 bit is reserved for the sign.|
||This is used to represent single-precision float. 8 bits are reserved for the exponent, 23 bits are reserved for the mantissa, and 1 bit is reserved for the sign.|
||This is used to represent double-precision float. 11 bits are reserved for the exponent, 52 bits are reserved for the mantissa, 1 bit is used for the sign.|
||This is identical to complex128.|
||This is used to represent the complex number where real and imaginary part shares 32 bits each.|
||This is used to represent the complex number where real and imaginary part shares 64 bits each.|
Given below is a list of characters that are used to represent
dtype in Numpy:
u- unsigned integer
c- complex float
U- Unicode string
V- the fixed chunk of memory for other types ( void )
Now its time to dive into some examples.
Now in the example given below, we will try to find out the data type of the array containing strings:
import numpy as np ar1 = np.array(['chair', 'book', 'notebook']) print(ar1.dtype)
In the example given below, we will construct a datatype object. It is important to note here that the data type object is mainly an instance of
numpy.dtype class and it can also be created using
numpy.dtype function. Let us see:
import numpy as np dt1 = np.dtype(np.int64) print (dt1)
Instead of using the int8, int16, int32, int64, etc. usually the simpler represenation using strings 'i1', 'i2', 'i3', and 'i4' and so on is preferred. Now we will see an example related to this:
import numpy as np a = np.dtype('i4') print (a)
In our following example, we will create a structured datatype and then apply it to a
import numpy as np # info with ket and value a = np.dtype([('rollno',np.int16)]) print(a) a = np.array([(101,),(201,),(301,)], dtype = a) print (a)
[(101,) (201,) (301,)]
Example 5 - Change datatype:
In the example given below, we will change the datatype from
integer by using
int as a parameter value:
import numpy as np ar= np.array([1.1, 2.1, 3.1]) newarr = ar.astype(int) print(newarr) print(newarr.dtype)
[1 2 3]
In this tutorial, we covered the concept of datatypes in Array. We saw how
dtype object is used to specify the datatype of values, its syntax, and parameters required for the
dtype object. We also covered various numeric data types and then a few examples for your understanding.