The problem is that you do not do any type conversion of the numpy array. You calculate a float32 variable and put it as an entry into a float64 numpy array. numpy then converts it properly back to float64

Try someting like this:

a = np.zeros(4,dtype="float64") 
print a.dtype
print type(a[0])
a = np.float32(a)
print a.dtype
print type(a[0])

The output (tested with python 2.7)

float64
<type 'numpy.float64'>
float32
<type 'numpy.float32'>

a is in your case the array tree.tree_.threshold

Answer from Glostas on Stack Overflow
🌐
IncludeHelp
includehelp.com › python › how-to-convert-numpy-array-type-and-values-from-float64-to-float32.aspx
Python - How to convert NumPy array type and values from Float64 to Float32?
# Import numpy import numpy as np # Creating a numpy array arr = np.ones(4,dtype="float64") # Display original array print("Original Array:\n",arr,"\n") # Display type of original array print("type of Original Array:\n",arr.dtype,"\n") # Converting array into float32 arr = np.float32(arr) # Display type of modified array print("type of modified array:\n",arr.dtype,"\n")
Discussions

numpy - How to force python float operation on float32 rather than float64? - Stack Overflow
I want to do some math operations ... than on float64 type. I need do these operations on number or numpy.array, and also some numpy math functions, such as sqrt mean. How do I do this? ... >>>PI=3.1415926535897 >>> print PI*PI 9.86960440109 >>> PI32=numpy.float32(PI) >>> print PI32*PI32 9.86961 · If you want to do math operation on float32, convert the operands ... More on stackoverflow.com
🌐 stackoverflow.com
Convert list of numpy.float64 to float in Python quickly - Stack Overflow
What is the fastest way of converting a list of elements of type numpy.float64 to type float? I am currently using the straightforward for loop iteration in conjunction with float(). I came acros... More on stackoverflow.com
🌐 stackoverflow.com
How to convert np.float32 to Python float easily?
Hi, I try to convert np.float32 to a Python float in my project, and I find it's not eaisly. I have to convert it to str and then convert to float. Here is the code: Reproducing code example: i... More on github.com
🌐 github.com
1
May 29, 2019
How do I convert a python float to a Float32?
I am trying to iterate over a Python list and compare the values to those in a Mojo Float32 list, but I have not found a way of converting a Python float to any Mojo type for comparison. I’ve tried Float32(f.to_float64(… More on forum.modular.com
🌐 forum.modular.com
1
0
January 27, 2026
🌐
GitHub
github.com › astropy › astropy › issues › 6845
Converting float64 to float32 rounds off data in hdu object · Issue #6845 · astropy/astropy
July 27, 2017 - To save hardisk space, I wanted to convert my float64 fits images to float32. I used the hdulist[0].scale('float32') method to do that.
Author   indiajoe
🌐
Python⇒Speed
pythonspeed.com › articles › float64-float32-precision
The problem with float32: you only get 16 million values
February 1, 2023 - >>> arr = np.arange(0, 8388608, 0.5, dtype=np.float64) >>> arr[-4:] array([8388606. , 8388606.5, 8388607. , 8388607.5]) >>> arr[-4:].astype(np.float32) array([8388606. , 8388606.5, 8388607.
🌐
w3resource
w3resource.com › python-exercises › numpy › basic › numpy-basic-exercise-41.php
NumPy: Convert numpy dtypes to native python types - w3resource
August 28, 2025 - This process ensures compatibility between NumPy arrays and standard Python operations by enabling seamless data type conversions. ... # Importing the NumPy library with an alias 'np' import numpy as np # Printing a message indicating conversion from numpy.float32 to Python float print("numpy.float32 to python float") # Creating a numpy.float32 value 'x' initialized to 0 x = np.float32(0) # Printing the type of 'x' print(type(x)) # Extracting the Python float value from the numpy.float32 'x' using the item() method pyval = x.item() # Printing the type of the extracted Python float value 'pyval' print(type(pyval))
Top answer
1 of 3
31

The tolist() method should do what you want. If you have a numpy array, just call tolist():

In [17]: a
Out[17]: 
array([ 0.        ,  0.14285714,  0.28571429,  0.42857143,  0.57142857,
        0.71428571,  0.85714286,  1.        ,  1.14285714,  1.28571429,
        1.42857143,  1.57142857,  1.71428571,  1.85714286,  2.        ])

In [18]: a.dtype
Out[18]: dtype('float64')

In [19]: b = a.tolist()

In [20]: b
Out[20]: 
[0.0,
 0.14285714285714285,
 0.2857142857142857,
 0.42857142857142855,
 0.5714285714285714,
 0.7142857142857142,
 0.8571428571428571,
 1.0,
 1.1428571428571428,
 1.2857142857142856,
 1.4285714285714284,
 1.5714285714285714,
 1.7142857142857142,
 1.857142857142857,
 2.0]

In [21]: type(b)
Out[21]: list

In [22]: type(b[0])
Out[22]: float

If, in fact, you really have python list of numpy.float64 objects, then @Alexander's answer is great, or you could convert the list to an array and then use the tolist() method. E.g.

In [46]: c
Out[46]: 
[0.0,
 0.33333333333333331,
 0.66666666666666663,
 1.0,
 1.3333333333333333,
 1.6666666666666665,
 2.0]

In [47]: type(c)
Out[47]: list

In [48]: type(c[0])
Out[48]: numpy.float64

@Alexander's suggestion, a list comprehension:

In [49]: [float(v) for v in c]
Out[49]: 
[0.0,
 0.3333333333333333,
 0.6666666666666666,
 1.0,
 1.3333333333333333,
 1.6666666666666665,
 2.0]

Or, convert to an array and then use the tolist() method.

In [50]: np.array(c).tolist()
Out[50]: 
[0.0,
 0.3333333333333333,
 0.6666666666666666,
 1.0,
 1.3333333333333333,
 1.6666666666666665,
 2.0]

If you are concerned with the speed, here's a comparison. The input, x, is a python list of numpy.float64 objects:

In [8]: type(x)
Out[8]: list

In [9]: len(x)
Out[9]: 1000

In [10]: type(x[0])
Out[10]: numpy.float64

Timing for the list comprehension:

In [11]: %timeit list1 = [float(v) for v in x]
10000 loops, best of 3: 109 µs per loop

Timing for conversion to numpy array and then tolist():

In [12]: %timeit list2 = np.array(x).tolist()
10000 loops, best of 3: 70.5 µs per loop

So it is faster to convert the list to an array and then call tolist().

2 of 3
11

You could use a list comprehension:

floats = [float(np_float) for np_float in np_float_list]
🌐
GitHub
github.com › numpy › numpy › issues › 14150
How to convert np.float32 to Python float easily? · Issue #14150 · numpy/numpy
May 29, 2019 - Hi, I try to convert np.float32 to a Python float in my project, and I find it's not eaisly. I have to convert it to str and then convert to float. Here is the code: Reproducing code example: import numpy as np x = np.float32(1.9) x.toli...
Author   ringsaturn
Find elsewhere
🌐
Modular
forum.modular.com › mojo
How do I convert a python float to a Float32? - Mojo - Modular
January 27, 2026 - I am trying to iterate over a Python list and compare the values to those in a Mojo Float32 list, but I have not found a way of converting a Python float to any Mojo type for comparison. I’ve tried Float32(f.to_float64()), but to_float64() returns a PythonObject, and Float32 cannot convert a PythonObject.
🌐
GitHub
github.com › pytorch › pytorch › issues › 20755
Type conversion from float64 to float32 (cpu) sometimes crashes · Issue #20755 · pytorch/pytorch
May 21, 2019 - import torch a = torch.rand(3, 3, dtype = torch.float64) print(a.dtype, a.device) # torch.float64 cpu c = a.to(torch.float32) #works b = torch.load('bug.pt') print(b.dtype, b.device) # torch.float64 cpu c = b.to(torch.float32) # RuntimeError: expected scalar type Float but found Double d = b.clone().to(torch.float32) # works
Published   May 21, 2019
Author   vadimkantorov
🌐
Reddit
reddit.com › r/learnpython › [beginner] how to best convert a 64bit integer to a 32 bit float
r/learnpython on Reddit: [beginner] How to best convert a 64bit integer to a 32 bit float
August 9, 2022 -

I was trying numpy for matrix calculations and I used it to solve simultaneous eqns.

I have a matrix, `answer` with desired values shown as:

[[4.]
[2.]
[5.]]

I realise that the dots are because the actual values are a float, so I tried `answer.tolist()`

This gives me:

[[3.9999999999999987], [1.9999999999999996], [4.999999999999998]]

In my program, I want to convert this to an integer, however using Python's `int()` function means it becomes: 3, 1, 4

I also tried using `.astype()` to convert to an int:

answer.astype(int,casting='same_kind'))

but I get:

TypeError: Cannot cast array data from dtype('float64') to dtype('int32') according to the rule 'same_kind'

I am sure importing the ceiling/floor function from `math` would solve this, but I am aware that some results may end up being normal decimals of maybe 4dp, rather than .999999 or .111111 recurring, so rounding isn't the best option

Any advice on the best way of converting?

🌐
Stack Overflow
stackoverflow.com › questions › 78625587 › fail-to-convert-float-64-to-float-32-in-python
pandas - Fail to convert float 64 to float 32 in python - Stack Overflow
col1 float64 col2 float64 dtype: object · convert to float32 · out = df.astype('float32') print(out.dtypes) orint: col1 float32 col2 float32 dtype: object · Most functions in Pandas do not change the original as a result of applying the function. Share · Improve this answer ·
🌐
Google Groups
groups.google.com › g › julia-users › c › Lq2gQf4Ktp4
64 bit system: Force Float default to Float32
The best way I can come up with is to use some type aliasing. typealias Float Float32 Then, wherever you may need to assign a float you would simply constrain the variable as being `Float`. x::Float = 1.0 This should make switching between 64/32/16-bit floats easy and keep your code reasonably ...
Top answer
1 of 3
8

Yes, actually when you use Python's native float to specify the dtype for an array , numpy converts it to float64. As given in documentation -

Note that, above, we use the Python float object as a dtype. NumPy knows that int refers to np.int_, bool means np.bool_ , that float is np.float_ and complex is np.complex_. The other data-types do not have Python equivalents.

And -

float_ - Shorthand for float64.

This is why even though you use float to convert the whole array to float , it still uses np.float64.

According to the requirement from the other question , the best solution would be converting to normal float object after taking each scalar value as -

float(new_array[0])

A solution that I could think of is to create a subclass for float and use that for casting (though to me it looks bad). But I would prefer the previous solution over this if possible. Example -

In [20]: import numpy as np

In [21]: na = np.array([1., 2., 3.])

In [22]: na = np.array([1., 2., 3., np.inf, np.inf])

In [23]: type(na[-1])
Out[23]: numpy.float64

In [24]: na[-1] - na[-2]
C:\Anaconda3\Scripts\ipython-script.py:1: RuntimeWarning: invalid value encountered in double_scalars
  if __name__ == '__main__':
Out[24]: nan

In [25]: class x(float):
   ....:     pass
   ....:

In [26]: na_new = na.astype(x)


In [28]: type(na_new[-1])
Out[28]: float                           #No idea why its showing float, I would have thought it would show '__main__.x' .

In [29]: na_new[-1] - na_new[-2]
Out[29]: nan

In [30]: na_new
Out[30]: array([1.0, 2.0, 3.0, inf, inf], dtype=object)
2 of 3
3

You can create an anonymous type float like this

>>> new_array = my_array.astype(type('float', (float,), {}))
>>> type(new_array[0])
<type 'float'>
🌐
PyTorch Forums
discuss.pytorch.org › t › is-there-an-efficient-way-to-convert-a-model-with-float32-params-to-float64 › 130767
Is there an efficient way to convert a model with float32 params to float64? - PyTorch Forums
August 31, 2021 - Hi All, I was wondering if it’s at all possible to efficiently convert all my model parameters from float32 to float64? I’ve pretrained my model in float32, but when running it I get a NaN error but I know that model wo…
🌐
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 casts or casts within a kind, like float64 to float32, are allowed.
🌐
NumPy
numpy.org › doc › stable › user › basics.types.html
Data types — NumPy v2.4 Manual
To convert the type of an array, use the .astype() method. For example: ... Note that, above, we could have used the Python float object as a dtype instead of numpy.float64. NumPy knows that int refers to numpy.int_, bool means numpy.bool, that float is numpy.float64 and complex is numpy.complex128.
🌐
AskPython
askpython.com › home › python string to float, float to string
Python String to float, float to String - AskPython
February 16, 2023 - Python provides us with the built-in float() method to convert the data type of input from String to float.
🌐
Quora
quora.com › What-is-np-float32-and-np-float64-in-numpy-in-simple-terms
What is np.float32 and np.float64 in numpy in simple terms? - Quora
Answer (1 of 2): np.float32 - It means that each value in the numpy array would be a float of size 32 bits. np.float64- It means that each value in the numpy array would be a float of size 64. If you are concerned about storing big numbers you should consider using float64, however, it takes mo...