A 32-bit float can exactly represent about 7 decimal digits of mantissa. Your number requires more, and therefore cannot be represented exactly.

The mechanics of what happens are as follows:

A 32-bit float has a 24-bit mantissa. Your number requires 27 bits to be represented exactly, so the last three bits are getting truncated (set to zero). The three lowest bits of your number are 0112; these are getting set to 0002. Observe that 0112 is 310.

Answer from NPE on Stack Overflow
🌐
PyTorch Forums
discuss.pytorch.org › vision
Beginner question about astype("float32") and float() - vision - PyTorch Forums
December 14, 2019 - In the customized dataset file, in a multi-label context, 1/Is there a reason for the use of float() in addition to .astype(“float32”) in this code? 2/And why cast the labels to float and not leave it as a numpy array of integers(one-hot encoded)? labels = torch.from_numpy( item.iloc[1:].values.astype(“float32”)).float() Thanks in advance
🌐
Note.nkmk.me
note.nkmk.me › home › python › numpy
NumPy: astype() to change dtype of an array | note.nkmk.me
February 4, 2024 - a = np.array([1, 2, 3]) print(a) print(a.dtype) # [1 2 3] # int64 a_float = a.astype(np.float32) print(a_float) print(a_float.dtype) # [1. 2. 3.] # float32 print(a) print(a.dtype) # [1 2 3] # int64 · source: numpy_astype.py · As mentioned above, dtype can also be specified as a type name string, type code string, or Python type.
🌐
JAX Documentation
docs.jax.dev › en › latest › _autosummary › jax.numpy.astype.html
jax.numpy.astype — JAX documentation
>>> x = jnp.array([0, 1, 2, 3]) >>> x Array([0, 1, 2, 3], dtype=int32) >>> x.astype('float32') Array([0.0, 1.0, 2.0, 3.0], dtype=float32)
🌐
NumPy
numpy.org › doc › stable › user › basics.types.html
Data types — NumPy v2.4 Manual
>>> np.array([1, 2, 3], dtype='f') array([1., 2., 3.], dtype=float32) >>> np.array([1, 2, 3], dtype='d') array([1., 2., 3.], dtype=float64)
🌐
EDUCBA
educba.com › home › software development › software development tutorials › pandas tutorial › pandas dataframe.astype()
Examples of Pandas DataFrame.astype()
April 3, 2023 - import pandas as pd Core_Series = pd.Series([ 10, 20, 30, 40, 50, 60]) print(" THE CORE SERIES ") print(Core_Series) Transformed_Series = Core_Series.astype('float32') print("") print(" THE FLOAT SERIES ") print(Transformed_Series)
Address   Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
Top answer
1 of 3
13

Let's see if I can address some of the confusion I'm seeing in the comments.

Make an array:

In [609]: x=np.arange(5)
In [610]: x
Out[610]: array([0, 1, 2, 3, 4])
In [611]: x.dtype
Out[611]: dtype('int32')

The default for arange is to make an int32.

astype is an array method; it can used on any array:

In [612]: x.astype(np.float32)
Out[612]: array([ 0.,  1.,  2.,  3.,  4.], dtype=float32)

arange also takes a dtype parameter

In [614]: np.arange(5, dtype=np.float32)
Out[614]: array([ 0.,  1.,  2.,  3.,  4.], dtype=float32)

whether it created the int array first and converted it, or made the float32 directly isn't any concern to me. This is a basic operation, done in compiled code.

I can also give it a float stop value, in which case it will give me a float array - the default float type.

In [615]: np.arange(5.0)
Out[615]: array([ 0.,  1.,  2.,  3.,  4.])
In [616]: _.dtype
Out[616]: dtype('float64')

zeros is similar; the default dtype is float64, but with a parameter I can change that. Since its primary task with to allocate memory, and it doesn't have to do any calculation, I'm sure it creates the desired dtype right away, without further conversion. But again, this is compiled code, and I shouldn't have to worry about what it is doing under the covers.

In [618]: np.zeros(5)
Out[618]: array([ 0.,  0.,  0.,  0.,  0.])
In [619]: _.dtype
Out[619]: dtype('float64')
In [620]: np.zeros(5,dtype=np.float32)
Out[620]: array([ 0.,  0.,  0.,  0.,  0.], dtype=float32)

randn involves a lot of calculation, and evidently it is compiled to work with the default float type. It does not take a dtype. But since the result is an array, it can be cast with astype.

In [623]: np.random.randn(3)
Out[623]: array([-0.64520949,  0.21554705,  2.16722514])
In [624]: _.dtype
Out[624]: dtype('float64')
In [625]: __.astype(np.float32)
Out[625]: array([-0.64520949,  0.21554704,  2.16722512], dtype=float32)

Let me stress that astype is a method of an array. It takes the values of the array and produces a new array with the desire dtype. It does not act retroactively (or in-place) on the array itself, or on the function that created that array.

The effect of astype is often (always?) the same as a dtype parameter, but the sequence of actions is different.

In https://stackoverflow.com/a/39625960/901925 I describe a sparse matrix creator that takes a dtype parameter, and implements it with an astype method call at the end.

When you do calculations such as dot or *, it tries to match the output dtype with inputs. In the case of mixed types it goes with the higher precision alternative.

In [642]: np.arange(5,dtype=np.float32)*np.arange(5,dtype=np.float64)
Out[642]: array([  0.,   1.,   4.,   9.,  16.])
In [643]: _.dtype
Out[643]: dtype('float64')
In [644]: np.arange(5,dtype=np.float32)*np.arange(5,dtype=np.float32)
Out[644]: array([  0.,   1.,   4.,   9.,  16.], dtype=float32)

There are casting rules. One way to look those up is with can_cast function:

In [649]: np.can_cast(np.float64,np.float32)
Out[649]: False
In [650]: np.can_cast(np.float32,np.float64)
Out[650]: True

It is possible in some calculations that it will cast the 32 to 64, do the calculation, and then cast back to 32. The purpose would be to avoid rounding errors. But I don't know how you find that out from the documentation or tests.

2 of 3
3
arr1 = np.array([25, 56, 12, 85, 34, 75])    
arr2 = np.array([42, 3, 86, 32, 856, 46])

arr1.astype(np.complex)
print (arr1)
print(type(arr1[0]))
print(arr1.astype(np.complex))
arr2 = np.array(arr2,dtype='complex')
print(arr2)
print(type(arr2[0]))

OUTPUT for above

[25 56 12 85 34 75]
<class 'numpy.int64'>
[25.+0.j 56.+0.j 12.+0.j 85.+0.j 34.+0.j 75.+0.j]
[ 42.+0.j   3.+0.j  86.+0.j  32.+0.j 856.+0.j  46.+0.j]
<class 'numpy.complex128'>

It can be seen that astype changes the type temporally as we do in normal type casting but where as the generic method changes the type permanently

🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.ndarray.astype.html
numpy.ndarray.astype — NumPy v2.4 Manual
June 22, 2021 - ‘same_kind’ means only safe ... to float32, are allowed. ‘unsafe’ means any data conversions may be done. ‘same_value’ means any data conversions may be done, but the values must not change, including rounding of floats or overflow of ints · New in version 2.4: Support for 'same_value' was added. ... If True, then sub-classes will be passed-through (default), otherwise the returned array will be forced to be a base-class array. ... By default, astype always returns ...
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.DataFrame.astype.html
pandas.DataFrame.astype — pandas 3.0.1 documentation
>>> ser = pd.Series([1, 2], dtype="int32") >>> ser 0 1 1 2 dtype: int32 >>> ser.astype("int64") 0 1 1 2 dtype: int64
Find elsewhere
🌐
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.ndarray.astype.html
numpy.ndarray.astype — NumPy v2.1 Manual
‘same_kind’ means only safe ... to float32, are allowed. ‘unsafe’ means any data conversions may be done. ... If True, then sub-classes will be passed-through (default), otherwise the returned array will be forced to be a base-class array. ... By default, astype always returns ...
🌐
GitHub
github.com › ageron › handson-ml › issues › 265
chapter 10 why have we converted MNIST data to float32 and not use directly ? · Issue #265 · ageron/handson-ml
July 29, 2018 - chapter 10 why have we converted MNIST data to float32 and not use directly ?#265 · Copy link · nitml · opened · on Jul 29, 2018 · Issue body actions · In chap 10 jupyter notebook code for MNIST dataset for DNN classifier was : (X_train, y_train), (X_test, y_test) = tf.keras.datasets.mnist.load_data() X_train = X_train.astype(np.float32).reshape(-1, 28*28) / 255.0 X_test = X_test.astype(np.float32).reshape(-1, 28*28) / 255.0` firstly why we have converted it to float and targets to int ?
Author   nitml
🌐
NumPy
numpy.org › doc › 2.3 › reference › generated › numpy.ndarray.astype.html
numpy.ndarray.astype — NumPy v2.3 Manual
‘same_kind’ means only safe ... to float32, are allowed. ‘unsafe’ means any data conversions may be done. ... If True, then sub-classes will be passed-through (default), otherwise the returned array will be forced to be a base-class array. ... By default, astype always returns ...
🌐
GitHub
github.com › numpy › numpy › issues › 12721
np.array(..., dtype=np.float32).astype(np.float16) doesn't handle denorms properly · Issue #12721 · numpy/numpy
January 11, 2019 - The following interpreter sequence shows such a case that is properly handled by the float64->float16 conversion, but not by the float32->float16 conversion (see the last line below). $ python3 Python 3.5.2 (default, Nov 12 2018, 13:43:14) [GCC 5.4.0 20160609] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import numpy as np >>> np.__version__ '1.14.3' >>> '%.40f' % np.array([2**-24]).astype(np.float64)[0] '0.0000000596046447753906250000000000000000' >>> '%.40f' % np.array([2**-24]).astype(np.float32)[0] '0.0000000596046447753906250000000000000000' >>> '%.4
Author   DickJC123
🌐
Spark Code Hub
sparkcodehub.com › pandas › data-manipulation › convert-types-astype
Mastering astype in Pandas for Data Type Conversion
df = df.astype({ 'region': 'category', 'revenue': 'float32', 'units_sold': 'Int16' }) print(df.memory_usage(deep=True)) This enhances performance (Memory Usage). The · astype method can be chained with other Pandas operations like · replace or · groupby for complex workflows (Replace Function).
🌐
Spark By {Examples}
sparkbyexamples.com › home › pandas › pandas convert column to float in dataframe
Pandas Convert Column to Float in DataFrame - Spark By {Examples}
October 14, 2024 - You can use the Pandas DataFrame.astype() function to convert a column from string/int to float, you can apply this on a specific column or on an entire DataFrame. To cast the data type to a 54-bit signed float, you can use numpy.float64, ...
🌐
Python⇒Speed
pythonspeed.com › articles › float64-float32-precision
The problem with float32: you only get 16 million values
February 1, 2023 - And as it turns out, float32s can represent 16 million different values at a precision of $1,000,000 just fine: >>> values = np.arange(0, 16_000_000_000_000, step=1_000_000) >>> values[-5:] array([15999995000000, 15999996000000, 15999997000000, 15999998000000, 15999999000000]) >>> values[-5:].astype(np.float32) array([1.5999995e+13, 1.5999996e+13, 1.5999997e+13, 1.5999998e+13, 1.5999999e+13], dtype=float32) So in this case we don’t have to do anything special at all: float32 works just fine.
🌐
GitHub
github.com › weecology › DeepForest › issues › 669
Update uses of .astype(float) in utilities.py to float32 · Issue #669 · weecology/DeepForest
May 11, 2024 - Following #667 we should use numpy float32 throughout the code base for compatibility with mac M3 chips. There are a few additional uses of .astype(float) in utilities.py not covered by #667 that should be updated to .astype("float32") so folks using M3's can use those helper functions.
🌐
NumPy
numpy.org › doc › 2.0 › reference › generated › numpy.ndarray.astype.html
numpy.ndarray.astype — NumPy v2.0 Manual
‘same_kind’ means only safe ... to float32, are allowed. ‘unsafe’ means any data conversions may be done. ... If True, then sub-classes will be passed-through (default), otherwise the returned array will be forced to be a base-class array. ... By default, astype always returns ...