The problem is not that you're failing to set a float64 dtype. The error message says:

Input contains NaN, infinity or a value too large for dtype('float32').

So try checking for those conditions first:

assert not np.any(np.isnan(x_arr))
assert np.all(np.isfinite(x_arr))
assert np.all(x_arr <= finfo('float32').max)
assert np.all(x_arr >= finfo('float32').min)
Answer from John Zwinck on Stack Overflow
🌐
NumPy
numpy.org › doc › stable › reference › arrays.dtypes.html
Data type objects (dtype) — NumPy v2.5 Manual
>>> dt = np.dtype('uint32') # 32-bit unsigned integer >>> dt = np.dtype('float64') # 64-bit floating-point number
🌐
GitHub
github.com › pandas-dev › pandas › issues › 55679
BUG: setting 1.02 into float32 Series upcasts to float64 · Issue #55679 · pandas-dev/pandas
October 25, 2023 - I checked what earlier versions of Pandas did, and it looks like Pandas 1.4.4 kept the dtype as float32 after the assignment, while Pandas 1.5.2 changes the dtype to float64. I'm not sure whether that was intentional or not - it doesn't seem to be explicitly called out in the release notes for e.g. 1.5.0 (though it's possible I missed something). The warning can of course be suppressed by writing np.float32(1.02) instead of the simple literal, but it doesn't seem great to require users to write this kind of stuff for every literal assignment.
Author: pandas-dev
Top answer
1 of 2
2

It might not be a direct answer to the question but I think for practical purposes it should be adressed as a data science question.

First, a value of 1.4e258 feel rather suspicious as it is hard to picture where it could have a meaningfull physical signification. Such extreme values might screw your metrics and your model badly. The question should be about it being an outlier or not. The answer depends on your data, their meaning.

  • If it is an oultier (as in extreme value), the right approach might be to remove the instance entirely. This will probably boost the performance of your trained model on the rest of the instances. The downside is that the model won't perform well on that instance or similar extreme values. Practically, this requires that you make everyone using the model aware of its limitations on those extreme values.

  • If it is not an outlier you should consider modifying it to make it more informative, both to human and machine. That could mean using a more meaningful scale like a logarithmic one : it would be easier to manipulate for humans and might avoid significant calculation problems. Another approach would be to use some sort of renormalisation. For exemple if all your values are between 1e250 and 1e260 you could divide them by 1e255. If the variable that take such values depends on another one, you might renormalize by this value or one of its power, like if it is a volume, you might want to renormalise by a size variable to the 3rd power. That might help, both to avoid sklearn calculation problems and make more meaningful models.

2 of 2
1

It's a numerical precision problem. There is no current solution. The numbers are huge

I can replicate using this:

import numpy as np
from sklearn.ensemble import AdaBoostRegressor


X = np.repeat([1.4619664882428694e+258],100)
X = X.reshape(10,10)
y = np.ones((10,1))

model = AdaBoostRegressor()
model.fit(X,y)

ValueError: Input contains NaN, infinity or a value too large for dtype('float32').

np.all(np.isfinite(X))
True

I opened a request here: https://github.com/scikit-learn/scikit-learn/issues/15628

🌐
NumPy
numpy.org › doc › 2.1 › user › basics.types.html
Data types — NumPy v2.1 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)
🌐
NumPy
numpy.org › devdocs › reference › arrays.dtypes.html
Data type objects (dtype) — NumPy v2.6.dev0 Manual
>>> dt = np.dtype('uint32') # 32-bit unsigned integer >>> dt = np.dtype('float64') # 64-bit floating-point number
🌐
GitHub
github.com › tinygrad › tinygrad › issues › 1383
numpy default cast to float64 in some cases · Issue #1383 · tinygrad/tinygrad
July 31, 2023 - ON CPU In [9]: cmp = Tensor([1, 0], dtype=dtypes.int32) == Tensor([1.1, 1.0], dtype=dtypes.float32) In [10]: cmp.realize() KeyError: 'float64' In [17]: Tensor([1], dtype=dtypes.int32).exp().realize() KeyError: 'float64' In [17]: Tensor([1], dtype=dtypes.int32).log().realize() KeyError: 'float64' In [38]: Tensor([1], dtype=dtypes.int32).sin().realize() KeyError: 'float64' In [4]: Tensor([1], dtype=dtypes.int32).sqrt().realize() KeyError: 'float64' There might be more · There are no internal ways to change numpy's default casting behavior numpy/numpy#6860 · So some possible solutions of covering this issue are: change to UnaryOps.SIN: lambda x: np.sin(x, dtype=np.float32) (this doesn't seem right cuz what about when x is float16) overload/wrap the numpy functions to cast to float32 if float64?
Author: tinygrad
🌐
The Neural Base
theneuralbase.com › home › numpy for ml › beginner course › dtype: float32, float64, int32
dtype: float32, float64, int32 | Numpy For Ml Beginner Course | The Neural Base
A correct answer identifies: (1) GPU memory overflow (int64 indices don't fit), (2) slow GPU computation (float64 is slow on GPU), (3) dtype mismatch with PyTorch/TensorFlow defaults. The fix is one line: convert the entire array to float32 and int64 before training. VERSION NumPy 2.0+ removed ...
Find elsewhere
🌐
NumPy
numpy.org › doc › stable › user › basics.types.html
Data types — NumPy v2.5 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)
🌐
Python⇒Speed
pythonspeed.com › articles › float64-float32-precision
The problem with float32: you only get 16 million values
February 1, 2023 - Libraries like NumPy and Pandas ... or 64-bit floats) to numpy.float32 (“single-precision” or 32-bit floats) cuts memory usage in half....
🌐
APXML
apxml.com › courses › essential-numpy-pandas › chapter-2-getting-started-numpy-arrays › array-data-types
NumPy Array Data Types (dtypes)
Notice in the last example, because the list contained both integers and a float, NumPy automatically inferred the most general type that could accommodate all elements, which is float64 in this case. You don't have to rely on NumPy's automatic inference. You can explicitly specify the desired data type when creating an array using the dtype argument. This is useful for controlling memory usage or ensuring a specific precision. import numpy as np # Specify float32 arr_float32 = np.array([1, 2, 3], dtype=np.float32) print(f"Array: {arr_float32}") print(f"Data Type: {arr_float32.dtype}") # Speci
🌐
NumPy
numpy.org › devdocs › user › basics.types.html
Data types — NumPy v2.6.dev0 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)
🌐
GitHub
github.com › tensorflow › models › issues › 7719
TypeError: x and y must have the same dtype, got tf.float32 != tf.float64 · Issue #7719 · tensorflow/models
October 25, 2019 - Traceback (most recent call last): File "./run_train.py", line 49, in <module> tf.app.run() … File "/root/anaconda3/envs/hrl/lib/python3.7/site-packages/tensorflow_core/python/ops/math_ops.py", line 997, in _truediv_python3 (x_dtype, y_dtype)) TypeError: x and y must have the same dtype, got tf.float32 != tf.float64 In call to configurable 'train_uvf' (<function train_uvf at 0x7f0b2e5ef320>)
Author: tensorflow
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

🌐
GitHub
github.com › numpy › numpy › issues › 18087
BUG? np.array unexpectedly taking np.float32/64 instance as dtype · Issue #18087 · numpy/numpy
December 29, 2020 - ### Lines yielding errors: error1 = np.array(0.0, float(1.0)) error2 = np.array(0.0, np.float(1.0)) ### Lines not yielding errors: good1 = np.array(0.0, np.float32(1.0)) good2 = np.array(0.0, np.float64(1.0))
Author: numpy
🌐
GitHub
github.com › numpy › numpy › issues › 19716
`ndarray.astype` typing ignores the passed dtype · Issue #19716 · numpy/numpy
August 19, 2021 - Mypy is not aware that changing the dtype of an array changes its annotated type, even if the dtype is passed as a literal np.dtype[np.float64], np.dtype[np.float32], etc. I imagine that this kind of type inference could be very difficul...
Author: numpy
🌐
GitHub
github.com › huggingface › datasets › issues › 8469
Empty column loses its dtype under numpy format (always float32) · Issue #8469 · huggingface/datasets
August 12, 2026 - .tolist() drops the arrow type. For a non-empty list numpy re-infers it correctly; for [] it falls back to float64. NumpyFormatter._tensorize then applies its float default on top: elif isinstance(value, np.ndarray) and np.issubdtype(value.dtype, np.floating): default_dtype = {"dtype": np.float32}
🌐
NumPy
numpy.org › doc › 2.1 › reference › arrays.dtypes.html
Data type objects (dtype) — NumPy v2.1 Manual
>>> dt = np.dtype('uint32') # 32-bit unsigned integer >>> dt = np.dtype('float64') # 64-bit floating-point number