Data types ¶
ndarray.dtype : the data type of an array.
One crucial difference in ndarray versus lists is that a ndarray can only contain one data type. Elements must be all floats, ints, strings, bools, etc. This is because NumPy is heavily focused on performance, and enforcing this rule let's them design blazing fast algorithms.
What are all of the data types?
Note
All of these data types are attributes of NumPy.
For example, to use
int8
you could specify "int8" as
dtype
or
np.int8
.
Integers ¶
Type | Alias | Description |
---|---|---|
int8
|
i1
|
An 8-bit signed integer whose values exist on the interval
[-128, +127]
.
|
int16
|
i2
|
A 16-bit signed integer whose values exist on the interval
[−32,767, +32,767]
.
|
int32
|
i4
|
A 32-bit signed integer whose values exist on the interval
[−2,147,483,647, +2,147,483,647]
.
|
int64
|
i8
|
A 64-bit signed integer whose values exist on the interval
[−9,223,372,036,854,775,807, +9,223,372,036,854,775,807]
.
|
Type | Alias | Description |
---|---|---|
uint8
|
u1
|
An 8-bit unsigned integer whose values exist on the interval
[0, +255]
.
|
uint16
|
u2
|
A 16-bit unsigned integer whose values exist on the interval
[0, +65,535]
.
|
uint32
|
u4
|
A 32-bit unsigned integer whose values exist on the interval
[0, +4,294,967,295]
.
|
uint64
|
u8
|
A 64-bit unsigned integer whose values exist on the interval
[0, +18,446,744,073,709,551,615]
.
|
Floats ¶
We almost always use
float64
.
Type | Alias | Description | Min > 0 | Max > 0 |
---|---|---|---|---|
float32
|
f4
|
IEEE 754 single-precision binary floating-point number. | 1.18 ⨉ 10 -38 | 3.40 ⨉ 10 38 |
float64
|
f8
|
IEEE 754 double-precision binary floating-point number. | 2.23 ⨉ 10 -308 | 1.80 ⨉ 10 308 |
The table below shows the absolute precision for both formats over a range of values given by
$$ \text{precision} = \frac{\text{value}}{2^{n}}; $$
where $n$ is the number of mantissa bits which is 23 for
float32
and 52 for
float64
.
This can be used to select an appropriate format given the expected value of a number and the required precision.
Value |
float32
precision
|
float64
precision
|
---|---|---|
10 -12 | 10 -19 | 10 -28 |
10 -9 | 10 -16 | 10 -25 |
10 -6 | 10 -13 | 10 -22 |
10 -3 | 10 -10 | 10 -19 |
10 0 | 10 -7 | 10 -16 |
10 3 | 10 -4 | 10 -13 |
10 6 | 10 -1 | 10 -10 |
10 9 | 10 2 | 10 -7 |
10 12 | 10 5 | 10 -4 |
If you are primarily focused on speed, then check to see if the amount of precision loss is negligible.