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

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
Converting float64 to float32 rounds off data in hdu object
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. More on github.com
🌐 github.com
8
July 26, 2017
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
Type conversion from float64 to float32 (cpu) sometimes crashes
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) # torc More on github.com
🌐 github.com
2
May 21, 2019
🌐
Python⇒Speed
pythonspeed.com › articles › float64-float32-precision
The problem with float32: you only get 16 million values
February 1, 2023 - So how do you fit float64s into float32s without losing precision? By transforming the data so it has a range of at most 16 million (centered around zero!) for a given level of precision.
🌐
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
🌐
GitHub
github.com › astropy › astropy › issues › 6845
Converting float64 to float32 rounds off data in hdu object · Issue #6845 · astropy/astropy
July 26, 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
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 › 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
🌐
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)
Find elsewhere
🌐
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))
🌐
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
... np.float32 reduces memory footprint ... hardware (GPUs often use float32). np.float64 gives more accurate results for computations sensitive to rounding or requiring higher precision; CPU math on float64 may be faster or equally ...
🌐
Google Groups
groups.google.com › g › julia-users › c › Lq2gQf4Ktp4
64 bit system: Force Float default to Float32
-E ... Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message ... Note also that while Float64 * Float32 (and other arithmetic ops) produce Float64, for other "less precise" numeric types, Float32 wins out, making ...
🌐
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.
🌐
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?

🌐
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) See Specifying and constructing data types for more information about specifying and constructing data type objects, including how to specify parameters like the byte order. 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.
🌐
GitHub
github.com › numba › numba › issues › 7478
Incorrect silent conversion from float64 to float32 in cuda jitted function · Issue #7478 · numba/numba
October 11, 2021 - I have included a self contained code sample to reproduce the problem. i.e. it's possible to run as 'python bug.py'. When calling a cuda jitted kernel with the wrong input type, numba silently converts it and this conversion yields bugs. For example, this kernel correctly print all elements of an array (in any order): import numpy as np from numba import cuda @cuda.jit() def kernel(a): i = cuda.grid(1) if i < len(a): print(a[i]) a = np.array([1, 2, 3], dtype=np.float64) kernel[1, 3](a) ... However, when demanding the input to be float32 but still passing a float64 array, something goes wrong during conversion (it appears to convert to zeros).
Author   RendersJens
🌐
Google Groups
groups.google.com › g › jep-project › c › tiKRsN-7lMM
Problem getting Python float32 as Java Float instead of Double
October 25, 2018 - That behavior also seems to be platform dependent within numpy, on the system I am using right now a numpy float32 does not extend a Python float so Jep will only convert it to a String, however numpy float64 does extend a Python float so it inherits the smarter Jep conversion.
🌐
Quora
quora.com › What-are-the-differences-between-float32-and-float64
What are the differences between float32 and float64? - Quora
Mixed precision options: keep accumulators in float64 while storing data in float32, use compensated summation (Kahan) for float32, or use iterative refinement to recover double precision results from lower precision computations.