🔢 Numpy Data Types¶
NumPy, short for Numerical Python, is a library in Python that is often used for numerical computations. In this tutorial, we will explore a fundamental aspect of NumPy: data types (dtypes
).
1. What are NumPy dtypes?¶
NumPy's array object, ndarray
, is a grid that can hold values of the same data type (dtype
). The dtype
object is an instance of numpy.dtype class and it can be created using numpy.dtype.
The dtype
object describes the following aspects of the data:
- Type of the data (integer, float, Python object, etc.)
- Size of the data (number of bytes)
- Byte order (little-endian or big-endian)
- If the data type is a sub-array, its shape and data type
- The data type's name
2. Working with NumPy dtypes¶
First, let's import NumPy.
import numpy as np
2.1 Creating Arrays with Predefined dtypes¶
You can define the dtype
when you create the array.
# Creating an array of float type
arr = np.array([1, 2, 3], dtype='float')
arr, arr.dtype
(array([1., 2., 3.]), dtype('float64'))
2.2 Checking the dtype of an Array¶
You can check the dtype of an array using the dtype
attribute.
arr = np.array([1, 2, 3])
arr.dtype
dtype('int64')
2.3 Converting Data Type on Existing Arrays¶
Data type of existing arrays can be converted to another type using the astype
function.
arr = np.array([1.1, 2.2, 3.3])
print(arr)
print("dtype before:", arr.dtype)
# Convert float to int
arr = arr.astype('int')
print(arr)
print("dtype after:", arr.dtype)
[1.1 2.2 3.3] dtype before: float64 [1 2 3] dtype after: int64
3. Types of dtypes¶
Here are some examples of numpy dtypes:
bool
: Boolean values (True or False) stored as a byte.int
: Integer of variable size, also used for boolean types.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.complex128
: Complex number, represented by two 64-bit floats.
This is by no means a comprehensive list. You can find the complete list of dtypes in the NumPy documentation.
Remember, understanding how dtypes work in NumPy can help you make your code more efficient and take less memory. So it's worth spending time to get familiar with these concepts.