What is the difference between dtype='f'
, dtype='f4'
, dtype='>f4'
, dtype'<f4'
?
The syntax is not explained in docs on types (except that 'f' is a shorthand for 'float32'); it is extensively used in the page on records but the meaning of >
/<
is also left unexplained in there.
After some experimentation I found out that
In [13]: a = np.array([1.0], dtype='f')
In [15]: print(a.dtype)
float32
and
In [16]: a = np.array([1.0], dtype='<f4')
In [17]: print(a.dtype)
float32
but
In [18]: a = np.array([1.0], dtype='>f4')
In [19]: print(a.dtype)
>f4
It makes me believe those are not equivalent, which may be the explanation for issues I am facing with an external library.
It means: 'O' (Python) objects. Source. The first character specifies the kind of data and the remaining characters specify the number of bytes per item, except for Unicode, where it is interpreted as the number of characters. The item size must correspond to an existing type, or an error will be raised.
'f4' also means 'float32' because it has 4 bytes and each byte has 8 bits. Similarly, 'f8' means 'float64' because 8*8 = 64.
# dtype('<U11') In the first case, each element of the list we pass to the array constructor is an integer. Therefore, NumPy decides that the dtype should be integer (32 bit integer to be precise). In the second case, one of the elements (3.0) is a floating-point number.
Endian-ness.
<
= little-endian (LSB first)
>
= big-endian (MSB first)
https://docs.scipy.org/doc/numpy/reference/generated/numpy.dtype.byteorder.html
By looking up the data type object you can see that the '>' and '<' reference the Endianess of the datatype
https://docs.scipy.org/doc/numpy/reference/arrays.dtypes.html
>>> dt = np.dtype('>H') # big-endian unsigned short
>>> dt = np.dtype('<f') # little-endian single-precision float
f is a single-precision floating point number and in your case it uses 4 bytes (4 x 8 = 32 bits).
dtype='<f4'
Makes dtype a 32 bit single-precision floating point number using little endian order of bytes.
More on Endianness can be found using wiki https://en.wikipedia.org/wiki/Endianness
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With