Introduction
In this lab, we will cover datatypes in the NumPy library of Python. We will go through the dtype object syntax and its parameters.
VM Tips
After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.
Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.
If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.
Understanding NumPy dtype Object
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:
- The type of data (e.g.,
integer,float, or Pythonobject) - The size of data
- 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
Creating a NumPy dtype object
Given below is the required syntax used to create the dtype object:
numpy.dtype(object, align, copy)
Following is a description of arguments of the above mentioned constructor:
- object: This argument is used to represent the object which is to be converted to the data type.
- align: It is an optional argument. It 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.
- copy: This argument is used to create a copy of
dtypeobject and it is also an optional argument.
Numeric Data Types in NumPy
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 below table:
| SN | Data type | Description |
|---|---|---|
| 1 | bool_ |
This is used to represents the boolean value indicating true or false. It is stored as a byte. |
| 2 | int_ |
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. |
| 3 | intc |
This is similar to the C integer (C int) as it represents 32 or 64-bit int. |
| 4 | intp |
This is used to represent the integers that are used for indexing. |
| 5 | int8 |
This is the 8-bit integer identical to a byte. The range of the value is -128 to 127. |
| 6 | int16 |
This is the 2-byte (16-bit) integer and the range is -32768 to 32767. |
| 7 | int32 |
This is the 4-byte (32-bit) integer. The range is -2147483648 to 2147483647. |
| 8 | int64 |
This is the 8-byte (64-bit) integer and the range is -9223372036854775808 to 9223372036854775807. |
| 9 | uint8 |
This is 1-byte (8-bit) unsigned integer. |
| 10 | uint16 |
This is 2-byte (16-bit) unsigned integer. |
| 11 | uint32 |
This is 4-byte (32-bit) unsigned integer. |
| 12 | uint64 |
This is 8 bytes (64-bit) unsigned integer. |
| 13 | float_ |
This is identical to float64. |
| 14 | float16 |
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. |
| 15 | float32 |
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. |
| 16 | float64 |
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. |
| 17 | complex_ |
This is identical to complex128. |
| 18 | complex64 |
This is used to represent the complex number where real and imaginary part shares 32 bits each. |
| 19 | complex128 |
This is used to represent the complex number where real and imaginary part shares 64 bits each. |
Characters used to represent dtype in NumPy
Here is the list of characters that are used to represent dtype in NumPy:
i: integerb: booleanu: unsigned integerf: floatc: complex floatm: timedeltaM: datetimeO: objectS: stringU: Unicode stringV: the fixed chunk of memory for other types (void)
Examples
Example 1: Finding Data Type of an Array
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)
Output:
<U8
Example 2: Constructing a dtype Object
We can create a dtype object using numpy.dtype function.
import numpy as np
dt1 = np.dtype(np.int64)
print (dt1)
Output:
int64
Example 3: Using Shorter Representation of Numeric Data Types
In the following example, we will use the shorter representation of numeric data types:
import numpy as np
a = np.dtype('i4')
print (a)
Output:
int32
Example 4: Creating a Structured dtype Object
We will create a structured data type and apply it to a ndarray object:
import numpy as np
## info with key and value
a = np.dtype([('rollno',np.int16)])
print(a)
a = np.array([(101,),(201,),(301,)], dtype=a)
print(a)
Output:
[('rollno', '<i2')]
[(101,) (201,) (301,)]
Example 5: Changing Data Type
In the following example, we will change the datatype from float to 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)
Output:
[1 2 3]
int64
Summary
In this lab, we covered the concept of datatypes in an 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.