Updated March 23, 2023
Introduction to NumPy Data Types
The data types are used for defining a variable with a specific type that is used for identifying the variable and allowing the given types of data. Numpy is a data type used on Python programming, and comes along with the python package that can be used for multiple scientific computational operations. A few of the commonly used NumPy data types are np.byte, np.short, np.int_, np.uintc, np.ubyte, np.bool_, np.longlong, np.single, np.half, np.single, np.double, np.csingle, np.int8, np.int64, np.int32, np.intp, np.unitp, np.float64, etc.
Numpy Data Types
The various data types supported by numpy are :
Numpy data type | Closely associated C data type | Storage Size | Description |
np.bool_ | bool | 1 byte | can hold boolean values, like (True or False) or (0 or 1) |
np.byte | signed char | 1 byte | can hold values from 0 to 255 |
np.ubyte | unsigned char | 1 byte | can hold values from -128 to 127 |
np.short | signed short | 2 bytes | can hold values from -32,768 to 32,767 |
np.ushort | unsigned short | 2 bytes | can hold values from 0 to 65,535 |
np.uintc | unsigned int | 2 or 4 bytes | can hold values from 0 to 65,535 or 0 to 4,294,967,295 |
np.int_ | long | 8 bytes | can hold values from -9223372036854775808 to 9223372036854775807 |
np.uint | unsigned long | 8 bytes | 0 to 18446744073709551615 |
np.longlong | long long | 8 bytes | can hold values from -9223372036854775808 to 9223372036854775807 |
np.ulonglong | unsigned long long | 8 bytes | 0 to 18446744073709551615 |
np.half / np.float16 | — | allows half float precision with Format: sign bit, 5 bits exponent, 10 bits mantissa |
|
np.single | float | 4 bytes | allows single float precision Format: sign bit, 8 bits exponent, 23 bits mantissa |
np.double | double | 8 bytes | allows double float precision Format: sign bit, 11 bits exponent, 52 bits mantissa. |
np.longdouble | long double | 8 bytes | extension of float |
np.csingle | float complex | 8 bytes | can hold complex with real and imaginary parts up to single-precision float |
np.cdouble | double complex | 16 bytes | can hold complex with real and imaginary parts up to double-precision float |
np.clongdouble | long double complex | 16 bytes | extension of float for complex number |
np.int8 | int8_t | 1 byte | can hold values from -128 to 127 |
np.int16 | int16_t | 2 bytes | can hold values from -32,768 to 32,767 |
np.int32 | int32_t | 4 bytes | can hold values from -2,147,483,648 to 2,147,483,647 |
np.int64 | int64_t | 8 bytes | can hold values from -9223372036854775808 to 9223372036854775807 |
np.uint8 | uint8_t | 1 byte | can hold values from 0 to 255 |
np.uint16 | uint16_t | 2 bytes | can hold values from 0 to 65,535 |
np.uint32 | uint32_t | 4 bytes | can hold values from 0 to 4,294,967,295 |
np.uint64 | uint64_t | 8 bytes | can hold values from 0 to 18446744073709551615 |
np.intp | intptr_t | 4 bytes | a signed integer used for indexing |
np.uintp | uintptr_t | 4 bytes | an unsigned integer used for holding a pointer |
np.float32 | float | 4 bytes | single float precision |
np.float64 | double | 8 bytes | double float precision |
np.complex64 | float complex | 8 bytes | single float precision in complex numbers |
np.complex128 | double complex | 16 bytes | double float precision in complex numbers |
Examples of NumPy Data Types
Now, let’s understand how a particular numpy data type is used.
Example #1
Creating a data type object
dt = np.dtype(np.int8)
Output:
Example #2
Finding the size of a data type
dt = np.dtype(np.int8)
name = dt.name
sizeoftype = dt.itemsize
print('name:',name, 'size:',sizeoftype)
Output:
Example #3
Creating a data type object using unique symbols for each data type
Each data type in numpy has an associated character code that uniquely identifies it.
dt = np.dtype('i4')
Output:
Example #4
Using data types to create a structured array
employee_info = np.dtype([('name','S10'), ('age', 'i1'),('salary', 'f4'),('rating', 'f4')])
print(employee_info)
Output:
a = np.array([('Karthik',31,20000,3.84),('Rita',25,25123.34,4.41)], dtype = employee_info)
print (a)
Output:
Conclusion
Numpy data types are more or less like the C data types. They can be roughly categorized into a bool, byte, int, float, double and complex. It is a must for good programmers to understand how data is stored and manipulated. This can be achieved by understanding data types effectively.
Recommended Articles
This is a guide to NumPy Data Types. Here we discuss How a particular numpy data type is used along with the Examples. You may also have a look at the following articles to learn more –